First of all: most Unix systems such as Linux have achieved reliable signals. 1 ~ 31 signal and SIGRTMIN-SIGRTMAX is not the difference between reliable signal and unreliable signal, in most systems they are reliable signal. Only: 1 ~ 31 signal-queuing is not supported, which is a common signal. SIGRTMIN-SIGRTMAX signal-support queuing, real-time signal
Unreliable signal what is unreliable signal: unreliable means that the signal may be lost or processed incorrectly. In the early-rising system, the signal has two major defects, resulting in unreliable signals. Defect 1: After a signal occurs, the signal processing method is reset to the default action of the system. It is still said that the knowledge of the signal function associates the signal with our signal processing function once. After a signal occurs, the signal processing method is reset to the system default. This causes the following code to be used in the signal processing function:
int sig_int(); /* my signal handling function */
...
signal(SIGINT, sig_int); /* @1establish handler */
...
sig_int()
{
signal(SIGINT, sig_int); /* @2reestablish handler for next time */
...
./*process the signal ... */
...
}
We have to use signal () again in the signal processing function ().
However, this process does not guarantee that the program is completely correct, because when a signal occurs, we start to call the sig_int function, there is a time interval between the signal function (that is, the @ 2 Code) in the execution of the sig_int function. if a signal occurs again during this interval, the processing method for this signal is the default method. Therefore, early signals are unreliable because they cannot guarantee that all signals are processed in the correct (we expect) processing method.
Defect 2: poor signal control over processes:
In early system implementation, when we do not want a signal to happen, the process cannot close a signal and record it.
Many times we have such a requirement that we do not want a signal to interrupt a job, but when the work is completed, we want the system to tell us what signal has occurred during this period. For example, if we run a program and require that it not be interrupted before running (for example, our CTL + C), we need to temporarily disable this signal. First, let's clarify our requirements. What we need is that the signal does not work for the time being, and we can be reminded later that the signal has occurred.
To achieve this, we use the following code:
int sig_int(); /* my signal handling function */
int sig_int_flag; /* set nonzero when signal occurs */
main()
{
signal(SIGINT, sig_int); /* establish handler */
...
while (sig_int_flag == 0)
pause(); /* go to sleep, waiting for signal */
...
}
sig_int()
{
signal(SIGINT, sig_int); /* reestablish handler for next time */
sig_int_flag = 1; /* set flag for main loop to examine */
}
Sig_int has only two lines of code. Its function is to ignore the signal and use sig_int_flag to indicate that the signal has occurred. The reason for using while is only because pause may be interrupted by other signals. (My understanding)
There are still defects in this Code. After the while test, there is a time interval before pause. If the signal occurs during this time interval and does not occur since then, the process continues to sleep.
Reliable signal: the reliable signal can be understood to solve the two defects of the unreliable signal:
Solving unreliable Signal Defects 1. Replacing signal with sigaction
(In most modern systems, signal also uses sigaction implementation, so it is reliable .) We recommend that you use sigaction instead of signal as much as possible, because sigaction is the same standard and highly portable. Once an action is installed on the signal by sigaction, the signal action remains valid until it is displayed and changed in the call. That is to say, the default action will not be returned once. This processing method is different from the signal processing method that is unreliable in Early systems. It can be said that the current system signal processing method is reliable.
For more information about sigaction and signal, see this blog.
Solution to unreliable signal defect 2: You can use sigprocmask and sigaction to set blocked words as signal blocking. Putting the signal in pending state naturally solves defect 2.
Apue Study Notes-10. Reliable and unreliable Signals