Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <error.h>
- int main(int argc, char **argv)
- {
- int fileDescriptors[2]; //used for the pipe command
- pid_t childProcessID1;
- pid_t childProcessID2;
- int result; //store the return Value
- char buf;
- //Create the interprocess channel
- result = pipe(fileDescriptors);
- if(result < 0)
- {
- //error while creating pipe
- perror("pipe");
- exit(1);
- }
- //Creating the child Process
- childProcessID1 = fork();
- if(childProcessID1 < 0)
- {
- //error while forking
- perror("fork");
- exit(2);
- }
- //Redirect STDOUT and execute first command
- if(childProcessID1 == 0)
- {
- if(dup2(fileDescriptors[1], STDOUT_FILENO) == -1) //Map STDOUT on write end of Communication Channel
- perror("dup2");
- if (close (fileDescriptors [0]) == -1)
- perror ("close");
- execlp(argv[1], argv[1], NULL);
- return 0;
- }
- //Redirect STDOUT and execute first command
- if(dup2(fileDescriptors[0], STDIN_FILENO) == -1) //Map STDIN on read end of Communication Channel to get the data from the first command
- perror("dup2");
- if (close (fileDescriptors [1]) == -1)
- perror ("close");
- execlp(argv[2], argv[2], NULL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement