问题描述
在 Linux 中,当一个程序(可能有多个线程)收到一个信号时会发生什么,比如 SIGTERM 或 SIGHUP?
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?
推荐答案
根据您使用的 Linux 内核版本,这略有不同.
This is slightly nuanced, based on which version of the Linux kernel you are using.
假设有 2.6 个 posix 线程,如果您谈论的是操作系统发送 SIGTERM 或 SIGHUP,则信号被发送到进程,由根线程接收和处理.使用 POSIX 线程,您也可以将 SIGTERM 发送到单个线程,但我怀疑您是在询问操作系统向进程发送信号时会发生什么.
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.
在 2.6 中,SIGTERM 将导致子线程干净地"退出,而在 2.4 中,子线程处于不确定状态.
In 2.6, SIGTERM will cause child threads to exit "cleanly", where as 2.4, child threads were left in an indeterminate state.
这篇关于Linux 中多线程的信号处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!