title: Linux fifo (named pipe) O_NONBLOCK breaks pipe
body: <p>I've been writing a small program in C++ for linux using inter process communication. I had a problem when trying to read in a non-blocking way with one process and write (blocking) with another process. The issue looks like this, when the parent tries to read the pipe with <code>O_NONBLOCK</code> (or <code>O_NDELAY</code>) it never reads a single byte and when the child tries to write on the pipe, it fails and the <code>SIGPIPE</code> broken pipe signal is sent. Here is an example of the code:</p>
  
  <pre><code>// Parent process
  mkfifo(PROC_COPROC, 0666);
  
  int fd_co = open(PROC_COPROC, O_RDONLY | O_NDELAY);
  char c;
  int n;
  fcntl(fd_co,F_SETFL,0); //fix it
  while ((n = read(fd_co, &amp;c, 1)) &gt; 0)
  {
      printf("%c", c);
  }
  close(fd_co);
  
  // Child process
  int fd = open(PROC_COPROC, O_WRONLY | O_APPEND);
  if ( fd != -1 ) 
  {
      write( fd , "message\n" , 8); //Fails here if flag not set
  }
  else
      printf("Ne peut pas ecrire sur le fifo\n");
  close(fd);
  </code></pre>
  
  <p>I finally found a way to resolve this by using <code>fcntl(fd_co,F_SETFL,0);</code> right after the non-blocking open call.</p>
  
  <p>After reading the <a href="http://linux.die.net/man/2/fcntl" rel="nofollow">man page</a>, I couldn't find any (simple) explanation why I sould reset the file descriptor's flag if I want to read non-blocking and write blocking.</p>
  
  <p>Anyone has an explanation or am I doing it wrong?</p>