Advertisement
Guest User

Untitled

a guest
May 27th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. //controller.c
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <fcntl.h>
  7. #include <sys/stat.h>
  8.  #include <unistd.h>
  9.  
  10. #define BUFSIZE 256
  11.  
  12. main()
  13. {
  14.     int c1p[2]; //pipe to link c1 to c2
  15.     int c1pid;  //parent ID
  16.     int c1status;
  17.  
  18.     int c2p[2]; //pipe
  19.     int c2pid;  //parent ID
  20.     int c2status;
  21.  
  22.     int c3p[2]; //pipe
  23.     int c3pid;  //parent ID
  24.     int c3status;
  25.  
  26.     //buffer to store user words
  27.     char buffer[BUFSIZE];      
  28.  
  29.     //initialize pipes
  30.     pipe(c1p);
  31.     pipe(c3p);
  32.    
  33.    
  34.    
  35.     //begin child process of c1
  36.     c1pid = fork();
  37.     if (c1pid == 0) {
  38.         //set the newfd to be the duplicate descrition of the program
  39.         int newfd = dup2(c1p[1],1);
  40.         //if the duplication failed
  41.         if (newfd != 1)
  42.         {
  43.             fprintf(stderr,"unable to duplicate newfd \n");
  44.             exit(1);
  45.         }
  46.         //execute the program
  47.         execvp("./c1", NULL);
  48.         //close the program
  49.         close(c1p[0]);
  50.  
  51.  
  52.     } else {
  53.         //wait for the child process to finish
  54.         c1pid = wait(&c1status);
  55.         //print the exit status
  56.         int es = WEXITSTATUS(c1status);
  57.         printf("c1 finished \n");
  58.         printf("Exit status was %d\n\n", es);
  59.        
  60.     }
  61.  
  62.    
  63.     //begin child process of c2
  64.  
  65.     c2pid = fork();
  66.     if (c2pid == 0) {
  67.         //rediredt the stdout to the stdin of c2
  68.         dup2(c1p[1],0);
  69.         close(c1p[1]);
  70.         //set the newfd to be the duplicate descrition of the program
  71.  
  72.         int newfd = dup2(c2p[1],1);
  73.         //if the duplication failed
  74.  
  75.         if (newfd != 1)
  76.         {
  77.             fprintf(stderr,"unable to duplicate newfd \n");
  78.             exit(1);
  79.         }
  80.         //execute the program
  81.         execvp("./c2", NULL);
  82.         //close the prog
  83.         close(c2p[0]);
  84.        
  85.  
  86.        
  87.  
  88.     } else {
  89.         //wait for the child process to finish
  90.         c2pid = wait(&c2status);
  91.         int es = WEXITSTATUS(c2status);
  92.         //print the exit status
  93.         printf("c2 finished \n");
  94.         printf("Exit status was %d\n\n", es);
  95.     }
  96.  
  97.  
  98.     //begin child process of c3
  99.     c3pid = fork();
  100.     if (c3pid == 0) {
  101.         //rediredt the stdout to the stdin of c3
  102.         dup2(c3p[0],0);
  103.         int newfd = dup2(c3p[0],0);    
  104.         //ecxecute the program
  105.         execvp("./c3", NULL);
  106.         //close the program
  107.         close(c3p[1]);
  108.  
  109.     } else {
  110.         //wait for the child process to finish
  111.         c3pid = wait(&c3status);
  112.         //print the exit status
  113.         int es = WEXITSTATUS(c3status);
  114.         printf("c3 finished \n");
  115.         printf("Exit status was %d\n\n", es);
  116.     }
  117.    
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement