title: Using fifo open in non-blocking mode with select
body: <p>I have two processes A and B. The communication flow is always A -> B, but I need to do it using a named pipe, because I must use the pipe file descriptor in a select call inside the B process, and the data written to the pipe must persist when any or both of the processes exit.</p>
  
  <p>The pipe is opened in non-blocking mode on both ends. In process A:</p>
  
  <pre><code>int push_fifo_fd = open(FIFO_NAME, O_WRONLY | O_NONBLOCK | O_CREAT, 0644);
  </code></pre>
  
  <p>In process B:</p>
  
  <pre><code>int fd = open(FIFO_NAME, O_RDONLY | O_NONBLOCK | O_CREAT, 0644);
  </code></pre>
  
  <p>Q1.
  The process B uses curl multi interface, so I get the fd_sets of the curl multi handle and add the "fd" descriptor to the read fd_set, than make a call to select, to get the file descriptors available for reads and writes. In every call to select, "fd" is contained in the result read fd_set, but read returns 0, even if the write end is opened. This causes the  process B to use 100% of processor time. I mention that I don't know to order in which the ends of the pipe are opened. The relevant code from B:</p>
  
  <pre><code>while (1)
  {
      fd_set read_fds, write_fds, err_fds;
  
      FD_ZERO(&amp;read_fds);
      FD_ZERO(&amp;write_fds);
      FD_ZERO(&amp;err_fds);
  
      FD_SET(fifo_fd, &amp;read_fds);
      // some code
      ccode = curl_multi_fdset(curlm, &amp;read_fds, &amp;write_fds, &amp;err_fds, &amp;max_fd);
      max_fd = MAX(max_fd, fifo_fd);
  
      rc = select(max_fd + 1, &amp;read_fds, &amp;write_fds, &amp;err_fds, &amp;timeout);
      switch (rc)
      {
          case -1:
              WARN("select");
              continue;
  
          case 0:
          default:
              {
                  if (FD_ISSET(fifo_fd, &amp;read_fds))
                  {
                      // read from the fifo_fd
                  }
  
                  /* Now look at the handles that need attention */
                  int old_running_handles = running_handles;
  
                  ccode = curl_multi_perform(curlm, &amp;running_handles);
                  if (ccode != CURLM_OK &amp;&amp; ccode != CURLM_CALL_MULTI_PERFORM)
                  {
                      WARN("curl_multi_perform error: %s", curl_multi_strerror(ccode));
                      continue;
                  }
  
                  if (running_handles != old_running_handles)
                  {
                      CURLMsg *curl_msg;
                      int left_msgs = 0;
                      while ((curl_msg = curl_multi_info_read(curlm, &amp;left_msgs)) != NULL)
                      {
                          // treat each easy handle
                      }
                  }
              }
              break;
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Q2. In "man 7 fifo" is said "A process can open a FIFO in nonblocking mode.  In this case, opening for read-only will succeed even if no-one has opened on the  write side yet, opening for write-only will fail with ENXIO (no such device or address) unless the other end has already been opened." but the process A always can open successfully the write end of the pipe in non-blocking mode even the read end is not opened. Why is that? The platform on which I test is Ubuntu server 12.04.3, kernel 3.8.0-29.</p>