Advertisement
Guest User

Untitled

a guest
Feb 8th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1.   int pipe1[2], pipe2[2], pipe3[2];
  2.   pid_t pid;
  3.  
  4.   char string1[1000], string2[1000];
  5.  
  6.   if (pipe(pipe1) == -1)
  7.   {
  8.     perror("demo1");
  9.     exit(1);
  10.   }
  11.   if ((pid = fork()) == -1)
  12.   {
  13.     perror("demo2");
  14.     exit(1);
  15.   }
  16.   else if (pid == 0)
  17.   {
  18.     close(pipe1[0]);       // close read end of pipe
  19.     dup2(pipe1[1], 1);     // make 1 same as write-to end of pipe
  20.     close(pipe1[1]);       // close excess fildes
  21.     execvp(cmd1[0], cmd1); // execute
  22.     perror("demo3");       // still around?  exec failed
  23.     _exit(1);              // no flush
  24.   }
  25.   if (pipe(pipe2) == -1)
  26.   {
  27.     perror("demo4");
  28.     exit(1);
  29.   }
  30.   if ((pid = fork()) == -1)
  31.   {
  32.     perror("demo5");
  33.     exit(1);
  34.   }
  35.   else if (pid == 0)
  36.   {
  37.     close(pipe2[1]);
  38.     dup2(pipe2[0], 0);
  39.     execvp(cmd2[0], cmd2);
  40.     close(pipe2[0]);
  41.     perror("demo6");
  42.     _exit(1);
  43.   }
  44.  
  45.   if (pipe(pipe3) == -1)
  46.   {
  47.     perror("demo7");
  48.     exit(1);
  49.   }
  50.   if ((pid = fork()) == -1)
  51.   {
  52.     perror("demo8");
  53.     exit(1);
  54.   }
  55.   else if (pid == 0)
  56.   {
  57.     close(pipe3[1]);
  58.     dup2(pipe3[0], 0);
  59.     execvp(cmd3[0], cmd3);
  60.     close(pipe3[0]);
  61.     perror("demo9");
  62.     _exit(1);
  63.   }
  64.  
  65.   if (pid > 0)
  66.   {
  67.     /*
  68.     Read from pipe 1
  69.     */
  70.     close(pipe1[1]);   //close write end of pipe
  71.     dup2(pipe1[0], 0); // make 0 same as read-from end of pipe
  72.     int string1_length = read(pipe1[0], string1, 1000);
  73.     close(pipe1[0]);
  74.  
  75.     /*
  76.     Write to pipe 2
  77.     */
  78.     dup2(pipe2[1], 1);
  79.     close(pipe2[1]);
  80.  
  81.     /*
  82.     Write to pipe 3
  83.     */
  84.     dup2(pipe3[1], 1);
  85.     close(pipe3[1]);
  86.     perror("demo10");
  87.     exit(1);
  88.   }
  89.   return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement