Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/wait.h>
- int main(){
- int fd[2], pid1, pid2;
- char *my_program1[3] = {“/bin/ls”, “-l”, NULL}; #directory listing
- char *my_program2[2] = {“/usr/bin/wc”, NULL}; #line, word and character count
- pipe(fd);
- if ((pid1=fork())==0){
- close(1); # close the stdout
- dup(fd[1]); # duplicate fd[1] to position 1
- close(fd[0]);
- close(fd[1]);
- execv(my_program1[0],my_program1); # the output of the program redirected to fd[1]
- }
- if ((pid2=fork())==0){
- close(0); #close the stdin
- dup(fd[0]); #duplicate the fd[0] to position 0
- close(fd[0]);
- close(fd[1]);
- execv(my_program2[0],my_program2); # the input of the program redirected to fd[1]
- }
- close(fd[0]);
- close(fd[1]);
- waitpid(pid1, NULL, 0);
- waitpid(pid2, NULL, 0);
- return 0;
- }
Add Comment
Please, Sign In to add comment