Pending signal and signal blocking

  

Signal state: The signal's "Pending" is a state that refers to the period from the generation of the signal until the signal is processed; the signal & rdquo; blocking & ldquo; Is a switching action that refers to blocking signals from being processed, but not blocking signals. The APUE example uses sigprocmask to block the exit signal before sleep, then sleep, and then generates an exit signal during the sleep process, but the exit signal is blocked at this time. (Chinese & rdquo; blocking & rdquo; is easily misunderstood here. The state is actually a kind of action similar to a switch, so it is said that "blocked too", instead of "blocked", so in the "pending" state, after sleep, use sigprocmask off The blocking switch of the exit signal is removed, because the previously generated exit signal is always in the pending state. When the blocking switch is closed, the status is "exited" and processed, which occurs before the sigprocmask returns. Summary: Signals are blocked and not discarded, but are not processed. This state is called a pending state. After unblocking, the signal in "pending status" will continue to be processed.

Unblocking calls sigprocmask, but before sigprocmask returns, the blocking action is complete.

Before the unblocking function sigprocmask returns, the pending signal is processed.

int sigpend(sigset_t *set);---Get pending signals.

int sigprocmask(int how,const sigset_t *set,sigset_t *oset);

---- According to the value of how to complete the action, blocking, unblocking, etc..

Note: It is impossible to have the same in the pending signal. If the signal is generated

once and is pending, it will be discarded when it is generated again.

Of course, if it is a real-time signal, it is another matter. Because the real-time signal can be multiple of the same pending signal.

Signal Lifecycle: For a complete signal lifecycle (from signal to execution of the corresponding handler), it can be divided into three important phases, which consist of four important events. To portray: 1. The signal is born; 2. The signal is registered in the process; 3. The signal is cancelled in the process; 4. The signal processing function is executed. The time interval between two adjacent events constitutes a phase of the signal life cycle. The following describes the practical significance of the four events: 1. Signal " Birth". The birth of a signal refers to the occurrence of a trigger signal (such as detecting a hardware exception, timer timeout, and calling the signal sending function kill () or sigqueue (), etc.).

2. The signal in the target process "registration"; process's task_struct structure has data members about the pending signal in the process: struct sigpending pending; struct sigpending{struct sigqueue *head, ** Tail; sigset_t signal;}; The first and second members point to the beginning and end of a sigqueue type structure chain (referred to as "pending signal information chain"), and the third member is all pending signals in the process. Set, each sigqueue structure in the information chain depicts the information carried by a particular signal and points to the next sigqueue structure: struct sigqueue{struct sigqueue *next; siginfo_t info;}; signal registration in the process refers to the signal value Joined into the pending signal set of the process (the second member of the sigpending structure sigset_t signal), and the information carried by the signal is retained in a sigqueue structure of the pending signal information chain. As long as the signal is in the pending signal set of the process, it indicates that the process already knows the existence of these signals, but has not had time to process it, or the signal is blocked by the process. Note: When a real-time signal is sent to a process, it will be re-registered regardless of whether the signal is already registered in the process. Therefore, the signal will not be lost. Therefore, the real-time signal is also called "reliable signal". This means that the same real-time signal can occupy multiple sigqueue structures in the pending signal information chain of the same process (every process receives a real-time signal, it will assign a structure to register the signal information and add the structure. At the end of the pending signal chain, all generated real-time signals are registered in the target process); when a non-real-time signal is sent to a process, if the signal is already registered in the process, the signal will be discarded, causing the signal Lost. Therefore, the non-real-time signal is also called "unreliable signal". This means that the same non-real-time signal has at most one sigqueue structure in the pending signal information chain of the process (after a non-real-time signal is born, (1), if it is found that the same signal has been registered in the target structure, then it is no longer Registration, for the process, is equivalent to not knowing that this signal occurs, the signal is lost; (2) If the process does not have the same signal in the pending signal, register itself in the process).

3. The signal is unregistered in the process. During the execution of the target process, it is detected whether there is a signal waiting to be processed (this check is done each time it returns from system space to user space). If there is a pending signal waiting to be processed and the signal is not blocked by the process, the process will remove the structure occupied by the signal in the pending signal chain before running the corresponding signal processing function. Whether to remove the signal from the process pending signal set is different for real-time and non-real-time signals. For non-real-time signals, since only one sigqueue structure is occupied in the pending signal information chain, after the structure is released, the signal should be deleted in the process pending signal set (signal cancellation completed); It is said that multiple sigqueue structures may be occupied in the pending signal information chain, so it should be treated differently for the number of occupied gqueue structures: if only one sigqueue structure is occupied (the process only receives the signal once), then the signal should be in the process. Deletion of the pending signal set (signal logout completed). Otherwise, the signal is not deleted in the pending signal set of the process (signal logout completed). Before executing the signal corresponding processing function, the process must first log out the signal in the process.

4. Signal life is terminated. After the process deregisters the signal, the corresponding signal processing function is executed immediately. After the execution is completed, the effect of the current transmission of the signal on the process is completely ended. Note: 1) Whether the signal is registered or not, regardless of the function of sending signals (such as kill() or sigqueue(), etc.) and the signal installation function (signal() and sigaction()), only related to the signal value (signal value is less than SIGRTMIN) The signal is registered only once, and the signal value between SIGRTMIN and SIGRTMAX is registered as long as it is received by the process. 2) If the process receives the same signal multiple times while the signal is being logged out to the corresponding signal processing function, each time the real-time signal is registered in the process; for non-real-time signals It is said that no matter how many times the signal is received, it will be regarded as receiving only one signal and only registering once in the process.

Copyright © Windows knowledge All Rights Reserved