Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/shm.h>
- int main ()
- {
- int i, j, row, col, id;
- int status = 0;
- row = 4;
- col = 4;
- //default test matrices
- int M[4][4] = {
- {10, 20, 30, 40},
- {5, 6, 7, 8},
- {4, 3, 2, 1},
- {8, 7, 6, 5}
- };
- int N[4][4] = {
- {10, 30, 50, 70},
- {2, 4, 6, 8},
- {7, 3, 5, 7},
- {8, 6, 4, 2}
- };
- int Q[4][4] = {
- {0, 0, 0, 0},
- {0, 0, 0, 0},
- {0, 0, 0, 0},
- {0, 0, 0, 0}
- };
- int shmid;
- srand ((unsigned int) getpid ());
- // get a shared memory id of 2D array with size of 4 * 4
- shmid = shmget ((key_t) 1234, sizeof (int[4][4]), 0666 | IPC_CREAT);
- // check is the memory failed
- if (shmid == -1)
- {
- fprintf (stderr, "shmget failed\n");
- exit (EXIT_FAILURE);
- }
- // allocate the memory
- int *shared_memory = (int *) shmat (shmid, 0, 0);
- if (shared_memory == (void *) -1)
- {
- fprintf (stderr, "shmat failed\n");
- exit (EXIT_FAILURE);
- }
- // initialize the shared memory
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- shared_memory[i * 4 + j] = 0;
- }
- }
- pid_t pid = fork ();
- if (pid == 0) //first child process
- {
- int P = 0;
- printf ("First child process working with row %d\n", P + 1);
- // calculate row 1
- for (int k = 0; k < col; k++)
- {
- for (int j = 0; j < col; j++)
- {
- shared_memory[P * 4 + k] += M[P][j] * N[j][k]; //multiply the numbers then add it to the sum in the product matrix
- }
- printf (" %d ", shared_memory[P * 4 + k]);
- }
- printf ("\n");
- exit (0);
- }
- else
- {
- pid_t pid2 = fork ();
- if (pid2 == 0) //second child process
- {
- int P = 1;
- printf ("Second child process working with row %d\n", P + 1);
- // calculate row 2
- for (int k = 0; k < col; k++)
- {
- for (int j = 0; j < col; j++)
- {
- shared_memory[P * 4 + k] += M[P][j] * N[j][k]; //multiply the numbers then add it to the sum in the product matrix
- }
- printf (" %d ", shared_memory[P * 4 + k]);
- }
- printf ("\n");
- exit (0);
- }
- else
- {
- pid_t pid3 = fork ();
- if (pid3 == 0) //third child process
- {
- int P = 2;
- printf ("Third child process working with row %d\n", P + 1);
- // calculate row 3
- for (int k = 0; k < col; k++)
- {
- for (int j = 0; j < col; j++)
- {
- shared_memory[P * 4 + k] += M[P][j] * N[j][k]; //multiply the numbers then add it to the sum in the product matrix
- }
- printf (" %d ", shared_memory[P * 4 + k]);
- }
- printf ("\n");
- exit (0);
- }
- else
- {
- pid_t pid4 = fork ();
- if (pid4 == 0) //fourth child process
- {
- int P = 3;
- printf ("Fourth child process working with row %d\n",
- P + 1);
- // calculate row 4
- for (int k = 0; k < col; k++)
- {
- for (int j = 0; j < col; j++)
- {
- shared_memory[P * 4 + k] += M[P][j] * N[j][k]; //multiply the numbers then add it to the sum in the product matrix
- }
- printf (" %d ", shared_memory[P * 4 + k]);
- }
- printf ("\n");
- exit (0);
- }
- else
- {
- while ((wait (&status)) > 0);
- printf ("\n\n");
- //print M, N and Q
- printf ("M is: \n");
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- printf ("%d ", M[i][j]);
- }
- printf ("\n");
- }
- printf ("N is:\n");
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- printf ("%d ", N[i][j]);
- }
- printf ("\n");
- }
- printf ("Q is:\n");
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- printf (" %d ", shared_memory[i * 4 + j]);
- }
- printf (" \n", shared_memory[i * 4 + j]);
- }
- exit (0);
- }
- }
- }
- }
- exit (0);
- return 0;
- }
Add Comment
Please, Sign In to add comment