阅读丧偶管道挂起

Reading a widowed pipe hangs

根据pipe (2)

A pipe whose read or write end has been closed is considered widowed. Writing on such a pipe causes the writing process to receive a SIGPIPE signal. Widowing a pipe is the only way to deliver end-of-file to a reader: after the reader consumes any buffered data, reading a widowed pipe returns a zero count.

据我了解,从关闭的管道读取数据会导致 reader 到 return 读取 0 个字节。然而,在下面的测试程序中,read 调用阻塞,程序挂起。我觉得我一定是在 man 页面上遗漏了什么;为什么这个程序会阻塞?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void child(int pipe) {
        close(pipe);
}

void parent(int pipe) {
        char b;
        ssize_t nread;

        printf("about to read\n");

        nread = read(pipe, &b, 1);

        printf("nread = %zd\n", nread);
}

int main(void) {
        pid_t pid;
        int pipes[2];

        pipe(pipes);

        pid = fork();

        if (pid == 0)
                child(pipes[1]);
        else
                parent(pipes[0]);

        return 0;
}

我需要在管道真正关闭之前关闭父进程中管道的写入端。