Guest User

Untitled

a guest
Sep 12th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. Having trouble with pipe / fork. Can't understand half of the manual (man)
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sys/wait.h>
  6. void main () {
  7. int a[] = {1,2,3,4}, f[2]; // ok so we initialize an array and a "pipe folder" (pipefd in manual) ?
  8. pipe (f); // not 100% sure what this does ?
  9. if (fork () == 0) { // if child
  10. close (f[0]); // close pipe read-end
  11. a[0] += a[1];
  12. write (f[1], &a[0], sizeof (int)) // fixed
  13. close (f[1]); // close pipe write-end
  14. exit(0); // closes child and sends status update to parent ?
  15. }
  16. else { // if parent
  17. close (f[1]) // close write-end of pipe
  18. a[2]+=a[3];
  19. read (f[0], &a, sizeof(int)) // fixed
  20. wait (0); // waits for child to ... close ? or just finish ? or is it the same thing
  21. a[0]+= a[2]; close (f[0]);
  22. printf ("%dn, "a[0]);
  23. }
  24. }
Add Comment
Please, Sign In to add comment