如何阻止在线程中的所有信号,而无需使用sigwait的? [英] How to block all SIGNALS in thread WITHOUT using SIGWAIT?

查看:229
本文介绍了如何阻止在线程中的所有信号,而无需使用sigwait的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成一个单独的线程来处理消息掀起了队列中的主要应用。我有AIX上的问题,当我按CTRL-C,因为它似乎使在线程变为无效一些连接句柄。我有在主程序关闭钩子捕获SIGINT,但在AIX上似乎以某种方式发出一个信号,线程以及...虽然说从我听到的是不是真的有可能...

I have a main application that spawns a seperate thread to process messages off a queue. I have an issue on AIX when I hit CTRL-C as it seems to make some "connection handles" in the thread become invalid. I do have a shutdown hook in the main program catching the SIGINT but on AIX it seems to somehow send a signal to the thread as well...although that is not really possible from what I hear...

基本上我想知道如果我想在主应用程序来处理所有的信号我很感兴趣,并纷纷跟帖/从来没有处理任何信号是......良好做法?

Essentially I would like to know if I want the MAIN application to handle ALL signals I am interested in and have the thread/s NEVER handle any signals...is that "good practice"?

如果这样我怎么不能用调用sigwait在跟帖......其实我不希望有任何信号code中的线程/ s的...他们必须根本不是在所有接收任何信号

If so how can I NOT use "sigwait" in the thread...in fact I do not want any "signal code" in the thread/s...they must simply not receive any signals at all.

我已经清空了所有的信号:

I have emptied out all the signals:

sigemptyset(&set);

和已设置的SIG_BLOCK

And have set the SIG_BLOCK

s = pthread_sigmask(SIG_BLOCK, &set, NULL);

因此​​,这里是一个虚拟测试PROGRAME:

So here is a dummy test programe:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>

#define handle_error_en(en, msg) do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

static void * threadMainLoop(){
    //Here I do not want the thread to use "sigwait"....
    while(running == TRUE){
      //do some thread work and never have any signals come in
    }
}

void shutdownHook(int sig){
    printf("\nCtrl-C pressed....shutdown hook in main...\n");
}

void signalErrorHandler(int signum){
    printf("\nSignal error handler in main...\n");
}

int main(int argc, char *argv[]){
    pthread_t thread;
    sigset_t set;
    int s;

    //Catch the following signals in the MAIN thread
    (void) signal(SIGINT, shutdownHook);
    (void) signal(SIGSEGV, signalErrorHandler);
    (void) signal(SIGBUS, signalErrorHandler);
    (void) signal(SIGILL, signalErrorHandler);
    (void) signal(SIGTERM, signalErrorHandler);
    (void) signal(SIGABRT, signalErrorHandler);

    sigemptyset(&set); //BLOCK all signals

    s = pthread_sigmask(SIG_BLOCK, &set, NULL);
    if (s != 0)
        handle_error_en(s, "pthread_sigmask");

    s = pthread_create(&thread, NULL, &threadMainLoop, (void *) NULL);
    if (s != 0)
        handle_error_en(s, "pthread_create");  

    pause();
}

如果我只是创建一个线程,并有,例如,在主线程SIGINT信号处理程序,但不要有SIG_BLOCK设置线程和用户点击CTRL-C ....没有线程得到受影响的所有即使在主线程中运行的信号处理程序?这似乎是我所看到的AIX; - (

If I just create a thread and have, for example, the SIGINT signal handler in the MAIN thread but do NOT has the SIG_BLOCK set for the thread and the user hits CTRL-C....does the thread get affected at all even though the signal handler in the main thread runs? That seems to be what I am seeing on AIX ;-(

感谢您的帮助,很多AP preciated

Thanks for the help, much appreciated

林顿

推荐答案

通过 S = pthread_sigmask(SIG_BLOCK,&安培;集,NULL); ,你不堵任何东西。

With s = pthread_sigmask(SIG_BLOCK, &set, NULL); , you're not blocking anything.

使用:

sigfillset(&set);
sets = pthread_sigmask(SIG_SETMASK, &set, NULL);

如果您想要阻止每一个信号,或者明确添加要阻止的信号设置如果你使用SIG_BLOCK。

If you want to block every signal, or explicitly add the signals you want to block to the set if you're using SIG_BLOCK.

您已经创建线程后,您需要恢复信号屏蔽,否则没有线程将捕获任何信号。

After you've created the threads, you need to restore the signal mask, otherwise no threads will catch any signal.

不过,看着你的previous的问题,它可能是线捕捉信号不处理被打断。也就是说,如果你正在做阻塞系统调用,信号到达时,即系统调用被中止。某些操作系统默认为自动再次调用系统调用,有的返回一个错误,errno设置为EINTR,应用程序必须处理 - 如果这不是处理可能发生不好的事情

However, looking at your previous question, it might be that the thread catching the signal doesn't handle being interrupted. That is, if you're blocked doing a syscall, and a signal arrives, that syscall gets aborted. Some operating systems defaults to automatically call the system call again, some returns an error and sets errno to EINTR, which the application must handle - and bad things might happen if that's not handled.

相反,随着<一个安装信号处理程序href=\"http://publib.boulder.ibm.com/infocenter/aix/v6r1/index.jsp?topic=/com.ibm.aix.basetechref/doc/basetrf2/sigaction.htm\">sigaction()信号(),而不是和设置 SA_RESTART 标记,这将导致系统调用的情况下,自动重新启动它得到了由信号中断。

Instead, install your signal handlers with sigaction() instead of signal() , and set the SA_RESTART flag, which will cause system calls to automatically restart in case it got aborted by a signal.

这篇关于如何阻止在线程中的所有信号,而无需使用sigwait的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆