signal(7)描述了POSIX.1要求进程共享属性中的所有线程,包括:
信号处理
POSIX.1还要求每个线程都有一些不同的属性,包括:
信号掩码(signal(7))
备用信号堆栈(signal(7))
Linux内核的signal(7)例程具有以下代码块 - 注释非常有用:
/*
* 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_GROUP_EXIT) &&
!sigismember(&t->real_blocked, sig) &&
(sig == SIGKILL || !p->ptrace)) {
/*
* This signal will be fatal to the whole group.
*/
所以,你看到你负责传递信号的地方:
如果您的进程已将信号的处置设置为signal(7)或SIG_DFL,则会忽略所有线程的信号(或默认值 - kill,core或ignore)。
如果您的进程已将信号设置为特定处理程序例程,则可以通过使用signal(7)操纵特定线程信号掩码来控制哪个线程将接收信号。您可以指定一个线程来管理它们,或者为每个信号创建一个线程, 或者这些选项的任何混合用于特定信号,或者您依赖于Linux内核当前将信号传递到主线程的默认行为。
但是,根据signal(7)手册页,某些信号是特殊的:
可以为整个过程生成(并因此待决)信号 (例如,使用kill(2)发送时)或特定线程(例如, 某些信号,如SIGSEGV和SIGFPE,生成为 执行特定机器语言指令的结果是 线程定向,以及针对特定线程使用的信号pthread_kill(3))。 可以将过程引导的信号传递给任何信号 其中一个当前没有阻塞信号的线程。 如果多个线程的信号未被阻塞,那么 内核选择一个任意线程来传递信号。