Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main(int argc, char *argv[])
- {
- if (argc!=3) {printf("\nusage: <dir> <filter>\n"); return 1;}
- int fd1[2];
- pipe(fd1);
- int fd2[2];
- pipe(fd2);
- //int stdin = dup(STDIN_FILENO);
- //int stdout = dup(STDOUT_FILENO);
- pid_t pid;
- pid = fork();
- //send ls result via fd1
- if(pid==0) //son
- {
- close(fd1[READ]);
- dup2(fd1[WRITE],STDOUT_FILENO);
- execlp("ls","ls",argv[1],"-laR",NULL);
- exit(1);
- }//end_son, start father
- wait(NULL);
- pid = fork();
- //receive ls from fd1 and send grep vi fd2
- if(pid==0) //son
- {
- close(fd1[WRITE]);
- dup2(fd1[READ],STDIN_FILENO);
- close(fd2[READ]);
- dup2(fd2[WRITE],STDOUT_FILENO);
- execlp("grep","grep",argv[2],NULL);
- exit(1);
- }//end_son, start father
- //wait(NULL);
- pid = fork();
- //receive via fd2
- if(pid==0) //son
- {
- close(fd2[WRITE]);
- dup2(fd2[READ],STDIN_FILENO);
- execlp("sort","sort",NULL);
- exit(1);
- }//end_son, start father
- //wait(NULL);
- //wait(NULL);
- //wait(NULL);
- // close(fd1[WRITE]);
- //close(fd1[READ]);
- //close(fd2[WRITE]);
- //close(fd2[READ]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement