Author:Sun Xiaoming, a lecturer at Huaqing vision embedded College.
On UNIX, there are many communication methods between processes. In the early days, there were unknown pipelines, famous pipelines and signals, and later I introduced IPC objects and sockets. Signals are a simulation of the interrupt mechanism at the software level and an asynchronous communication method. The signal can directly interact between the user space process and the kernel process, and the kernel process can also be used to notify the user
System events that occur to a space process. There are three processing methods for user processes to connect to received signals:
Default processing: Linux defines default operations for each signal.
Ignore signal: Do not process the received signal
Capture signals: pre-defined signal processing functions. Execute corresponding processing functions when receiving signals
Note: Only default operations can be performed for signal sigkill/sigstop.
In earlier Unix systems, only 32 types of signals were defined. Linux supports 64 types of signals, ranging from 1 to 64 (sigrtmin = 34, sigrtmax = 64 ). The first 32 signals have predefined values. Each signal has a definite purpose and meaning, and each signal has its own default action. The following signals represent real-time signals, which are part of the POSIX standard and can be used in application processes.
Non-real-time signals do not support queuing and are unreliable signals. Real-time signals support queuing and are reliable signals.
Functions related to sending signals are as follows:
1. # include <sys/types. h>
# Include <signal. h>
Int kill (pid_t PID, int signo); // sends a signal to the specified process/Process Group
2. # include <signal. h>
Int raise (INT signo); // The process sends a signal to itself
3. # include <unistd. h>
Unsigned int alarm (unsigned int seconds); // sets the timer to send the sigalrm signal to the process at that time.
4. # include <stdlib. h>
Void abort (void); // sends a sigabort signal to the process. By default, the process exits abnormally.
5. # include <sys/types. h>
# Include <signal. h>
Int sigqueue (pid_t PID, int Sig, const Union sigval Val); // sigqueue () is a relatively new sending signal system call, it is mainly proposed for real-time signals (of course the first 32 types are also supported). It supports signals with parameters and works with the function sigaction ().
Functions related to signal settings are as follows:
If you want the process to capture the processing signal, you need to set the signal processing method in the process. That is, what operations are required when a process receives a signal. In Linux, there are two main function signal settings: Signal () and sigaction (). The signal () function has two parameters and does not support signal transmission information. It is mainly used for processing the first 32 non-real-time signals. The sigaction () function has three parameters, it supports signal delivery information and is mainly used in combination with sigqueue () system calls. Sigaction () also supports non-real-time signal settings. For space purposes, we will only introduce the use of the signal function.
# Include <signal. h>
Void (* signal (INT Sig, void (* func) (INT)
Signal () has two parameters: the first parameter is the type of the signal to be set; the second parameter is a function pointer, which stores the entry address of the signal processing function. The return value of a function is also a function pointer, and its value is the processing method before the setting. For example:
For example, to capture the SIGINT signal (this signal is generated when you press Ctrl-c), define the signal processing function first.
Void signal_handler (INT signo)
{
If (signo = SIGINT)
{
Printf ("We caught signal SIGINT/N ");
}
}
Then set the signal processing method in the main () function.
Int main (INT argc, char * argv [])
{
......
Signal (SIGINT, signal_handler );
......
}
After the settings are complete, the program continues to execute. When the SIGINT signal arrives, the program will execute the function signal_handler. After the function is executed, the program is interrupted and continues to run.
Note: Multiple signals can share the same signal processing function. In the function, they are processed separately based on the signal type (parameters passed to the signal processing function. If you want to pass other parameters to the signal processing function, you can only set them using sigaction.