首页 > 解决方案 > Linux poll(2) 虽然所有信号都被阻塞但被中断

问题描述

当我复杂的多线程程序调用 poll(2) 时,调用被中断——即使所有信号都已被阻塞。此外,从不调用所有信号的已安装信号处理程序。我想了解可能导致这种情况的一些想法。

该程序太复杂而无法包含,但这里有一个说明问题的代码片段:

#define _XOPEN_SOURCE 700
#undef   NDEBUG
#include <assert.h>
#include <poll.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>

void foo(const int sock)
{
    sigset_t allSigs;

    sigfillset(&allSigs);

    int status = pthread_sigmask(SIG_BLOCK, &allSigs, NULL); // Blocks all signals
    assert(status == 0);
    {
        sigset_t sigset;
        status = sigpending(&sigset);
        assert(status == 0);
        for (int sigNum = 1; sigNum < _NSIG; ++sigNum)
            if (sigismember(&sigset, sigNum))
                fprintf(stderr, "Signal %d is pending", sigNum); // Prints nothing
    }

    struct pollfd pfd;

    pfd.fd = sock;
    pfd.events = POLLRDNORM;
    errno = 0; // Just to make sure
    status = poll(&pfd, 1, 30000); // All-signals signal-handler never called

    if (status < 0)
        perror("poll() failure"); // Prints "poll() failure: Interrupted system call"
}

我知道,这是不可能的——但它正在发生。

如果您对可能导致此问题的原因有任何想法,我想听听他们的意见。

这是我的环境:

$ cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
$ uname -a
Linux gilda 3.10.0-862.11.6.el7.x86_64 #1 SMP Tue Aug 14 21:49:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
$ gcc -dumpversion
4.8.5

标签: linuxsignalspolling

解决方案


推荐阅读