Header file # include <signal. h>
Function: Set the corresponding action of a signal.
Function prototype: void (* signal (int signum, void (* handler) (int );
Or: typedef void (* sig_t) (int );
Sig_t signal (int signum, sig_t handler );
Parameter description:
The first parameter signum specifies the signal type to be processed. It can take any signal Except SIGKILL and SIGSTOP.
The second handler describes the action associated with the signal. It can take the following three values:
(1) A function address without return values
This function must be stated before signal () is called, and handler is the name of this function. When a signal of sig type is received, the function specified by handler is executed. This function should be defined as follows:
Void func (int sig );
Sig is the unique parameter passed to it. After a signal () call is executed, the process immediately executes the func () function no matter which part of the program it is executing receives a signal of the sig type. When the execution of the func () function ends, the control returns the point where the process is interrupted and continues to be executed.
(2) SIG_IGN
This symbol indicates that the signal is ignored. After the corresponding signal () call is executed, the process ignores signals of the sig type.
(3) SIG_DFL
This symbol indicates the default signal processing by the recovery system.
Function Description:
Signal () sets the signal processing function based on the signal number specified by the signum parameter. When the specified signal arrives, the function execution specified by handler is redirected. When a signal processing function is executed, if the process receives the signal again, the signal will be automatically stored without interrupting the execution of the signal processing function, after the signal processing function is executed, call the corresponding processing function again. However, if the process receives other types of signals during signal processing function execution, the execution of the function will be interrupted.
Return Value: returns the pointer to the previous signal processing function. If an error occurs, returns SIG_ERR (-1 ).
The following situations can generate Signal:
Press CTRL + C to generate SIGINT
Hardware interruption, such as Division 0 and illegal memory access (SIGSEV)
The Kill function can send Signal to a process.
Kill command. It is actually a packaging of the Kill function.
Software interruption. For example, when the Alarm Clock times out (SIGURG), when the Reader stops, it writes data (SIGPIPE) to the pipeline, and so on.