title: write operation on pipe is always failing
body: <p>I'm a bit new to pipes and concurrency, and have been frustrated with this problem for hours. I am struggling to understand why this <code>write</code> operation is constantly failing on my pipe. I am trying to have the child process write data through a pipe that will be received by the parent process. My current code is this:</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #define MAXSIZE 4096
  
  int main() {
      pid_t status;
      int fd[2]; //The array of file descriptors
  
      if (pipe(fd) == -1) {
          printf("Error piping");
      }
  
      status = fork(); //Begin the fork process
      switch (status) {
          case -1:
              perror("Error forking");
              break;
          case 0:
              //Child process
              close(fd[0]); //Only send data
              char some_string[15] = "hi there";
              if (write(fd[1], some_string, MAXSIZE) == -1) {
                  printf("Error writing to the pipe");
              }
              close(fd[1]); //Close write end
              exit(1);
          default:
              close(fd[1]); //Only receive data
              char readed[500] = "";
              while(read(fd[0], readed, MAXSIZE) != 0) {
                  printf("read this %s\n", readed);
              }
              printf("Done reading");
              close(fd[0]);
              break;
      }
      return 1;
  }
  </code></pre>
  
  <p>However, I constantly get the message "Error writing to pipe", meaning that the <code>write</code> operation has failed in the child process. Another interesting thing is that if I change <code>some_string</code> to a string literal instead, this code works fine with the exception that it never terminates and instead, the <code>read</code> operation in the parent process reads from <code>STDIN</code>! I don't understand why this could be happening, is it possible that we have a zombie child when parent executes so the pipe is "dead"? Or perhaps that the parent process terminates and we have an orphaned child? How can I avoid this and how does this explain the weird behaviour from the string literal instead? Any insights?</p>