Guest User

Untitled

a guest
Feb 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/wait.h>
  4.  
  5. int main(){
  6.   int fd[2], pid1, pid2;  
  7.   char *my_program1[3] = {/bin/ls”,-l”, NULL};   #directory listing
  8.   char *my_program2[2] = {/usr/bin/wc”, NULL};     #line, word and character count
  9.  
  10.   pipe(fd);
  11.   if ((pid1=fork())==0){           
  12.     close(1);   # close the stdout
  13.     dup(fd[1]);     # duplicate fd[1] to position 1
  14.     close(fd[0]);  
  15.     close(fd[1]);
  16.     execv(my_program1[0],my_program1); # the output of the program redirected to fd[1]
  17.   }
  18. if ((pid2=fork())==0){         
  19.     close(0);    #close the stdin
  20.     dup(fd[0]);  #duplicate the fd[0] to position 0
  21.     close(fd[0]);
  22.     close(fd[1]);
  23.     execv(my_program2[0],my_program2); # the input of the program redirected to fd[1]
  24.   }
  25.  
  26.  close(fd[0]);
  27.  close(fd[1]);
  28.  waitpid(pid1, NULL, 0);
  29.  waitpid(pid2, NULL, 0);
  30.  return 0;
  31. }
Add Comment
Please, Sign In to add comment