The signal mechanism is a very old process communication method used by Unix. It is used to notify another process (or itself) of what happened to one process, what to do.
1. signal:
Signals are a series of macros starting with SIG defined in signal. H, essentially integers. The signal can be sent through a process (calling kill, raise, alarm, abort, or kernel sending signal) or the kernel (by pressing CTL + C. The signal is also called Soft Interrupt. Note that the signal is only used to notify a process of events and does not transmit any data to the process.
2. Signal mechanism:
Understand how the kernel sends signals to a process, how the process receives a signal, how the process controls its response to the signal, when the kernel processes the signal, and how it processes the signal received by the process. the kernel sends signals to a process by setting corresponding signals in the signal domain of the process's struct task_struct. In the table items of the Progress table (struct task_struct), there is a soft interrupt signal field. Each digit in the field corresponds to a signal. When a signal is sent to the process, it corresponds to a position. From this we can see that the process can retain different signals at the same time, but for the same signal, the process does not know how many signals have come before processing. 2. check whether a signal is received when the process returns the user State from the kernel state (the process is running and processes the signal .), Or when you want to enter or exit a proper low-scheduling priority sleep state (depending on the priority of the process into sleep, if the process is sleep at the priority that can be interrupted, it will wake up the process; otherwise, only the corresponding bit of the signal in the process is set, and the process will not perform the signal without waking up the process ).
3. signal classification:
There are many reasons for sending signals. Here we simply classify the reasons for sending signals to learn about various signals:
(1) signals related to process termination. This type of signal is sent when the process exits or the sub-process is terminated.
(2) signals related to process exception events. For example, if a process crosses the border, attempts to write a read-only memory area (such as the program body area), or executes a privileged command and other hardware errors.
(3) signals related to unrecoverable conditions during system calls. For example, when the system calls exec, the original resources have been released, and the system resources have been exhausted.
(4) signals related to non-predicted error conditions during system calls. For example, execute a non-existing system call.
(5) signals sent by processes in user mode. For example, a process call system calls kill to send signals to other processes.
(6) signals related to terminal interaction. For example, you can close a terminal or press the Break Key.
(7) signal of process execution.
4
Signal processing method:Processes that receive signals have different processing methods for various signals. The processing method can be divided into three types: the first method is to define a function pointer (prototype: void (*) (INT) as a processing function to process the signal to be processed. The second method is to ignore a signal through the macro sig_ign (defined as a function pointer in signal. h) and do not process the signal, just as it has never happened. The third method is to use sig_dfl (defined in signal. A function pointer in H) retains the default value of the system for processing the signal. This default operation causes the process to terminate most of the default operations on the signal.
5. Signal Processing Process
For a complete signal Life Cycle (after the signal is sent to the corresponding processing function for execution), it can be divided into three phases:
SIGNAL INSTALLATION
Signal Generation
Signal execution
5.1 signal installation (set signal Association)
If a process needs to process a signal, it must be installed in the process. The installation signal is mainly used to determine the ing between the signal value and the action of the process against the signal value, that is, the signal to be processed by the process, and the operation to be performed when the signal is transmitted to the process.
Linux mainly has two function implementation signals: Signal () and sigaction ().
Signal () is implemented based on reliable signal system calls and is a database function. It has only two parameters and does not support signal transmission information. It is mainly used for the installation of the first 32 non-real-time signals;
While sigaction () is a newer function (implemented by two systems: sys_signal and sys_rt_sigaction), it has three parameters that support signal transmission information, mainly used with sigqueue () system calls are used together. Of course, sigaction () also supports installation of non-real-time signals. Sigaction () is superior to signal () mainly because it supports signal parameters.
5.2 signal generation there are two sources of signal events: hardware sources (for example, we press the keyboard or other hardware faults); software sources, the most common system functions for sending signals include: Kill (), raise (), sigqueue (), alarm (), setitimer (), and abort ()., Software sources also include some illegal operations.