Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.16 KB | None | 0 0
  1. int main(int argc, char *argv[])
  2. {
  3.   if (argc!=3) {printf("\nusage: <dir> <filter>\n"); return 1;}
  4.  
  5.   int fd1[2];
  6.   pipe(fd1);
  7.   int fd2[2];
  8.   pipe(fd2);
  9.   //int stdin = dup(STDIN_FILENO);
  10.   //int stdout = dup(STDOUT_FILENO);
  11.  
  12.   pid_t pid;
  13.  
  14.   pid = fork();
  15.   //send ls result via fd1
  16.   if(pid==0) //son
  17.   {
  18.     close(fd1[READ]);
  19.     dup2(fd1[WRITE],STDOUT_FILENO);
  20.     execlp("ls","ls",argv[1],"-laR",NULL);
  21.     exit(1);
  22.   }//end_son, start father
  23.  
  24.   wait(NULL);
  25.  
  26.   pid = fork();
  27.    
  28.   //receive ls from fd1 and send grep vi fd2
  29.   if(pid==0) //son
  30.   {
  31.      close(fd1[WRITE]);
  32.     dup2(fd1[READ],STDIN_FILENO);
  33.     close(fd2[READ]);
  34.     dup2(fd2[WRITE],STDOUT_FILENO);
  35.      execlp("grep","grep",argv[2],NULL);
  36.      exit(1);
  37.   }//end_son, start father
  38.  
  39.   //wait(NULL);
  40.  
  41.   pid = fork();
  42.   //receive via fd2
  43.   if(pid==0) //son
  44.   {
  45.     close(fd2[WRITE]);
  46.     dup2(fd2[READ],STDIN_FILENO);
  47.     execlp("sort","sort",NULL);
  48.     exit(1);
  49.   }//end_son, start father
  50.  
  51.   //wait(NULL);
  52.   //wait(NULL);
  53.   //wait(NULL);
  54.  
  55.   //  close(fd1[WRITE]);
  56.   //close(fd1[READ]);
  57.   //close(fd2[WRITE]);
  58.   //close(fd2[READ]);
  59.  
  60.   return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement