简体   繁体   English

这段代码会阻塞所有产生的线程的信号,但仍会在MAIN()中捕获某些信号吗?

[英]Will this code block all signals to spawned thread but still catch some in MAIN()?

I wrote a similar question at How to block all SIGNALS in thread WITHOUT using SIGWAIT? 我在如何使用SIGWAIT阻止线程中的所有信号时写了一个类似的问题 but must admit I still need 100% clarity on the topic...C is not my day job ;-) Sorry for the similar question.... 但必须承认,我仍然需要100%的主题清晰度... C不是我的日常工作;-)对不起类似的问题...。

All i need 100% clarification on is: 我需要100%的澄清是:

  • I want to block all signals from going to the thread I create, but I want to catch the signals mentioned below in the MAIN thread, hence the SIG_UNBLOCK after the thread creation. 我想阻止所有信号进入创建的线程,但是我想在MAIN线程中捕获下面提到的信号,因此要在创建线程后捕获SIG_UNBLOCK。

  • Also very important is to prevent any interruptions to libraries I have no control over from being "disturbed" during ie a SIGINT. 此外,非常重要的是要防止任何中断我没有控制权从库中即SIGINT被“打扰”。 I have a situation where a WAIT step is happening in a thread on a "message GET from a queue". 我遇到一种情况,“线程从队列中获取消息”上的线程中正在发生等待步骤。 That wait step seems to reject SIGINT even though the signal handler is defined in main, hence the SA_RESTART below. 即使在main中定义了信号处理程序,该等待步骤似乎也拒绝了SIGINT,因此下面的SA_RESTART也是如此。

Could you please let me know if the code below will accomplish this? 您能否让我知道下面的代码是否可以做到这一点? I am pretty sure it is ok. 我很确定可以。

Thanks for the help, much appreciated 感谢您的帮助,非常感谢

Lynton 林顿

The following is a snippet of the MAIN program: 以下是MAIN程序的代码段:

int main(int argc, char * argv[]){
    sigset_t set;
    struct sigaction sa_shutdown;
    struct sigaction sa_error;  

    //Shutdown hook for CTRL-C    
    sa_shutdown.sa_handler = shutdownHook;
    sigemptyset(&sa_shutdown.sa_mask);
    sa_shutdown.sa_flags = SA_RESTART;
    sigaction(SIGINT, &sa_shutdown, NULL);        
    //Error handlers for erroneous signals
    sa_error.sa_handler = signalErrorHandler;
    sigemptyset(&sa_error.sa_mask);
    sa_error.sa_flags = SA_RESTART;
    sigaction(SIGSEGV, &sa_error, NULL);    
    sigaction(SIGBUS, &sa_error, NULL); 
    sigaction(SIGILL, &sa_error, NULL); 
    sigaction(SIGTERM, &sa_error, NULL); 
    sigaction(SIGABRT, &sa_error, NULL);    

    //BLOCK all SIGNALS in threads  
    sigfillset(&set);
    rc = pthread_sigmask(SIG_BLOCK, &set, NULL);    
    if(rc != 0){
        printf("Thread sigmask failed\n");
        exit(1);
    }
    rc = pthread_create(&outboundThread, NULL, outboundThreadMainLoop, (void *)argv);
    if(rc != 0){
        printf("Thread creation failed\n");
        exit(1);
    }
    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
    pthread_join(outboundThread, NULL); 
    return 0;
}

Regarding to block all signals, your code looks OK to me. 关于阻止所有信号,您的代码对我来说还可以。

Anyway I'd say you should not block SIGFPE, SIGILL, SIGSEGV, or SIGBUS. 无论如何,我要说您不应该阻止SIGFPE,SIGILL,SIGSEGV或SIGBUS。

pthread_sigmask() 's man page says: " 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. " pthread_sigmask()的手册页上说:“ 如果在阻止时生成了SIGFPE,SIGILL,SIGSEGV或SIGBUS信号中的任何一个,则结果是不确定的,除非该信号是由kill()函数生成的,否则sigqueue( )函数或raise()函数。

Regarding " ... interruptions to libraries ... ": If your are referring to any call into libraries from a thread created the way you did in your example you, I'd also say that's OK, they won't be interrupted. 关于“ ...对库的中断... ”:如果您是指以您在示例中所做的方式创建的线程对库的任何调用,那么我也可以说,它们不会被中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM