title: In C, how does the read function "know" when there is nothing left to read?
body: <p>In the below program (which is not mine, but one which I have modified), the child process makes two writes to the pipe. When I run the program, I get the following output:</p>
  
  <pre><code>Received string: Hello, world!
  This is the child process.
  </code></pre>
  
  <p>How is it that the <em>read</em> performed by the parent process captures both of these strings from the pipe buffer? What (if anything) prevents the parent process from assuming after it has read the first string (or the first char of the first string for that matter) that there is nothing else to read from the buffer, and exiting?</p>
  
  <p>Program in question:</p>
  
  <pre><code>int main(void)
  {
      int     fd[2], nbytes;
      pid_t   childpid;
      char    string[] = "Hello, world!\n";
      char    string2[] = "This is the child process.\n";
      char    readbuffer[80];
  
      pipe(fd);
  
      if((childpid = fork()) == -1)
      {
              perror("fork");
              return 1;
      }
  
      if(childpid == 0)
      {
              /* Child process closes pipe's input file descriptor */
              close(fd[0]);
  
              /* Send "string" through the output side of pipe */
              write(fd[1], string, (strlen(string)));
              write(fd[1], string2, (strlen(string2)+1));
              return 1;
      }
      else
      {
              /* Parent process closes pipe's output file descriptor */
              close(fd[1]);
  
              /* Read in a string from the pipe */
              nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
              printf("Received string: %s", readbuffer);
      }
  
      return 0;
  }
  </code></pre>