Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <wait.h>
  4. #include <fcntl.h>
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. int fd[2];
  9. int st;
  10. pipe(fd);
  11. pid_t pid = fork();
  12. if (pid == -1) {
  13. exit(1);
  14. } else if (pid == 0) {
  15. close(fd[0]);
  16. dup2(fd[1], 1);
  17. close(fd[1]);
  18. execlp(argv[1], argv[1], NULL);
  19. _exit(1);
  20. }
  21. close(fd[1]);
  22. int fd1[2];
  23. pipe(fd1);
  24. pid_t pid1 = fork();
  25. if (pid1 == -1) {
  26. exit(1);
  27. } else if (pid1 == 0) {
  28. dup2(fd[0], 0);
  29. dup2(fd1[1], 1);
  30. close(fd[0]);
  31. close(fd1[1]);
  32. close(fd1[0]);
  33. if (fork() == 0) {
  34. int f = open(argv[5], O_CREAT | O_TRUNC | O_WRONLY, 0600);
  35. dup2(f, 2);
  36. close(f);
  37. execlp(argv[2], argv[2], NULL);
  38. _exit(1);
  39. } else {
  40. wait(&st);
  41. if (WIFEXITED(st) && !WEXITSTATUS(st)) {
  42. if (fork() == 0) {
  43. execlp(argv[3], argv[3], NULL);
  44. _exit(1);
  45. } else {
  46. wait(0);
  47. }
  48. }
  49. }
  50. exit(0);
  51. }
  52. close(fd[0]);
  53. close(fd1[1]);
  54. pid_t pid2 = fork();
  55. if (pid2 == -1) {
  56. exit(1);
  57. } else if (pid2 == 0) {
  58. dup2(fd1[0], 0);
  59. close(fd1[0]);
  60. execlp(argv[4], argv[4], NULL);
  61. _exit(1);
  62. }
  63. close(fd1[0]);
  64. while (wait(0) != -1);
  65. exit(0);
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement