title: Can I use pipe as read at parent and write in child?
body: <p>I'm now learning how to use pipes correctly. I found examples only for write in parent and read in child, but I want to know how can I do it reverse. I tried like this:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;sys/types.h&gt;
  
  int main(){
  
    int  p1[2], p2[2];
    char original[]="This is a string\n ";
  
    pid_t child2;
    pid_t child=fork();
  
    if (child&gt;0)
    {
      child2 = fork();
      if(child2&gt;0){
        wait();
        printf("I'm the parrent.\n");
  
        close(p1[1]);
        close(p2[1]);
  
        printf("Parrent read p1:\n");
        if (read(p1[0], original, sizeof(original)) == -1)
          perror("read() error in parent p1");
        else printf("parent read '%s' from pipe 1\n", original);
  
        printf("Parrent read p2:\n");
        if (read(p2[0], original, sizeof(original)) == -1)
          perror("read() error in parent p2");
        else printf("parent read '%s' from pipe 2\n", original);
      }
      else{
        printf("Child2 \n");
        pipe(p2);
        close(p2[0]);
        if (write(p2[1], original, sizeof(original)+1) == -1)
          perror("write() error in child2");
        //close(p2[1]);
      }
    }
    else
    {
      printf("Child1 \n");
  
      pipe(p1);
      close(p1[0]);
      if (write(p1[1], original, sizeof(original)+1) == -1)
        perror("write() error in child1");
      //close(p1[1]);
    }
    return 0;
  }
  </code></pre>
  
  <p>But this way give to me error at reading in parent. <code>read() error in parent p1: Bad file descriptor</code> both times, at <code>p1</code> and also at <code>p2</code>. So, can I do this this way or not? Or this is just something trivial error?</p>