Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <sys/stat.h>
  6. int main() {
  7.    int pipefd[2], child_pid, grand_child;
  8.  
  9.    pipe(pipefd);
  10.  
  11.    child_pid = fork();
  12.  
  13.    if (child_pid) {
  14.       waitpid(child_pid, NULL, 0);
  15.       /* Parent */
  16.  
  17.       grand_child = fork();
  18.  
  19.       if (!grand_child) {
  20.          dup2(pipefd[0], STDIN_FILENO);
  21.          close(pipefd[0]);
  22.          close(pipefd[1]);
  23.          execlp("cat", "cat", NULL);
  24.       } else {
  25.          waitpid(grand_child, NULL, 0);
  26.       }
  27.  
  28.    } else {
  29.       /* Child */
  30.  
  31.       dup2(pipefd[1], STDOUT_FILENO);
  32.  
  33.       close(pipefd[1]);
  34.       close(pipefd[0]);
  35.  
  36.  
  37.       execlp("ls", "ls", NULL);
  38.    }
  39.  
  40.    return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement