title: UNIX FIFO: the process hangs if I don't close the input side of the fifo
body: <p>I've just started working with UNIX FIFOs, and I discovered something while experimenting with my first FIFO program. The program works this way: after creating the FIFO, two processes are started using the <code>fork()</code> function. The child process reads what the father passes to him through the FIFO, and prints it on the screen. The data exchanged is the string specified as an argument. The question is: in the father section, if I forget to close the input side of the FIFO (meaning that I exclude the <code>close(fd)</code> line) the program would just hang, even if the data between the processes is exchanged correctly. Otherwise, everything works fine and the program terminates withouth hanging. Can someone please explain me why?</p>
  
  <p>Thanks for your patience. Here is the code of the main function:</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
      if(argc != 2)
      {
          printf("An argument must be specified\n");
          return -1;
      }   
  
      int ret = mkfifo("./fifo.txt", 0644);
      char buf;
  
      if(ret &lt; 0)
      {
          perror("Error creating FIFO");
          return -1;
      }
  
      pid_t pid = fork();
  
      if(pid &lt; 0)
      {
          perror("Error creating child process");
          return -1;
      }
  
      if(pid == 0) /* child */
      {
          int fd = open("./fifo.txt", O_RDONLY); /* opens the fifo in reading mode */
  
          while(read(fd, &amp;buf, 1) &gt; 0)
          {
              write(STDOUT_FILENO, &amp;buf, 1);
          }
          write(STDOUT_FILENO, "\n", 1);
          close(fd);
          return 0;
      }
      else /* father */
      {
          int fd = open("./fifo.txt", O_WRONLY); /* opens the fifo in writing mode */
  
          write(fd, argv[1], strlen(argv[1]));
          close(fd);
          waitpid(pid, NULL, 0);
          return 0;
      }
  }
  </code></pre>