Advertisement
Guest User

Untitled

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