title: Cannot reopen FIFO when the same process uses both ends of the FIFO
body: <p>When using a FIFO on a single process, it looks like after both ends have been opened and one is then closed, it is not possible to reuse the FIFO. Any attempt to reopen the closed end fails or the returned file descriptor is useless.</p>
  
  <p>Is it possible to work around this behavior, or do we have to keep both ends of the FIFO open until we are absolutely sure we don't need it anymore? </p>
  
  <p>Here is some test code that shows and attempt to reopen a closed write end of a FIFO:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdio&gt;
  #include &lt;errno.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;limits.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  
  using namespace std;
  
  int main(int argc, const char **argv)
  {
        cout &lt;&lt; "Creating an instance of a named pipe..." &lt;&lt; endl;
        mode_t prevMask = umask(0);
        if (mknod("my_pipe", S_IFIFO | 0666, 0))
           return -1;
        umask(prevMask);
  
        cout &lt;&lt; "Opening Read end..." &lt;&lt; endl;
        int fdM = open("my_pipe", O_RDONLY | O_NONBLOCK);
        if (fdM == -1)
           return -1;
  
        cout &lt;&lt; "Opening Write end..." &lt;&lt; endl;
        int fdS = open("my_pipe", O_WRONLY | O_NONBLOCK);
        if (fdS == -1)
           return -1;
  
        cout &lt;&lt; "Sending data to pipe..." &lt;&lt; endl;
        const char *data = "Hello my friend!";
        ssize_t NbOfBytesWritten = write(fdS, data, strlen(data));
        if (NbOfBytesWritten &lt; 0)
           return -1;
        cout &lt;&lt; "Number of bytes sent: " &lt;&lt; NbOfBytesWritten &lt;&lt; endl;
  
        cout &lt;&lt; "Closing Write end..." &lt;&lt; endl;
        if (close(fdS))
           return -1;
  
        cout &lt;&lt; "Reopening Write end..." &lt;&lt; endl;
        fdS = open("my_pipe", O_WRONLY | O_NONBLOCK);
        if (fdS == -1)
        {
           cout &lt;&lt; "open() - failed("&lt;&lt; errno &lt;&lt; "): " &lt;&lt; strerror(errno) &lt;&lt; '.';
           remove("my_pipe");
           return -1;
        }
  
        cout &lt;&lt; "Sending some more data to pipe..." &lt;&lt; endl;
        data = "What's up?";
        NbOfBytesWritten = write(fdS, data, strlen(data));
        if (NbOfBytesWritten &lt; 0)
           return -1;
        cout &lt;&lt; "Number of bytes sent: " &lt;&lt; NbOfBytesWritten &lt;&lt; endl;
  
        cout &lt;&lt; "Reading data from pipe..." &lt;&lt; endl;
        char buff[128];
        ssize_t numBytesRead = read(fdM, buff, 127);
        if (NbOfBytesWritten &lt; 0)
           return -1;
        buff[numBytesRead] = '\0'; // null terminate the string
        cout &lt;&lt; "Number of bytes read: " &lt;&lt; numBytesRead &lt;&lt; endl;
        cout &lt;&lt; "Message: " &lt;&lt; buff &lt;&lt; endl;
  
        cout &lt;&lt; "Closing Write end..." &lt;&lt; endl;
        if (close(fdS))
           return -1;
  
        cout &lt;&lt; "Closing Read end..." &lt;&lt; endl;
        if (close(fdM))
           return -1;
  
        cout &lt;&lt; "Deleting pipe..." &lt;&lt; endl;
        if (remove("my_pipe"))
           return -1;
  
     return 0;
  }
  </code></pre>
  
  <p>Here is the output:</p>
  
  <pre><code>Creating an instance of a named pipe...
  Opening Read end...
  Opening Write end...
  Sending data to pipe...
  Number of bytes sent: 16
  Closing Write end...
  Reopening Write end...
  open() - failed(6): No such device or address.
  </code></pre>
  
  <p>I also tested similar code trying to reopen a closed read end (While the write end was kept open). In that case the open() function succeed, but the read() function using the file descriptor returned by open() fails with:
  Communication error on send. (70)</p>
  
  <p>EDIT:</p>
  
  <p>I'm using CYGWIN.</p>