Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdlib.h>
- #include <wait.h>
- #include <fcntl.h>
- int main(int argc, char *argv[])
- {
- int fd[2];
- int st;
- pipe(fd);
- pid_t pid = fork();
- if (pid == -1) {
- exit(1);
- } else if (pid == 0) {
- close(fd[0]);
- dup2(fd[1], 1);
- close(fd[1]);
- execlp(argv[1], argv[1], NULL);
- _exit(1);
- }
- close(fd[1]);
- int fd1[2];
- pipe(fd1);
- pid_t pid1 = fork();
- if (pid1 == -1) {
- exit(1);
- } else if (pid1 == 0) {
- dup2(fd[0], 0);
- dup2(fd1[1], 1);
- close(fd[0]);
- close(fd1[1]);
- close(fd1[0]);
- if (fork() == 0) {
- int f = open(argv[5], O_CREAT | O_TRUNC | O_WRONLY, 0600);
- dup2(f, 2);
- close(f);
- execlp(argv[2], argv[2], NULL);
- _exit(1);
- } else {
- wait(&st);
- if (WIFEXITED(st) && !WEXITSTATUS(st)) {
- if (fork() == 0) {
- execlp(argv[3], argv[3], NULL);
- _exit(1);
- } else {
- wait(0);
- }
- }
- }
- exit(0);
- }
- close(fd[0]);
- close(fd1[1]);
- pid_t pid2 = fork();
- if (pid2 == -1) {
- exit(1);
- } else if (pid2 == 0) {
- dup2(fd1[0], 0);
- close(fd1[0]);
- execlp(argv[4], argv[4], NULL);
- _exit(1);
- }
- close(fd1[0]);
- while (wait(0) != -1);
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement