简体   繁体   English

线程和进程的不同信号处理程序? 可能吗

[英]different signal handler for thread and process?. Is it possible

Have few questions regarding Signaling. 关于信令有几个问题。

1) when the process has few more threads along with main thread, and if the signal is raised, which thread will stop its processing and continue with signal handler ? 1)当进程与主线程一起的线程数减少时,并且如果发出信号,哪个线程将停止其处理并继续执行信号处理程序? Is it main thread or other than main thread ? 是主线程还是主线程以外的线程?

2) Is it possible to keep different handler for the same signal between main thread and specific thread ? 2)是否可以为主线程和特定线程之间的同一信号保留不同的处理程序?

Signals can be sent to either a process or a particular thread. 信号可以发送到进程或特定线程。 For signals sent to the process, the signal will be delivered as soon as there is at least one thread where that signal isn't blocked, and if there's more than one such thread, it may be delivered to any one of them (unpredictable which one). 对于发送到进程的信号,只要有至少一个线程未阻塞该信号,便会立即发送该信号;如果有多个这样的线程,则可能会将该信号传递给其中的任何一个(无法预测一)。 For signals sent to a particular thread, they're delivered as soon as that thread does not have the signal blocked. 对于发送到特定线程的信号,只要该线程没有信号被阻塞,它们就会立即传递。

Using the raise function to raise a signal sends the signal to the thread that called raise , not the whole process. 使用raise函数引发信号会将信号发送到称为raise的线程,而不是整个过程。 Signals automatically generated as a result of things the thread does (like SIGSEGV SIGFPE , and SIGPIPE ) are also delivered to that particular thread. 由于线程所做的事情而自动生成的信号(例如SIGSEGV SIGFPESIGPIPE )也将传递到该特定线程。

Signals generated from the terminal ( SIGINT , SIGTSTP , SIGQUIT ) are delivered to the whole process. 从终端生成的信号( SIGINTSIGTSTPSIGQUIT )被传递到整个过程。

There is no way to install separate signal handlers for each thread, but the signal handler for a signal may be able to examine which thread it's running in. If you know the signal did not interrupt an async-signal-unsafe function, you could call pthread_self to get the current thread id. 没有为每个线程安装单独的信号处理程序的方法,但是信号的信号处理程序可能能够检查它正在运行的线程。如果您知道信号没有中断async-signal-unsafe函数,则可以调用pthread_self获取当前线程ID。 Otherwise, one ugly but safe method is to take the address of errno and look up which thread you're in based on that (you'll have to keep a mapping table yourself and ensure that access to this table is async-signal-safe). 否则,一种丑陋但安全的方法是获取errno的地址,并基于该地址查找您所在的线程(您必须自己保留一个映射表,并确保对该表的访问是异步信号安全的)。

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

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