One, what is the signal
With Windows we know that when we do not end a program properly, we can force the task Manager to end the process, but how does this actually happen? The same functionality is done on Linux by generating signals and capturing signals, and the running process captures the signal and then makes a certain operation and ends up being terminated.
Signals are an event generated by UNIX and Linux systems in response to certain conditions, and the process of receiving the signal takes some action accordingly. Usually the signal is generated by an error. But they can also be a way of communicating or modifying behavior between processes, explicitly sending a process to another process. A signal is generated called a generation, receiving a signal called capture.
Ii. Types of Signals
The name of the signal is defined in the header file signal.h, the signal starts with the sig, the commonly used signal is not many, the commonly used signal is as follows:
More signal types can be viewed in the Appendix table.
Third, signal processing--signal function
A program can use the signal function to handle a specified signal, primarily by ignoring and restoring its default behavior. The prototype of the signal function is as follows:
#include <signal.h>
void (*signal (int sig, Void (*FUNC) (int))) (int);
This is a rather complicated statement, so be patient. Signal is a function with sig and Func two parameters, Func is a function pointer of type void (*) (int). The function returns a pointer of the same type as the Func, pointing to the function pointer of the previously specified signal-processing function. The parameters of the signal to be captured are given by SIG, and the function to be invoked after the specified signal is received is given by the parameter func. In fact, the use of this function is quite simple, through the following examples can be known. Note that the prototype of the signal processing function must be a void func (int), or the following special value:
Sig_ign: Ignoring the signal
SIG_DFL: Restore the default behavior of the signal
Say so much, or give an example to explain it, the source file name is signal1.c, the code is as follows:
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
void ouch (int sig)
{
printf ("\nouch! -I got signal%d\n ", SIG);
Restores the default behavior
(void) signal (SIGINT, SIG_DFL) of the terminal interrupt signal SIGINT;
int main ()
{
//change the default behavior of terminal interrupt signal SIGINT to perform ouch function
//instead of terminating program execution
(void) signal (SIGINT, ouch);
while (1)
{
printf ("Hello world!\n");
Sleep (1);
}
return 0;
}
The results of the operation are as follows:
As you can see, the first time you press the Terminate command (CTRL + C), the process is not terminated, and the face is output ouch! -I got signal 2, because the default behavior of SIGINT is changed by the signal function, when the process receives the signal SIGINT, it calls the function ouch to handle, note that the OUCH function changes the signal SIGINT processing mode to the default way, So when you press CTRL + C again, the process is terminated as before.