如果一个 fd 正在关闭而其他函数正在阻塞它怎么办?

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

根据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.

我有这样一个问题:

前提是管道缓冲区为空;

读取在管道上阻塞;

现在关闭写端,读端会自动解封吗?

是的,read 将被解锁并且 return EOF (0)。