Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <error.h>
  5.  
  6. int main(int argc, char **argv)
  7. {
  8.     int fileDescriptors[2]; //used for the pipe command
  9.     pid_t childProcessID1; 
  10.     pid_t childProcessID2; 
  11.     int result;             //store the return Value
  12.     char buf;
  13.  
  14.  
  15.     //Create the interprocess channel
  16.     result = pipe(fileDescriptors);
  17.     if(result < 0)
  18.     {
  19.         //error while creating pipe
  20.         perror("pipe");
  21.         exit(1);
  22.     }
  23.  
  24.     //Creating the child Process
  25.     childProcessID1 = fork();
  26.     if(childProcessID1 < 0)
  27.     {
  28.         //error while forking
  29.         perror("fork");
  30.         exit(2);
  31.     }
  32.  
  33.     //Redirect STDOUT and execute first command
  34.     if(childProcessID1 == 0)
  35.     {
  36.         if(dup2(fileDescriptors[1], STDOUT_FILENO) == -1)   //Map STDOUT on write end of Communication Channel
  37.             perror("dup2");
  38.         if (close (fileDescriptors [0]) == -1)
  39.             perror ("close");
  40.         execlp(argv[1], argv[1], NULL);
  41.         return 0;
  42.     }
  43.  
  44.     //Redirect STDOUT and execute first command
  45.     if(dup2(fileDescriptors[0], STDIN_FILENO) == -1)    //Map STDIN on read end of Communication Channel to get the data from the first command
  46.         perror("dup2");
  47.     if (close (fileDescriptors [1]) == -1)
  48.         perror ("close");
  49.     execlp(argv[2], argv[2], NULL);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement