Linux signal (signal) mechanism

First, what is the signal

1. The nature of the signal

Soft interrupt signal (signal, and the signal for short) is used to inform the process of asynchronous events occurred. On the software level is an analog interrupt mechanism, in principle, a process receives a signal from the processor receives an interrupt request can be said to be the same. Interprocess communication mechanisms signal is the only asynchronous communication mechanism, a process does not have to wait for the signal to reach by any action, in fact, the process signals in the end do not know what time to arrive. You can call each other kill soft interrupt signal is sent through the system between processes. The kernel can also send a signal to the process because of internal events, the occurrence of an event notification process. In addition to the basic notification signaling mechanism functions, you may also convey additional information.
The process of handling signals can be divided into three categories:

The first is similar to the interrupt handler for the signal to be processed, the process can be specified handler, it is handled by the function.
The second method is to ignore a signal, the signal is no treatment, just not happened.
The third method is the default value of the signal processing system is retained, such default operation, the default action is such that most of the signal process terminates. The process to specify the behavior of the process for handling a signal call signal through the system.

2. Signal Classification

The reliability of the signal can be divided into reliable and unreliable signal signal versus time signal can be divided into real time signals and non-real-time signal.

Reliable and unreliable signal signal

Linux signal mechanism is basically inherited from Unix systems over. Early Unix system signaling mechanism is relatively simple and primitive, the signal value is less than SIGRTMIN signals are not reliable signal. This is the source "unreliable signals". Its main problem is that the signal may be lost.
Over time, it proved the need to improve the mechanism and expand the original signal. Since the original signal has been defined in many applications, it does not change, the final and had to add some new signals, and put them at the outset defined as reliable signals that support queue, will not be lost.
Located between the signal value of the signal SIGRTMIN and SIGRTMAX are reliable signal, a reliable signal to overcome the problem of signal may be lost. Linux support in the new version of the signal installation function sigation while () function and signaling sigqueue () still support early signal () function signal installation, support signal transmission function kill ().
Reliable and unreliable signal only signal values, but not with the installation and function of the signal transmission. Current signal linux in () is the function implemented by sigation (), and therefore, even by the signal () signal is installed at the end of the signal processing function call is also no longer a function of signal mounting. At the same time, () support the installation of real-time signal from the line signal, the same is not lost.
For the current functions of linux install two signals: signal () and sigaction (), they can not get the signal before SIGRTMIN become a reliable signal (not line up support, may still be missing, it is still not reliable signal), and after SIGRTMIN signal of support queue. The biggest difference between these two functions is that, after the installation of the signal sigaction can pass information to the signal processing function, and after the signal is not transmitted the information signal mounted to the signal processing function. For signal transmission function it is also the same.

Real-time and non-real-time signal signal

Early Unix systems define only 32 kinds of signals, the first 32 kinds of signals have a predefined value, with each signal to determine the meaning and purpose, and each signal has its own default action. Such as the keyboard, press CTRL ^ C, the SIGINT signal is generated, the default reaction termination signal is the process. After the real-time signal 32 represents a signal, a signal equivalent to the front reliable forth. This ensures that multiple real-time signals sent are received.
Do not support non-real-time signal line, it is not reliable signal; real-time signal support line, are a reliable signal.

Second, the common signal for Linux

1. Check the signal

We can view the current signaling system supported by kill -l command, the system supports different signal is not the same.
Here Insert Picture Description

2. Description of a common signal and

Here Insert Picture Description

Third, the use of signal

1. The signal mounting

Function prototype

#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum,sighandler_t handler);

The first parameter specifies a signal value, the second parameter specifies processing for the preceding signal value, the signal can be ignored (set parameter SIG_IGN); the system may employ the default processing of signals (parameter set SIG_DFL); can achieve their own treatment (parameter to specify the address of a function).
If the signal () call is successful, return the last call for the installation of the signal signum The signal (handler value of); failure SIG_ERR is returned.
Transmitted to the signal processing routine is the signal value of the integer parameter, such a signal processing routine that may process a plurality of signals.

2.kill function

Usually, we kill kill understood as a process, in fact, this description is not accurate, in fact, kill as a signal, sent by one process to another process, the signal is sent termination signal.

3. A simple example program

#include <stdio.h>
#include <signal.h>

#define print_words "Hello World"
#define sleep_time 1

int main(int argc ,char **argv)
{
        for( ; ; )
        {
              printf("%s\n",print_words);
              sleep(sleep_time);
        }
         
        printf("I'll exit.\n");
        
        return 0;
}

After compile and run the program, every second will print "Hello World", when we press Ctrl + c, use the kill command to view or use the program to terminate the process ID, we can function to capture a signal by signal (), allow certain termination signal is ignored. Procedures are as follows:

#include <stdio.h>
#include <signal.h>
  
#define sleep_time 2
#define print_words "Hello World"

void sig_handle(int signum)
{
        printf("Catch signal [%d]\n",signum);
}

int main(int argc,char **argv)
{
        signal(SIGINT,sig_handle);
        signal(SIGTERM,sig_handle);
        
        for( ; ; )
        {
               printf("%s\n",print_words);
               sleep(sleep_time);
        }

        printf("I'll exit.\n");

        return 0;
}

Compile and run the program
Here Insert Picture Description
can be found, signals are captured program will not quit, however, by the above common signal table can have a signal that is not captured!
Watcher, we can see that the last program of "I'll exit" and did not print, this is because the program after receiving a termination signal will immediately exit the program, but does not perform behind the content, but in many cases, we hope before the program exits to do certain things, such as a switch or turn off certain file descriptor, we only need a simple modification to the program!

#include <stdio.h>
#include <signal.h>

int found = 0;

#define sleep_time 2
#define print_words "Hello World"

void sig_handle(int signum)
{
        printf("Catch signal [%d]\n",signum);
        found = 1;
}

int main(int argc,char **argv)
{
        signal(SIGINT,sig_handle);
        signal(SIGTERM,sig_handle);
        
        while(!found)
        {
               printf("%s\n",print_words);
               sleep(sleep_time);
        }

        printf("I'll exit.\n");

        return 0;
}
Released eight original articles · won praise 11 · views 304

Guess you like

Origin blog.csdn.net/weixin_45121946/article/details/104588413