Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Having trouble with pipe / fork. Can't understand half of the manual (man)
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/wait.h>
- void main () {
- int a[] = {1,2,3,4}, f[2]; // ok so we initialize an array and a "pipe folder" (pipefd in manual) ?
- pipe (f); // not 100% sure what this does ?
- if (fork () == 0) { // if child
- close (f[0]); // close pipe read-end
- a[0] += a[1];
- write (f[1], &a[0], sizeof (int)) // fixed
- close (f[1]); // close pipe write-end
- exit(0); // closes child and sends status update to parent ?
- }
- else { // if parent
- close (f[1]) // close write-end of pipe
- a[2]+=a[3];
- read (f[0], &a, sizeof(int)) // fixed
- wait (0); // waits for child to ... close ? or just finish ? or is it the same thing
- a[0]+= a[2]; close (f[0]);
- printf ("%dn, "a[0]);
- }
- }
Add Comment
Please, Sign In to add comment