Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <errno.h>
- int error_pipe(int pn)
- {
- if ( pn < 0) perror("Error in fork()\n");
- return 126;
- }
- int main()
- {
- int fd[2],status;
- pid_t p1,p2;
- char* ch1[]={"ls","-l",NULL};
- char* ch2[]={"grep","abc",NULL};
- pipe(fd);
- if ((p1 = fork())== 0)
- {
- dup2(fd[1],1);
- close(fd[0]);
- close(fd[1]);
- execvp(ch1[0],ch1);
- } // end if
- else error_pipe(p1);
- if ((p2 = fork()) == 0)
- {
- dup2(fd[0], 0);
- close(fd[0]);
- close(fd[1]);
- execvp(ch2[0],ch2);
- }// end if
- else error_pipe(p2);
- close(fd[0]);
- close(fd[1]);
- waitpid(p1,NULL ,0);
- waitpid(p2, &status,0);
- if (status != 0)
- {
- printf ("No such file ...\n");
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment