By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,441 Members | 979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,441 IT Pros & Developers. It's quick & easy.

Need some help with shared memory and fork system call

P: 3
Hello, I'm trying to make a program that will multiply 2 matrices using shmget() and fork(). For example, I would need to multiply a 64 x 64 matrix using 4 processes or 16 processes, and the multi-processes will be created using fork. Each process will calculate a partition of the final Matrix

Now, I'm not sure whether I should write all the matrices to the shared memory or just a single integer to keep track of what partition to calculate. I'm also not sure if I'm using fork correctly, as my print commands print out twice each instead of once.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <sys/shm.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7.  
  8.  
  9. #define DIM 4
  10. #define NUM_OF_FORK_CALLS 2
  11. #define NUM_OF_PROC 4
  12.  
  13. int main()
  14. {
  15.     pid_t pid;
  16.     int segment_id;
  17.     int *partition;
  18.     *partition = 0;
  19.     int i;
  20.     int j;
  21.     int matrixA[DIM][DIM];
  22.     int matrixB[DIM][DIM];
  23.     int matrixC[DIM][DIM];
  24.  
  25.     //allocating a shared memory segment
  26.     segment_id = shmget(IPC_PRIVATE, sizeof(int)*(DIM*DIM)*3, IPC_CREAT|0666);
  27.  
  28.     //attach the shared memory segment to the partition variable
  29.     partition = (int *) shmat(segment_id, NULL,0);
  30.  
  31.     //fill the matrices
  32.     for(i = 0; i < DIM; i++)
  33.     {
  34.     for(j = 0; j < DIM; j++)
  35.     {
  36.         matrixA[i][j] = i + j;
  37.         matrixB[i][j] = i + 3;
  38.     }
  39.     }
  40.  
  41.     for(i = 0; i < NUM_OF_FORK_CALLS; i++)
  42.     {
  43.        pid = fork();
  44.     }
  45.  
  46.  
  47.     //error occurred
  48.     if(pid < 0)
  49.     {
  50.         fprintf(stderr, "Fork Failed");
  51.         return 1;
  52.     }
  53.  
  54.     //child process
  55.     else if(pid==0)
  56.     {
  57.  
  58.         int start = (*partition * DIM)/NUM_OF_PROC;
  59.         /*The start position will tell which row to start calculating */
  60.  
  61.         int end = ((*partition+1) * DIM)/NUM_OF_PROC;
  62.         /*The end position wil tell which row to stop calculating*/
  63.  
  64.         int i;
  65.         int j;
  66.         int k;
  67.  
  68.          //These for loops will calculate a partition of C
  69.         for(i = start; i<end; i++)
  70.         {
  71.             for(j = 0; j < DIM; j++)
  72.             {
  73.                 matrixC[i][j] = 0;
  74.  
  75.                 for(k = 0; k < DIM; k++)
  76.                 {
  77.                      matrixC[i][j] += matrixA[i][k] * matrixB[k][j];
  78.                 }
  79.              }
  80.         }
  81.  
  82.     }
  83.  
  84.     //parent process
  85.     else
  86.     {
  87.  
  88.         wait(NULL);
  89.  
  90.         int i;
  91.         int j;
  92.  
  93.         //print matrix A
  94.         for(i = 0; i < DIM; i++)
  95.         {
  96.             for(j = 0; j<DIM; j++) 
  97.             {
  98.                 printf("%d ",matrixA[i][j]);  
  99.             }
  100.  
  101.             printf("\n");  
  102.         }
  103.  
  104.         printf("-----------\n\n");
  105.  
  106.         //print matrix B
  107.         for(i = 0; i < DIM; i++)
  108.         {
  109.             for(j = 0; j<DIM; j++)  
  110.             {
  111.                 printf("%d ",matrixB[i][j]);  
  112.             }
  113.  
  114.             printf("\n");  
  115.         }
  116.  
  117.          printf("-----------\n\n");
  118.  
  119.         //print matrix C
  120.         for(i = 0; i < DIM; i++)
  121.         {
  122.             for(j = 0; j<DIM; j++) 
  123.             {
  124.                 printf("%d ",matrixC[i][j]);  
  125.             }
  126.  
  127.             printf("\n"); 
  128.         }
  129.          printf("-----------\n\n");
  130.  
  131.     }
  132.  
  133.     return 0;
  134. }
  135.  
Any help would be appreciated, thanks in advance
Sep 19 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.