poll returns 0 但读取不阻塞

poll returns 0 but read does not block

我已经在这段代码上工作了一段时间,并且出于某种原因轮询 returns 零,即使有数据要从 outFds 管道中读取。出于某种原因,如果我读入一些数据然后 运行 轮询它 returns 正确的值,但这不是解决方案。有人以前看过这个并且知道我应该怎么做吗?

#include <stdio.h>
#include <stdlib.h>
#include <sys/poll.h>

int main(void)
{
    int outFds[2];

    pipe(outFds);

    if(!fork()) {
        dup2(outFds[1], 1);

        close(outFds[0]);
        close(outFds[1]);

        // disable printf buffering
        setvbuf(stdout, NULL, _IONBF, 0);

        sleep(1);
        char buf[32];
        printf("blah");

        exit(0);
    }

    close(outFds[1]);

    char c;
    // Read 'b' into c. If this next line is not commented poll returns 1
    //read(outFds[0], &c, 1); 

    struct pollfd outFd;
    outFd.fd = outFds[0];
    outFd.events = POLLIN;
    printf("%d\n", poll(&outFd, 1, 0)); // poll returns 0 for some reason
}

来自联机帮助页:

Specifying a timeout of zero causes poll() to return immediately, even if no file descriptors are ready.

我会将其解读为 tv_sectv_nsec 设置为 0 的 timespec 结构,但似乎将 nullptr 放入其中具有相同的效果。