title: linux C sending data from child to parent is fine, but fail to send data from child to parent
body: <p>I would like to fork a child and connect pipes to the stdin and stdout of child. And then run the exec ./xx. After that I send 17 from parent to child and child print it. all good so far. but when I send a 17 return to parent it doesnt work.
  The result is : output nothing and look like wait for some input.
  if I remove the code "fscanf(b, "%d", &amp;x); " in parent, the output is： 
  from C 0  from p 17
  I pretty confuse why I get the odd result?? Thank you</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdbool.h&gt;
  
  int main(int argc, char** argv) {
      int fds[2], cp[2], x = 0;
      pipe(fds); 
      pipe(cp);
      int pid=fork();
  
  // c
  if (pid==0) {               
      close(fds[1]);
      close(cp[0]);
      dup2(fds[0],0);
      dup2(cp[1], 1);
      close(cp[1]);
      close(fds[0]);      
      execlp("./xx", "xx", 0);            
  }
  // p
  if (pid) {      
  
      close(fds[0]);
      close(cp[1]);
      dup2(fds[1],1);
      close(fds[1]);
      FILE* a=fdopen(1, "w");
      FILE* b=fdopen(cp[0], "r");
      fprintf(a, "17");       
      fscanf(b, "%d", &amp;x); 
      fprintf(stderr, "from C %d", x);
      }
  
      return 0;
  }
  </code></pre>
  
  <p>XX </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdbool.h&gt;
  int main() {
      int y = 0;
      FILE* r=fdopen(STDIN_FILENO, "r");
      fscanf(r, "%d", &amp;y);
      fprintf(stderr, "from p %d \n ", y);
      FILE* w=fdopen(STDOUT_FILENO, "w");
      fprintf(w, "17");
      return 0;
  }
  </code></pre>