daily pastebin goal
27%
SHARE
TWEET

Untitled

a guest Jan 22nd, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6.  
  7. #define MAXSIZE 10
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11.     int i;
  12.     int p2c[2];
  13.     int child2p[2];
  14.     int val = 0;
  15.     int val2 = 0;
  16.     int threecount = 0;
  17.     int fivecount = 0;
  18.  
  19.     // create pipe descriptors
  20.     pipe(p2c);
  21.     pipe(child2p);
  22.  
  23.     // fork() returns 0 for child process, child-pid for parent process.
  24.     if (fork() != 0)
  25.     {
  26.         // parent: writing only, so close read-descriptor.
  27.         close(p2c[0]);
  28.         // parent: reading only, so close write descriptor
  29.         close(child2p[1]);
  30.         for(i = 0; i < MAXSIZE; i++)
  31.         {
  32.             // send the value on the write-descriptor to the child.
  33.             if(i % 5 == 0)
  34.             {
  35.                 fivecount++;
  36.  
  37.             }
  38.             else
  39.             {
  40.                 write(p2c[1],&i, sizeof(i));
  41.                 printf("Parent(%d) send value: %d\n", getpid(), i);
  42.                
  43.             }
  44.         }
  45.             // read from the child
  46.            
  47.  
  48.    
  49.         // close the write descriptor
  50.         close(p2c[1]);
  51.         read(child2p[0], &threecount,sizeof(threecount));      
  52.             printf("Parent(%d) received value: %d for threecount and has %d for fivecount.\n", getpid(), threecount, fivecount);
  53.  
  54.         wait();
  55.    
  56.         // close the read descriptor from the child
  57.         close(child2p[0]);
  58.     }
  59.     else
  60.     {   // child: reading only, so close the write-descriptor
  61.         close(p2c[1]);
  62.         // child: writing only, so close the read descriptor
  63.         close(child2p[0]);
  64.  
  65.         // now read the data from parent(will block)
  66.         while (read(p2c[0], &i, sizeof(i))){
  67.             printf("Child(%d) received value: %d\n", getpid(), i);
  68.             if(i % 3 == 0)
  69.             {
  70.             threecount++;
  71.             }
  72.         }
  73.        
  74.         // send to the
  75.         write(child2p[1], &threecount, sizeof(threecount));
  76.         printf("Child(%d) sendvalue: %d\n", getpid(), threecount);
  77.        
  78.        
  79.         // close the read-descriptor
  80.         close(p2c[0]);
  81.         // close the write-descriptor
  82.         close(child2p[1]);
  83.     }
  84.     return 0;
  85. }
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