Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<sys/types.h>
- #include<string.h>
- #include<sys/wait.h>
- int main(int argc, char const *argv[])
- {
- int fd[2];
- if (pipe(fd)==-1) exit(1);
- int pid = fork();
- if(pid == 0) {
- close(fd[0]);
- dup2(fd[1], 1);
- close(fd[1]);
- execlp("ls", "ls", (char *) 0);
- exit(1);
- }
- else if (pid > 0) {
- int pid2 = fork();
- if(pid2 == 0) {
- close(fd[1]);
- dup2(fd[0], 0);
- close(fd[0]);
- execlp("wc", "wc", (char *) 0);
- exit(1);
- } else if(pid2 > 0) {
- // Close both ends
- close(fd[0]);
- close(fd[1]);
- wait(NULL);
- wait(NULL);
- } else exit(1);
- } else exit(1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement