title: Why is my Fork child process not reading from pipe?
body: <p>I'm trying to create a separate "logger" process using fork(), which will log messages to a file when it receives them. However i'm having an issue, the child process seems to "give up" after one string.</p>
  
  <p>I'm not even sure if this is a great way to go about having a logger process - but here is the output and code</p>
  
  <p>Any ideas or help will be greatly appreciated - thanks</p>
  
  <p><a href="https://i.stack.imgur.com/83N93.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/83N93.png" alt="https://i.imgur.com/Mg7Jkzi.png"></a></p>
  
  <pre><code>int main()
      {   
          int pipefd[2], nbytes;
          pid_t pid;
          char szLogtest[] = "Log Event: Blah blah blah\n";
          char szLogtest2[] = "Log Event: Blah blah 2\n";
          char readbuffer[512];
  
          pipe(pipefd);
  
          if ((pid = fork()) == -1)
          {
              fprintf(stderr, "Fork error!\n");
              exit(1);
          }
          else if (pid == 0)
          {
              printf("I am child!\n");
              close(pipefd[1]); // close the write end of pipe
  
              while(1)
              {
                  nbytes = read(pipefd[0], readbuffer, sizeof(readbuffer));
                  printf("Nbytes is %d\n", nbytes);
                  if (nbytes)
                  {
                      printf("Received string: %s", readbuffer);
                  }
  
                  nbytes = 0;
              }
  
          }
          else
          {
              printf("I am parent!\n");
              close(pipefd[0]); //close read end of pipe
  
              write(pipefd[1], szLogtest, (strlen(szLogtest)+1));
              write(pipefd[1], szLogtest2, (strlen(szLogtest2)+1));
          }
  
          return 0;
      }
  </code></pre>