title: Detecting pipe hangup
body: <p>How can I get to know, in the reader process, if the write end of pipe is still active (writer process didn't close its descriptor or exit). I mean Posix (C language) interface. As it seems, if the reader process has is descriptor NONBLOCK, it cannot be guessed by the effect of &quot;read&quot; call. I tried checking the desciptor flags but they don't change:</p>
  <p>Writer:</p>
  <pre><code>#include &lt;unistd.h&gt;
  
  int main() {
      sleep(2);
      //actually doesn't write anything but has
      //stdout open and exits before reader does
  }
  </code></pre>
  <p>Reader:</p>
  <pre><code>#include &lt;fcntl.h&gt;
  #include &lt;cstdio&gt;
  #include &lt;unistd.h&gt;
  
  
  int main() {
      
      int i = fcntl(0, F_GETFL);
      int j = fcntl(0, F_GETFD);
      //now the write end is alive
      printf(&quot;i: %d, j: %d\n&quot;, i, j);
      sleep(3);
      i = fcntl(0, F_GETFL);
      j = fcntl(0, F_GETFD);
      //now the write end has exited
      printf(&quot;i: %d, j: %d\n&quot;, i, j);
      //all flags are all the time 0
  }
  </code></pre>
  <p>Supposing that these programs were connected into a pipeline (using &quot;pipe&quot; call) by some external program.</p>