Don't like ads? PRO users don't see any ads ;-)
Guest

OS assignment

By: rohitkrishnanp on Jul 24th, 2012  |  syntax: None  |  size: 0.86 KB  |  hits: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //multiply a[2][3] * b[3][4] = c[2][4]
  2. //with fork
  3. //each child completes one c[2][4]'s row
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6.  
  7. main()
  8. {
  9.   int a[2][3]={1,2,3,
  10.            4,5,6};
  11.   int b[3][4]={1,2,3,4,
  12.            5,6,7,8,
  13.            9,10,11,12};
  14.   int c[2][4];
  15.       int i,j;
  16.   int m;    //for the calculation
  17.   int n;    //common 3
  18.   int pid;  //fork
  19.  
  20.   for(i=0;i<2;i++)
  21.   {
  22.     pid=fork();
  23.     if(pid==-1)
  24.     {
  25.       printf("Can't fork\n");
  26.     }
  27.     if(pid==0)//child
  28.     {
  29.       for(j=0;j<4;j++)
  30.       {
  31.     for(n=0;n<3;n++)
  32.     {
  33.       m=m+a[i][n]*b[n][j];
  34.     }
  35.     c[i][j]=m;
  36.     m=0;
  37.       }
  38.       exit(EXIT_SUCCESS);
  39.     }
  40.     if(pid>0)//parent
  41.     {
  42.       wait(0);
  43.     }
  44.   }
  45.   printf("C:\n");
  46.   for(i=0;i<2;i++)
  47.   {
  48.     for(j=0;j<4;j++)
  49.     {
  50.       printf("%d\t",c[i][j]);
  51.     }
  52.     printf("\n");
  53.   }        
  54. }