title: reading and writing to a pipe c issue
body: <p>Here is the program:</p>
  
  <pre><code>  int fd[2];
    char buf1 [31];
    int i;
    char buf2;
  
    pipe(fd);
  
    if (fork() == 0) { // child
      close(fd[1]); // close writing pipe
      for(i = 0; i&lt; 20; i++) { 
          read(fd[0], buf1, 30);
          printf("%s\n", buf1);
  
      }
      close(fd[0]);
    }
  
    else { // parent
      close(fd[0]); // close reading end
      buf2 = 'a';
      for (i = 0; i &lt; 20; i++) {
          write(fd[1], &amp;buf2, sizeof(buf2));
      }
      close(fd[1]);
    }
    wait(NULL);
  </code></pre>
  
  <p>Since read blocks if there is nothing in there, we will first write to the pipe.</p>
  
  <p>My question is regarding the <strong>write</strong> function. The for loop runs 20 times, and each time I am writing an <code>a</code> to the pipe. After 20 iterations, the pipe on write end will contains <code>aaaaaaaaaaaaaaaaaaaa</code></p>
  
  <p>In the child, I read all 20 <code>a's</code> into <code>buf1</code>, and I print the result.</p>
  
  <p><strong>Now is the pipe empty, since we have wrote, and read what we wrote?</strong></p>
  
  <p>Since there is also a for loop here, we read again, but this time, I think the pipe is empty, so theres nothing to read. So I think the final output after 20 iterations of reading is just 
  <code>aaaaaaaaaaaaaaaaaaaa</code></p>
  
  <p>But actually it's this:</p>
  
  <pre><code>aaaaaaaaaaaaaaaaaaaa
  aaaaaaaaaaaaaaaaaaaa
  aaaaaaaaaaaaaaaaaaaa
  aaaaaaaaaaaaaaaaaaaa
  aaaaaaaaaaaaaaaaaaaa
  aaaaaaaaaaaaaaaaaaaa
  aaaaaaaaaaaaaaaaaaaa
  aaaaaaaaaaaaaaaaaaaa
  aaaaaaaaaaaaaaaaaaaa
  aaaaaaaaaaaaaaaaaaaa
  aaaaaaaaaaaaaaaaaaaa
  aaaaaaaaaaaaaaaaaaaa
  aaaaaaaaaaaaaaaaaaaa
  aaaaaaaaaaaaaaaaaaaa
  aaaaaaaaaaaaaaaaaaaa
  aaaaaaaaaaaaaaaaaaaa
  aaaaaaaaaaaaaaaaaaaa
  aaaaaaaaaaaaaaaaaaaa
  aaaaaaaaaaaaaaaaaaaa
  aaaaaaaaaaaaaaaaaaaa
  </code></pre>
  
  <p>Why? Is the pipe not empty after we read?</p>