Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <errno.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- int main(int argc, char *argv[]){
- // pr1 < f.dat | pr2 | pr3 > f.res
- int pid1, pid2, pid3;
- int fd1[2], fd2[1];
- if (pipe(fd1) == -1) exit(1);
- pipe(fd2);
- if ((pid1 = fork()) == -1){
- perror("fork\n");
- exit(1);
- }else if (pid1 == 0){//son1
- int fd_dat;
- if ((fd_dat = open(argv[2], O_RDONLY)) == -1){
- perror("open data\n");
- exit(1);
- }
- dup2(fd_dat, 0);
- dup2(fd1[1],1);
- close(fd1[1]);
- close(fd1[0]);
- close(fd2[0]);
- close(fd2[1]);
- //close(fd_dat);
- execlp(argv[1], argv[1], NULL);
- perror("execlp\n");
- _exit(1);
- }
- if ((pid2 = fork()) == -1){
- perror("fork\n");
- exit(1);
- }else if (pid2 == 0){//son
- if (dup2(fd1[0],0) == -1){
- perror("dup2\n");
- exit(1);
- }
- dup2(fd2[1],1);
- // close(fd1[0]);
- // close(fd1[1]);
- close(fd2[0]);
- close(fd2[1]);
- execlp(argv[3],argv[3],NULL);
- perror("execlp\n");
- _exit(1);
- }
- if ((pid3 = fork()) == -1){
- perror("fork\n");
- exit(1);
- }else if (pid3 == 0){//son
- int fd_res;
- if ((fd_res = open(argv[5], O_CREAT | O_WRONLY | O_TRUNC, 0777)) == -1){
- perror("open result\n");
- exit(1);
- }
- dup2(fd2[0],0);
- dup2(fd_res, 1);
- printf("2\n");
- close(fd1[0]);
- close(fd1[1]);
- close(fd2[0]);
- close(fd2[1]);
- //close(fd_res);
- execlp(argv[4], argv[4], NULL);
- perror("execlp\n");
- _exit(1);
- }
- close(fd1[1]);
- close(fd1[0]);
- close(fd2[1]);
- close(fd2[0]);
- // close(fd_dat);
- // close(fd_res);
- wait(NULL);
- wait(NULL);
- wait(NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement