Question
In Linux, what happens when a program (that possibly has multiple threads) receives a signal, like SIGTERM or SIGHUP?
Which thread intercepts the signal? Can multiple threads get the same signal? Is there a special thread dedicated entirely to handling signals? If not, what happens inside the thread that is to handle the signal? How does the execution resume after the signal handler routine finishes?
Answer by Alan
This is slightly nuanced, based on which version of the Linux kernel you are using.
Assuming 2.6 posix threads, and if you are talking about the OS sending SIGTERM or SIGHUP, the signal is sent to process, which is received by and handled by root thread. Using POSIX threads, you can also sent SIGTERM to individual threads as well, but I suspect you are asking about what happens when the OS sends the signal to the process.
In 2.6, SIGTERM will cause child threads to exit “cleanly”, where as 2.4, child threads were left in an indeterminate state.
Answer by sarnold
pthreads(7)
describes that POSIX.1 requires all threads in a process share attributes, including:
- signal dispositions
POSIX.1 also requires some attributes to be distinct for each thread, including:
- signal mask (
pthread_sigmask(3)
) - alternate signal stack (
sigaltstack(2)
)
The Linux kernel’s complete_signal()
routine has the following code block – the comments are quite useful:
1 | /* |
So, you see that you are in charge of where signals are delivered:
If your process has set a signal’s disposition to SIG_IGN
or SIG_DFL
, then the signal is ignored (or default – kill, core, or ignore) for all threads.
If your process has set a signal’s disposition to a specific handler routine, then you can control which thread will receive the signals by manipulating specific thread signal masks using pthread_sigmask(3)
. You can nominate one thread to manage them all, or create one thread per signal, or any mixture of these options for specific signals, or you rely on the Linux kernel’s current default behavior of delivering the signal to the main thread.
Some signals, however, are special:
1 | A signal may be generated (and thus pending) for a process as |
Article link: http://xnerv.wang/signal-handling-with-multiple-threads-in-linux/
Reprinted from: (StackOverflow) Signal handling with multiple threads in Linux