0%

(Stack Overflow) Signal handling with multiple threads in Linux

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* Now find a thread we can wake up to take the signal off the queue.
*
* If the main thread wants the signal, it gets first crack.
* Probably the least surprising to the average bear.
*/
if (wants_signal(sig, p))
t = p;
else if (!group || thread_group_empty(p))
/*
* There is just one thread and it does not need to be woken.
* It will dequeue unblocked signals before it runs again.
*/
return;
else {
/*
* Otherwise try to find a suitable thread.
*/
t = signal->curr_target;
while (!wants_signal(sig, t)) {
t = next_thread(t);
if (t == signal->curr_target)
/*
* No thread needs to be woken.
* Any eligible threads will see
* the signal in the queue soon.
*/
return;
}
signal->curr_target = t;
}

/*
* Found a killable thread. If the signal will be fatal,
* then start taking the whole group down immediately.
*/
if (sig_fatal(p, sig) &&
!(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
!sigismember(&t->real_blocked, sig) &&
(sig == SIGKILL || !t->ptrace)) {
/*
* This signal will be fatal to the whole group.
*/

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
2
3
4
5
6
7
8
9
10
11
A signal may be generated (and thus pending) for a process as
a whole (e.g., when sent using kill(2)) or for a specific
thread (e.g., certain signals, such as SIGSEGV and SIGFPE,
generated as a consequence of executing a specific machine-
language instruction are thread directed, as are signals
targeted at a specific thread using pthread_kill(3)). A
process-directed signal may be delivered to any one of the
threads that does not currently have the signal blocked. If
more than one of the threads has the signal unblocked, then
the kernel chooses an arbitrary thread to which to deliver
the signal.

Article link: http://xnerv.wang/signal-handling-with-multiple-threads-in-linux/
Reprinted from: (StackOverflow) Signal handling with multiple threads in Linux