Search code examples
linuxpipeposixipcmanual

What if an fd is being closed while other function is blocking on it?


According to the manual page of pipe:

If a process attempts to read from an empty pipe, then read(2) will
block until data is available.  If a process attempts to write to a
full pipe (see below), then write(2) blocks until sufficient data has
been read from the pipe to allow the write to complete.  Nonblocking
I/O is possible by using the fcntl(2) F_SETFL operation to enable the
O_NONBLOCK open file status flag.

I have such a question:

Provided that a pipe buffer is empty;

A read is blocking on the pipe;

Now, if the write end is being closed, will the read be unblocked automatically?


Solution

  • Yes, the read will be unblocked and return EOF (0).