Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int pipe1[2], pipe2[2], pipe3[2];
- pid_t pid;
- char string1[1000], string2[1000];
- if (pipe(pipe1) == -1)
- {
- perror("demo1");
- exit(1);
- }
- if ((pid = fork()) == -1)
- {
- perror("demo2");
- exit(1);
- }
- else if (pid == 0)
- {
- close(pipe1[0]); // close read end of pipe
- dup2(pipe1[1], 1); // make 1 same as write-to end of pipe
- close(pipe1[1]); // close excess fildes
- execvp(cmd1[0], cmd1); // execute
- perror("demo3"); // still around? exec failed
- _exit(1); // no flush
- }
- if (pipe(pipe2) == -1)
- {
- perror("demo4");
- exit(1);
- }
- if ((pid = fork()) == -1)
- {
- perror("demo5");
- exit(1);
- }
- else if (pid == 0)
- {
- close(pipe2[1]);
- dup2(pipe2[0], 0);
- execvp(cmd2[0], cmd2);
- close(pipe2[0]);
- perror("demo6");
- _exit(1);
- }
- if (pipe(pipe3) == -1)
- {
- perror("demo7");
- exit(1);
- }
- if ((pid = fork()) == -1)
- {
- perror("demo8");
- exit(1);
- }
- else if (pid == 0)
- {
- close(pipe3[1]);
- dup2(pipe3[0], 0);
- execvp(cmd3[0], cmd3);
- close(pipe3[0]);
- perror("demo9");
- _exit(1);
- }
- if (pid > 0)
- {
- /*
- Read from pipe 1
- */
- close(pipe1[1]); //close write end of pipe
- dup2(pipe1[0], 0); // make 0 same as read-from end of pipe
- int string1_length = read(pipe1[0], string1, 1000);
- close(pipe1[0]);
- /*
- Write to pipe 2
- */
- dup2(pipe2[1], 1);
- close(pipe2[1]);
- /*
- Write to pipe 3
- */
- dup2(pipe3[1], 1);
- close(pipe3[1]);
- perror("demo10");
- exit(1);
- }
- return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement