简体   繁体   English

在C中的多线程程序中,线程不应该阻塞哪些信号?

[英]What signals shouldn't be blocked by thread in multi-threaded program in C?

I have a multi-threaded program. 我有一个多线程程序。 I want to handle all meaningful signals in a single thread. 我想在一个线程中处理所有有意义的信号。 That's what I do before creating any thread: 这就是我在创建任何线程之前所做的事情:

sigset_t signal_set, old_set;

sigfillset(&signal_set);
// What signals should I leave unblocked here?
pthread_sigmask(SIG_SETMASK, &signal_set, &old_set);

std::thread( /* some args */ );

pthread_sigmask(SIG_SETMASK, &old_set, nullptr);

But my good sense suggests leaving some signals unblocked, since there is a plenty of situation when signals may be sent directly to specific thread: SIGSEGV or SIGPROF - I believe, it's as good as leaving unblocked SIGINT in an interactive program. 但我的理智建议保留一些信号畅通无阻,因为有很多情况可以将信号直接发送到特定线程: SIGSEGVSIGPROF - 我相信,它与在交互式程序中保留未阻塞的SIGINT一样好。


Are my suggestions correct about those two signals ( SIGSEGV , SIGPROF )? 我的建议是否正确这两个信号( SIGSEGVSIGPROF )?

What other signals should I leave unblocked following some common sense? 根据一些常识,我应该保留哪些其他信号?

Asynchronous signals (that's most of them, including anything sent by the kill command/function and signals generated by the controlling terminal like SIGINT ) are deliverable to any thread in the process that has the signal unblocked, so there's no need to keep them unblocked in all threads. 异步信号(其中大部分信号,包括由kill命令/函数发送的任何信号以及控制终端产生的信号,如SIGINT )都可以传送到信号未被阻塞的进程中的任何线程,因此不需要保持它们畅通无阻。所有线程。 If you're using a dedicated signal handling thread, you want them blocked in all threads except the signal handling thread. 如果您正在使用专用信号处理线程,则希望它们在除信号处理线程之外的所有线程中都被阻止。

Synchronous signals on the other hand are delivered to a particular thread as a result of an action by that thread. 另一方面,同步信号由于该线程的动作而被传递到特定线程。 They include SIGPIPE , SIGBUS , SIGSEGV , SIGFPE , etc. Except for SIGPIPE , none of these should happen at all unless you have serious bugs in your program, and you probably want to block SIGPIPE anyway so you can instead get the EPIPE error and handle this condition properly. 它们包括SIGPIPESIGBUSSIGSEGVSIGFPE等。除了SIGPIPE ,除非您的程序中存在严重错误,否则这些都不会发生,并且您可能想要阻止SIGPIPE ,以便您可以反而获得EPIPE错误并处理这个条件得当。 So for the most part I would say it doesn't hurt to just block them all. 因此,在大多数情况下,我会说只是阻止它们并没有什么坏处。 If you really find yourself needing to handle SIGSEGV or such, you probably should rethink the reasons, but in the mean time feel free to unblock it. 如果你真的发现自己需要处理SIGSEGV等,你可能应该重新考虑原因,但同时可以随意取消阻止它。

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

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