title: A puzzle between child and parent process over pipe
body: <p>Currently I'm leaning how to use pipe between child process and parent process, simply parent process writes to a pipe and child would read from pipe</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #define READEND 0
  #define WRITEEND 1
  int
  main(int argc, char *argv[]){
    int fd[2];
    pipe(fd);
    if (fork()){
       close(fd[READEND]);
       int write_number;
       for (int i = 2; i &lt; 36; i++){
           write_number = i;
           write(fd[WRITEEND], &amp;write_number, sizeof(write_number));
       }
       close(fd[WRITEEND]);
       wait(0);
    }else{// child process
       close(fd[WRITEEND]);
       int read_number[34];
       int len;
       int prime;
       int read_pipe = fd[READEND];
       while ((len = read(read_pipe, &amp;read_number, sizeof(read_number))) &gt; 0){
           printf(&quot;len : %d, %d\n&quot;, len / 4, getpid());
       }
    }
    exit(0);
  }
  
  </code></pre>
  <p>When I run multiple times, something quite unusual happened, but I have no idea, here is the output:</p>
  <pre><code>~# ./a.out
  len : 34, 30375
  ~# ./a.out
  len : 34, 30383
  ~# ./a.out
  len : 1, 30397
  len : 1, 30397
  len : 1, 30397
  len : 31, 30397
  ~# ./a.out
  len : 34, 30411
  ~# cc pipe_pipe.c
  ~# ./a.out
  len : 33, 30454
  len : 1, 30454
  </code></pre>
  <p>It seems like the child goes to the next iteration in the while loop, so that's why not only one line output, but I have closed all the write end of the pipe, it should know that I will just read once, but some runs do not output 34...
  So can anyone analyze that why it is this? thx</p>