Advertisement
Guest User

controller

a guest
May 27th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <sys/stat.h>
  6.  #include <unistd.h>
  7.  
  8. #define BUFSIZE 256
  9.  
  10. main()
  11. {
  12.     int c1p[2]; //pipe to link c1 to c2
  13.     int c1pid;  //parent ID
  14.     int c1status;
  15.  
  16.     int c2p[2]; //pipe
  17.     int c2pid;  //parent ID
  18.     int c2status;
  19.  
  20.     int c3p[2]; //pipe
  21.     int c3pid;  //parent ID
  22.     int c3status;
  23.  
  24.  
  25.     int len;           
  26.     char buffer[BUFSIZE];      
  27.     int read_len;
  28.  
  29.     pipe(c1p);
  30.    
  31.    
  32.    
  33.     c1pid = fork();
  34.     if (c1pid == 0) {
  35.         int newfd = 1;
  36.         newfd = dup2(c1p[1],newfd);
  37.         if (newfd != 1)
  38.         {
  39.             fprintf(stderr,"unable to duplicate newfd \n");
  40.             exit(1);
  41.         }
  42.         close(c1p[0]);
  43.         execvp("./c1", NULL);
  44.  
  45.  
  46.     } else {
  47.         c1pid = wait(&c2status);
  48.         printf("process 2 finished \n");
  49.     }
  50.  
  51.     pipe(c2p);
  52.  
  53.     c2pid = fork();
  54.     if (c2pid == 0) {
  55.         int newfd = 1;
  56.         newfd = dup2(c2p[1],newfd);
  57.         if (newfd != 1)
  58.         {
  59.             fprintf(stderr,"unable to duplicate newfd \n");
  60.             exit(1);
  61.         }
  62.         close(c2p[0]);
  63.         execvp("./c2", NULL);
  64.  
  65.  
  66.     } else {
  67.         c2pid = wait(&c2status);
  68.         printf("process 2 finished \n");
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement