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 ? 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 ?
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. Signals automatically generated as a result of things the thread does (like SIGSEGV
SIGFPE
, and SIGPIPE
) are also delivered to that particular thread.
Signals generated from the terminal (SIGINT
, SIGTSTP
, SIGQUIT
) are delivered to the whole process.
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. 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).
精彩评论