title: C Piping Child and Parent using dup2
body: <p>I'm practicing piping two processes together. I want to have both processes communicating with each other using two pipes. Here's what I have so far:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(){
  
    //setting up two pipes
    int pipe1[2];
    int pipe2[2];
    pipe(pipe1);
    pipe(pipe2);
  
    //setting up child and parent
    pid_t pid = fork();
    if(pid &lt; 0){
      perror("fork failed\n");
      exit(-1);
    }
    if(pid == 0){
      printf("child\n");
      dup2(pipe1[1], STDOUT_FILENO);
      close(pipe1[1]);
      close(pipe1[0]);
      dup2(pipe2[0], STDIN_FILENO);
      close(pipe2[0]);
      close(pipe2[1]);
    }
    if(pid &gt; 0){
      wait(NULL);
      printf("parent\n");
      dup2(pipe1[0], STDIN_FILENO);
      close(pipe1[0]);
      close(pipe2[1]);
      dup2(pipe2[1], STDOUT_FILENO);
      close(pipe2[0]);
      close(pipe2[1]);
    }
  
    return 0;
  }
  </code></pre>
  
  <p>I would like both processes communicate with each other by having the input of one process be the output of the other. Have I set this up correctly? If so, where could I go from here if for example I would like the parent to send integers to child and the child were to perform operations on them, and then send them back to the parent for printing.</p>
  
  <p>Thanks.</p>