Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <unistd.h>
  5. #include <sys/wait.h>
  6. #include <memory.h>
  7. #include <sys/fcntl.h>
  8.  
  9. #define VEC_SIZE 1000
  10. #define CHILD_NUMBER 5
  11. #define VEC_SLICE 200
  12.  
  13. /* Creates a vector of CHILD_NUMBER pipes. */
  14. void create_pipes(int pipes[CHILD_NUMBER][2]) {
  15. int i;
  16. for (i = 0; i < CHILD_NUMBER; i++)
  17. /* In case of an error exits with code -1 */
  18. if (pipe(pipes[i]) == -1) {
  19. exit(-1);
  20. }
  21. }
  22.  
  23. void initialize_array(int numbers[VEC_SIZE]) {
  24. time_t t; /* needed to initialize random number generator (RNG) */
  25. int i;
  26.  
  27. /* intializes RNG (srand():stdlib.h; time(): time.h) */
  28. srand((unsigned) time(&t));
  29.  
  30. /* initialize array with random numbers (rand(): stdlib.h) */
  31. for (i = 0; i < VEC_SIZE; i++) {
  32. numbers[i] = rand() % 255;
  33. }
  34. }
  35.  
  36. /* Creates CHILD_NUMBER child processes, stores each pid in the pids array and returns each child process id. */
  37. int babymaker() {
  38. int i;
  39. pid_t p;
  40.  
  41. for (i = 0; i < CHILD_NUMBER; i++) {
  42. p = fork();
  43. /* In case of an error exits with code -1 */
  44. if (p < 0) {
  45. exit(-1);
  46. }
  47. /* Child process returns it's id */
  48. if (p == 0) {
  49. return i + 1;
  50. }
  51. }
  52. /* Parent process returns 0 */
  53. return 0;
  54. }
  55.  
  56. /* Closes all the reading descriptors and all the writing descriptors except the one that belongs to the child.*/
  57. void close_child_file_descriptors(int pipes[CHILD_NUMBER][2], int id) {
  58. int i;
  59. for (i = 0; i < CHILD_NUMBER; i++) {
  60. close(pipes[i][0]);
  61. if (id != i) {
  62. close(pipes[i][1]);
  63. }
  64. }
  65. }
  66.  
  67. /* Closes all the writing file descriptors for the parent process */
  68. void close_parent_file_descriptors(int pipes[CHILD_NUMBER][2]) {
  69. int i;
  70. for (i = 0; i < CHILD_NUMBER; i++) {
  71. close(pipes[i][1]);
  72. }
  73. }
  74.  
  75. /* Each child process sums two values from the corresponding part of the vectors (vec1 and vec2) and writes each value in the pipe,
  76. when all the corresponding values are written the process closes the file descriptor previously used to write in the pipe. */
  77. void sum_vectors(int id, const int vec1[VEC_SIZE], const int vec2[VEC_SIZE], int pipes[CHILD_NUMBER][2]) {
  78. int start = (id - 1) * VEC_SLICE, i, identifier = id - 1, sum;
  79. for (i = 0; i < VEC_SLICE; i++) {
  80. sum = vec1[start + i] + vec2[start + i];
  81. write(pipes[identifier][1], &sum, sizeof(sum));
  82. }
  83. close(pipes[identifier][1]);
  84. }
  85.  
  86. /* The parent process reads all the values written in the pipes by the children processes, stores them in the res array
  87. * and when it's finished closes the file descriptor previously used to read from the pipe.*/
  88. void accumulate_results(int pipes[CHILD_NUMBER][2], int res[VEC_SIZE]) {
  89. int i, j, index = 0;
  90. for (i = 0; i < CHILD_NUMBER; i++) {
  91. for (j = 0; j < VEC_SLICE; j++) {
  92. read(pipes[i][0], &res[index], sizeof(res[index]));
  93. index++;
  94. }
  95. close(pipes[i][0]);
  96. }
  97. }
  98.  
  99. /* Prints the values from vec1, vec2 and results arrays.*/
  100. void print_results(int vec1[VEC_SIZE], int vec2[VEC_SIZE], int results[VEC_SIZE]) {
  101. int i;
  102. for (i = 0; i < VEC_SIZE; i++)
  103. printf("%d + %d = %d\n", vec1[i], vec2[i], results[i]);
  104. }
  105.  
  106. int main() {
  107. int vec1[VEC_SIZE], vec2[VEC_SIZE], res[VEC_SIZE];
  108. int id;
  109. int pipes[CHILD_NUMBER][2];
  110.  
  111. initialize_array(vec1);
  112. initialize_array(vec2);
  113.  
  114. create_pipes(pipes);
  115.  
  116. id = babymaker(5);
  117.  
  118. if (id != 0) {
  119. /* Child process */
  120. close_child_file_descriptors(pipes, id - 1);
  121. sum_vectors(id, vec1, vec2, pipes);
  122. exit(id);
  123. } else {
  124. /* Parent process */
  125. close_parent_file_descriptors(pipes);
  126. accumulate_results(pipes, res);
  127. print_results(vec1, vec2, res);
  128. }
  129. return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement