Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<unistd.h>
  4. #include<sys/types.h>
  5. #include<string.h>
  6. #include<sys/wait.h>
  7.  
  8. int main(int argc, char const *argv[])
  9. {
  10. int fd[2];
  11.  
  12. if (pipe(fd)==-1) exit(1);
  13.  
  14. int pid = fork();
  15. if(pid == 0) {
  16. close(fd[0]);
  17. dup2(fd[1], 1);
  18. close(fd[1]);
  19. execlp("ls", "ls", (char *) 0);
  20. exit(1);
  21. }
  22. else if (pid > 0) {
  23. int pid2 = fork();
  24. if(pid2 == 0) {
  25. close(fd[1]);
  26. dup2(fd[0], 0);
  27. close(fd[0]);
  28. execlp("wc", "wc", (char *) 0);
  29. exit(1);
  30.  
  31. } else if(pid2 > 0) {
  32. // Close both ends
  33. close(fd[0]);
  34. close(fd[1]);
  35. wait(NULL);
  36. wait(NULL);
  37. } else exit(1);
  38. } else exit(1);
  39.  
  40. return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement