Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. main()
  2. {
  3. int i;
  4. pid_t pid;
  5. int status = 0;
  6. int fd[2];
  7. int runningTotal = 0;
  8. pipe(fd);
  9.  
  10. int t;
  11. int r;
  12.  
  13. for (i = 0; i < 10; i++) {
  14. pid = fork();
  15. if (pid == 0){
  16. close(fd[0]);
  17. t = ChildProcess();
  18. write(fd[1], &t, sizeof(t));
  19. exit(0);
  20. }
  21. close(fd[1]);
  22. read(fd[0], &r, sizeof(r));
  23. runningTotal = runningTotal + r;
  24. wait(&status);
  25. }
  26.  
  27. printf("%in", runningTotal);
  28. }
  29.  
  30. int ChildProcess() {
  31. int i;
  32. int total = 0;
  33. int r = 0;
  34.  
  35. for (i = 0; i < 10000; i++) {
  36. r = rand() % 10; // 0 to 10
  37. total = total + r;
  38. }
  39.  
  40. printf("%in", total);
  41. return total;
  42. }
  43.  
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <time.h>
  47. #include <unistd.h>
  48.  
  49. int ChildProcess(void)
  50. {
  51. int total = 0;
  52. srand(time(0) + getpid());
  53.  
  54. for (int i = 0; i < 10000; i++)
  55. {
  56. int r = rand() % 10; // 0 to 10
  57. total = total + r;
  58. }
  59.  
  60. printf("%in", total);
  61. return total;
  62. }
  63.  
  64. int main(void)
  65. {
  66. int fd[2];
  67. pipe(fd); // Missing error check
  68.  
  69. for (int i = 0; i < 10; i++) {
  70. pid_t pid = fork();
  71. if (pid == 0){
  72. close(fd[0]);
  73. int t = ChildProcess();
  74. write(fd[1], &t, sizeof(t)); // Missing error check?
  75. exit(0);
  76. }
  77. // Print PID here? Error check?
  78. }
  79.  
  80. close(fd[1]);
  81.  
  82. int r;
  83. int runningTotal = 0;
  84. while (read(fd[0], &r, sizeof(r)) > 0) // Debugging opportunities here
  85. runningTotal = runningTotal + r;
  86.  
  87. while (wait(0) > 0) // Lots of debugging opportunities here
  88. ;
  89.  
  90. printf("%in", runningTotal);
  91. return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement