Guest User

Untitled

a guest
Jun 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6. #include <errno.h>
  7.  
  8. int error_pipe(int pn)
  9.  
  10. {
  11. if ( pn < 0) perror("Error in fork()\n");
  12. return 126;
  13. }
  14.  
  15. int main()
  16. {
  17. int fd[2],status;
  18. pid_t p1,p2;
  19. char* ch1[]={"ls","-l",NULL};
  20. char* ch2[]={"grep","abc",NULL};
  21. pipe(fd);
  22. if ((p1 = fork())== 0)
  23. {
  24. dup2(fd[1],1);
  25. close(fd[0]);
  26. close(fd[1]);
  27. execvp(ch1[0],ch1);
  28.  
  29. } // end if
  30.  
  31. else error_pipe(p1);
  32.  
  33. if ((p2 = fork()) == 0)
  34. {
  35. dup2(fd[0], 0);
  36. close(fd[0]);
  37. close(fd[1]);
  38. execvp(ch2[0],ch2);
  39. }// end if
  40.  
  41. else error_pipe(p2);
  42.  
  43. close(fd[0]);
  44. close(fd[1]);
  45.  
  46. waitpid(p1,NULL ,0);
  47. waitpid(p2, &status,0);
  48.  
  49. if (status != 0)
  50. {
  51. printf ("No such file ...\n");
  52. }
  53. return 0;
  54. }
Add Comment
Please, Sign In to add comment