#include #include #include #include #include int main(void) { pid_t childpid; int fd[2]; //file descriptor1 if (pipe(fd)==-1) { printf(" pipe failed \n" ); } childpid = fork(); if (childpid==0) { /* ls is the child */ if (dup2(fd[1] , 1) == -1) { perror("Failed to redirect stdout of ls"); } else if ((close(fd[0]) == -1) || (close(fd[1]) == -1)) { perror("failed at closing"); } else { if (execl("/bin/cat", "cat", "sortthis.txt", NULL) == -1) { perror("catting test failed"); } } return 1; } int fd2[2]; pipe(fd2); if (dup2(fd[0], 0) == -1) /* sort is the parent */ perror("Failed to redirect stdin of sort"); else if ((close(fd[0]) == -1) || (close(fd[1]) == -1)) { perror("failed at closing"); } else { //Output (1) of execl should go into fd2[1] not into terminal! if (dup2(1,fd2[1])==-1) { perror("I blame charlie"); } if (execl("/usr/bin/sort", "sort", "-r", NULL) == -1) { perror("could not sort anything"); } } //Now lets pray that somehow the output from sort will go into WC childpid = fork(); if (childpid==0) { // WC // if (dup2(fd2[0] , 0) == -1) { perror("Failed to redirect stdout of sort"); } else if ((close(fd2[0]) == -1) || (close(fd2[1]) == -1)) { perror("failed at closing"); } else { if (execl("/usr/bin/wc", "wc", "-cl", NULL) == -1) { perror("catting sort output failed"); } } return 1; } //Ignore parent for now... return 1; }