SpiralLaser

Assignment 1

Feb 13th, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6. #include <sys/shm.h>
  7.  
  8. int main ()
  9. {
  10. int i, j, row, col, id;
  11. int status = 0;
  12.  
  13. row = 4;
  14. col = 4;
  15.  
  16. //default test matrices
  17. int M[4][4] = {
  18. {10, 20, 30, 40},
  19. {5, 6, 7, 8},
  20. {4, 3, 2, 1},
  21. {8, 7, 6, 5}
  22. };
  23.  
  24. int N[4][4] = {
  25. {10, 30, 50, 70},
  26. {2, 4, 6, 8},
  27. {7, 3, 5, 7},
  28. {8, 6, 4, 2}
  29. };
  30.  
  31. int Q[4][4] = {
  32. {0, 0, 0, 0},
  33. {0, 0, 0, 0},
  34. {0, 0, 0, 0},
  35. {0, 0, 0, 0}
  36. };
  37.  
  38. int shmid;
  39. srand ((unsigned int) getpid ());
  40. // get a shared memory id of 2D array with size of 4 * 4
  41. shmid = shmget ((key_t) 1234, sizeof (int[4][4]), 0666 | IPC_CREAT);
  42.  
  43. // check is the memory failed
  44. if (shmid == -1)
  45. {
  46. fprintf (stderr, "shmget failed\n");
  47. exit (EXIT_FAILURE);
  48. }
  49. // allocate the memory
  50. int *shared_memory = (int *) shmat (shmid, 0, 0);
  51. if (shared_memory == (void *) -1)
  52. {
  53. fprintf (stderr, "shmat failed\n");
  54. exit (EXIT_FAILURE);
  55. }
  56. // initialize the shared memory
  57. for (int i = 0; i < 4; i++)
  58. {
  59. for (int j = 0; j < 4; j++)
  60. {
  61. shared_memory[i * 4 + j] = 0;
  62. }
  63. }
  64.  
  65. pid_t pid = fork ();
  66. if (pid == 0) //first child process
  67. {
  68. int P = 0;
  69. printf ("First child process working with row %d\n", P + 1);
  70.  
  71. // calculate row 1
  72. for (int k = 0; k < col; k++)
  73. {
  74. for (int j = 0; j < col; j++)
  75. {
  76. shared_memory[P * 4 + k] += M[P][j] * N[j][k]; //multiply the numbers then add it to the sum in the product matrix
  77. }
  78. printf (" %d ", shared_memory[P * 4 + k]);
  79. }
  80. printf ("\n");
  81. exit (0);
  82. }
  83. else
  84. {
  85. pid_t pid2 = fork ();
  86. if (pid2 == 0) //second child process
  87. {
  88. int P = 1;
  89. printf ("Second child process working with row %d\n", P + 1);
  90. // calculate row 2
  91.  
  92. for (int k = 0; k < col; k++)
  93. {
  94. for (int j = 0; j < col; j++)
  95. {
  96. shared_memory[P * 4 + k] += M[P][j] * N[j][k]; //multiply the numbers then add it to the sum in the product matrix
  97. }
  98. printf (" %d ", shared_memory[P * 4 + k]);
  99. }
  100. printf ("\n");
  101. exit (0);
  102. }
  103.  
  104. else
  105. {
  106. pid_t pid3 = fork ();
  107. if (pid3 == 0) //third child process
  108. {
  109. int P = 2;
  110. printf ("Third child process working with row %d\n", P + 1);
  111. // calculate row 3
  112.  
  113. for (int k = 0; k < col; k++)
  114. {
  115. for (int j = 0; j < col; j++)
  116. {
  117. shared_memory[P * 4 + k] += M[P][j] * N[j][k]; //multiply the numbers then add it to the sum in the product matrix
  118. }
  119. printf (" %d ", shared_memory[P * 4 + k]);
  120. }
  121.  
  122. printf ("\n");
  123. exit (0);
  124. }
  125. else
  126. {
  127. pid_t pid4 = fork ();
  128. if (pid4 == 0) //fourth child process
  129. {
  130. int P = 3;
  131. printf ("Fourth child process working with row %d\n",
  132. P + 1);
  133. // calculate row 4
  134.  
  135. for (int k = 0; k < col; k++)
  136. {
  137. for (int j = 0; j < col; j++)
  138. {
  139. shared_memory[P * 4 + k] += M[P][j] * N[j][k]; //multiply the numbers then add it to the sum in the product matrix
  140. }
  141. printf (" %d ", shared_memory[P * 4 + k]);
  142. }
  143. printf ("\n");
  144. exit (0);
  145. }
  146. else
  147. {
  148. while ((wait (&status)) > 0);
  149. printf ("\n\n");
  150.  
  151. //print M, N and Q
  152. printf ("M is: \n");
  153. for (int i = 0; i < 4; i++)
  154. {
  155. for (int j = 0; j < 4; j++)
  156. {
  157. printf ("%d ", M[i][j]);
  158. }
  159. printf ("\n");
  160. }
  161. printf ("N is:\n");
  162. for (int i = 0; i < 4; i++)
  163. {
  164. for (int j = 0; j < 4; j++)
  165. {
  166. printf ("%d ", N[i][j]);
  167. }
  168. printf ("\n");
  169. }
  170.  
  171. printf ("Q is:\n");
  172. for (int i = 0; i < 4; i++)
  173. {
  174. for (int j = 0; j < 4; j++)
  175. {
  176. printf (" %d ", shared_memory[i * 4 + j]);
  177. }
  178. printf (" \n", shared_memory[i * 4 + j]);
  179. }
  180. exit (0);
  181. }
  182. }
  183.  
  184. }
  185. }
  186. exit (0);
  187. return 0;
  188. }
Add Comment
Please, Sign In to add comment