daily pastebin goal
2%
SHARE
TWEET

Untitled

a guest Sep 12th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top