linux - 信号线程问题

标签 linux multithreading boost signals

我正在尝试让一个线程运行,该线程将负责检测来自同一进程中任何其他线程的信号。

sigset_t    sigset;
sigfillset(&sigset);
pthread_sigmask( SIG_BLOCK, &sigset, &old );

完成此操作后,我将流程守护进程:

m_child = fork();
if (m_child == 0)
{
     // Start the thread responsible for detecting a signal
}

这是在另一个名为“SignalHandler”的类中完成的,该线程的启动如下:

m_pThread   = new boost::thread( boost::bind(&SignalHandler::operator(), this ) );

仿函数在一个紧密的循环中运行如下:

while (1)
{
    if ( sigtimedwait( &sigset, &info, &ts ) == -1 )
    {
        if ( errno == EAGAIN || errno == EINTR )
        {
            std::cout << "Time-out waiting for signal" << std::endl;
        }

        throw SystemError( "sigtimedwait error" );
    }
    else
    {
        std::cout << "Detected a signal [" << info.si_signo << "]" << std::endl;
    }
}

这在我执行“kill -9 myProcess”时有效,但当我实际执行一些非法指针操作时(在任何其他线程中),进程核心转储。

为什么子线程产生的信号没有被我的boost信号捕捉线程捕捉到?

最佳答案

啊 - 我明白了。

问题出在这里:

sigset_t    sigset;
sigfillset(&sigset);
pthread_sigmask( SIG_BLOCK, &sigset, &old );

对 pthread_sigmask 使用 sigfillset 是危险的,因为阻塞同步信号会导致不良影响。

If any of the SIGFPE, SIGILL, SIGSEGV, or SIGBUS signals are generated while they are blocked, the result is undefined, unless the signal was generated by the kill() function, the sigqueue() function, or the raise() function.

关于linux - 信号线程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2409797/

相关文章:

linux - 将 stdout 传递给同一个命令两次

linux - rsync:如何向远程端验证用户身份

c - 如何测量Linux多线程环境中函数的总执行时间

java - 如果将来的任务抛出异常,线程是否返回到执行器服务的线程池?

c++ - 如何处理 "Error in function boost::math::cyl_bessel_k<d>(d,d): numeric overflow"

Ubuntu boost b2 错误 : Cannot use --layout=system with --build-type complete

linux - 在VI编辑器中如何在打开的文件中定义变量以通过gf打开其他文件

php - 进程 fork 期间 PHAR 内部损坏(crc32 不匹配)

等待锁定未(明显)锁定的对象的 Java 线程

c++ - 将自定义类型与 Boost 程序选项一起使用