pselect 通知两个管道读端文件描述符,即使只有一个写端被写入

pselect notifies both pipe read end file descriptors even though only one write end was written on

我无法理解 pselect 的行为。基本上我所做的是以下内容:

我希望以下代码的输出是:

Pipe1 is set!

但是,我得到的是:

Pipe1 is set!
Pipe2 is set!

当我只在一个管道写入端写入时,为什么两个管道读取端文件描述符都设置了?此行为是正常 pselect 虚假文件描述符通知的一部分吗?我究竟做错了什么?

程序如下:

    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include<iostream>

    enum PipeEnd {
        READ_END = 0, WRITE_END = 1, MAX_END
    };

    int pipe1[MAX_END], pipe2[MAX_END];

    void handle_sigchld(const int signal) {
        //In the SIGCHLD handler write the process ID on pipe1
        int returnStatus;
        int childPID = waitpid(static_cast<pid_t>(-1), &returnStatus, WNOHANG);
        write(pipe1[WRITE_END], &childPID, sizeof(childPID));
    }

    void createPipe(int newPipe[MAX_END]) {
        pipe(newPipe);
        fcntl(newPipe[READ_END], F_SETFL, O_NONBLOCK);
        fcntl(newPipe[WRITE_END], F_SETFL, O_NONBLOCK);
    }

    int main(int argc, const char** argv) {
        //Add a handler for the SIGCHLD signal
        struct sigaction sa;
        sa.sa_handler = &handle_sigchld;
        sigemptyset(&sa.sa_mask);
        sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
        sigaction(SIGCHLD, &sa, nullptr);

        //Create two pipes
        createPipe(pipe1);
        createPipe(pipe2);

        //Create a child process
        if (0 == fork()) {
            sleep(5);
            exit(0);
        }

        fd_set read_fds;
        FD_ZERO(&read_fds);
        FD_SET(pipe1[READ_END], &read_fds);
        FD_SET(pipe2[READ_END], &read_fds);
        int maxfd = std::max(pipe1[READ_END], pipe2[READ_END]);
        //Wait for a file descriptor to be notified   
        pselect(maxfd + 1, &read_fds, nullptr, nullptr, nullptr, nullptr);

        //Check if the read ends of the two pipes are set/notified
        if (FD_ISSET(pipe1[READ_END], &read_fds))
            std::cout << "Pipe1 is set!" << std::endl;
        if (FD_ISSET(pipe2[READ_END], &read_fds))
            std::cout << "Pipe2 is set!" << std::endl;
        return 0;
    }

即使信号处理程序没有 write 任何东西,程序也会表现出相同的行为,这会让您感到惊讶。

原因是pselect失败了。引用 man 7 signal,

The following interfaces are never restarted after being interrupted by a signal handler, regardless of the use of SA_RESTART; they always fail with the error EINTR when interrupted by a signal handler:

....

  • File descriptor multiplexing interfaces: epoll_wait(2), epoll_pwait(2), poll(2), ppoll(2), select(2), and pselect(2).

始终测试系统调用returns。