1. Signals sent to a process that is not currently executing must be saved by the kernel until that process resumes execution.
2. 信号的两个概念:
-
内核或者一个进程向另一个进程发送信号(
send a signal to),包括两个过程:
-
The kernel updates a data structure of the destination process to represent that a new signal has been sent.
!!终于在 11.2 找到了一个信号如何令一个阻塞的系统调用中断并返回的,即把“调用该阻塞的系统调用的”进程 从TASK_INTERRUPTIBLE状态唤醒:
- 1. Many kernel functions generate signals: they accomplish the first phase of signal handling ( 注:signal generation ) described earlier in the section "The Role of Signals" by updating one or more process descriptors as needed. 2. specific_send_sig_info() -->signal_wake_up() --> try_to_wake_up()
-
Invokes try_to_wake_up( )see the section "The try_to_wake_up( ) Function" inChapter 7to
awake the process if it is either in TASK_INTERRUPTIBLE state
, or inTASK_STOPPED state and the signal isSIGKILL.
Signal generation
All functions in Table 11-9 end up invoking the specific_send_sig_info( ) function described in the next section.
Signal delivery
The kernel forces the destination process to react to the signalby changing its execution state,(有可能唤醒睡眠中的进程) by starting the execution of a specified signal handler, or both.
3. 信号挂起(pending)的概念
Signals that have been generated but not yet delivered are called pending signals.
At any time, only one pending signal of a given type may exist for a process; additional pending signals of the same type to the same process are not queued but simply discarded.(信号不排队) Real-time signals are different, though: there can be several pending signals of the same type.
信号挂起的几个因素:
-
Signals are usually delivered only to the currently running process (that is, to thecurrent process).
-
Signals of a given type may be selectively blocked by a process (see the later section "Modifying the Set of Blocked Signals"). In this case, the process does not receive the signal until it removes the block.
(相关API:sigprocmask()通过使用信号阻塞字来阻塞信号的投递,sigpending()可以获取已经产生,当前正阻塞着的信号;) -
When a process executes a signal-handler function, it usually masks the corresponding signal.e., it automatically blocks the signal until the handler terminates. A signal handler therefore cannot be interrupted by another occurrence of the handled signal, and the function doesn't need to be reentrant.
4. 信号阻塞和被忽略的区别:
sigprocmask()设置阻塞的信号,如果信号这些阻塞的信号产生,那么它们是挂起的,用sigpending()可以查看这些挂起的信号;
Notice that blocking a signal is different from ignoring it. A signal is not delivered as long as it is blocked; it is delivered only after it has been unblocked. An ignored signal is always delivered, and there is no further action.
5. SIGKILL/SIGSTOP
The SIGKILL and SIGSTOP signals cannot be ignored, caught(设置信号处理函数), or blocked, and their default actions must always be executed. Therefore,SIGKILL and SIGSTOP allow a user with appropriate privileges to terminate and to stop, respectively, every process,[*] regardless of the defenses taken by the program it is executing.
6. Fata Signal
A signal is fatal for a given process if delivering the signal causes the kernel to kill the process. TheSIGKILL signal is always fatal.
POSIX Requirement:If a fatal signal is sent to a multithreaded application, the kernel will kill all threads of the applicationnot just the thread to which the signal has been delivered.
7. Thread Group
In this chapter the term "thread group" denotes any thread group, even if it is composed by a single (conventional) process. For instance, when we state thatkill( ) can send a signal to a thread group, we imply that this system call can send a signal to a conventional process, too. We will use the term "process" to denote either a conventional process or a lightweight process that is, a specific member of a thread group.
7. 内核信号相关数据结构
内核需要这些数据结构来处理的任务:
For each process in the system, the kernel must keep track of what signals are currently pending or masked; the kernel must also keep track of how every thread group is supposed to handle every signal.
task_struct 中和内核相关的字段如下:
8.
参考:Understanding The Linux Kernel, 3rd, Chapter 11