http://blog.csdn.net/ta893115871/article/details/7475095
Signal () function describes Linux functions in detail
Signal () function understanding
In the <signal.h> header file.
Signal (parameter 1, parameter 2);
Parameter 1: The signal we are going to process. The signal of the system we can again terminal type Kill-l view (total 64). Actually these signals are the system-defined macros.
Parameter 2: How we handle it (whether the system is default or ignored or captured).
Generally, there are 3 ways to operate.
(1) eg:signal (SIGINT, sig_ing);
Sig_ing represents ignoring SIGINT signals, SIGINT signals are generated by Interruptkey, usually ctrl +c or delete. The process that is sent to all foreground group.
Here we write a dead loop:
Then we save the execution.
Press CTRL _c The program does not respond. That's right
If we want to end the program you can press CTRL +\ to end
Actually, when we press CTRL +\, the Sigquit signal is generated.
(2) eg:signal (SIGINT, SIG_DFL);
SIGINT signals are generated by Interruptkey, usually ctrl +c or delete. The process that is sent to all foregroundgroup. The SIG_DFL represents the system default action, which in fact terminates the process for most signals when the system default action is performed. This is the same as not writing this handler function.
We're going to change the procedure above
You can then press CTRL +C to terminate the process. Put signal (SIGINT,SIG_DFL); this sentence is removed, the effect is the same.
(3) void (*signal (int sig, void (* handler) (int))) (int);
Int (*p) ();
This is a function pointer, and p points to a function that has no arguments and returns a value of int.
Int (*fun ()) ();
The difference between this and the above is that the fun () instead of P, and fun () is a function, so it can be considered as fun () after the function is executed, its return value is a function pointer, the function pointer (in fact, the p above) is a function that points to no parameters, And a function that returns a value of int.
void (*signal (int signo, void (*handler))) (int)), which can be seen as a signal () function (which itself is a function with two parameters, an integer type, and a function pointer), and this signal () The return value of a function is also a function pointer, which points to a function with an integer parameter and a return value of void.
The function for signal processing when writing a signal processing function is also void sig_fun (int signo), which is exactly the same as the function pointer that is returned by the signal () function above. void (*signal ()) (int);
Signal is a function that returns a function pointer to a function that takes an integer parameter and does not return a value, looking closely at the 2nd argument of the siganal (int signo, void (*handler) (int)), and the fact that he is returning the S Ignal 2nd signal processing function, point to the signal processing function, you can execute the function (signal internal, signal the signal as a parameter to the handler signal processing function, and then signal function return pointer, and then point to the signal handler function, start to execute it)
So what about the parameters of the signal function? The signal function accepts two parameters: an integer signal number, and a pointer to a user-defined signal handler function. We have previously defined a pointer SFP that points to a user-defined signal handler function:
The type of SFP can be obtained by removing the SFP from the above declaration, Void (*) (int). In addition, the return value of the signal function is a pointer to the user-defined signal handler function before the call, and the type of the pointer is consistent with the type of the SFP pointer. Therefore, we can declare the signal function as follows:
void (*signal (int, void (*) (int)))) (int); |
Similarly, using typedef can simplify the function declaration above:
typedef void (*handler) (int); HANDLER signal (int, HANDLER); |
Ok; Look at an example:
This program is for when we press the CTRL +C key, the signal handler function we define is executed.
The number of the signal is printed every time we press the CTRL +c key. It can be seen that the signal num is 2
To exit you can press CTRL +\ to print the result as the last line.
Some of the commonly used signal are as follows:
Note: The following is from the Baidu Library Search (*^__^*) hehe ...
Signal |
Description |
Sigabrt |
Generated by calling the Abort function, the process is not properly exited |
Sigalrm |
A timer timeout or Setitimer function set with the alarm function sets the interval timer timeout |
Sigbus |
A specific hardware exception, usually caused by memory access |
Sigcancel |
Used internally by the Solaris Thread Library and typically does not use |
SIGCHLD |
When a process terminate or stops, SIGCHLD sends it to its parent process. By default, the signal is ignored |
Sigcont |
When the stop process resumes running, it is automatically sent |
Sigemt |
and implementation-related hardware exceptions |
SIGFPE |
Math-related anomalies, such as being removed by 0, floating-point overflow, etc. |
Sigfreeze |
Solaris dedicated, hiberate or suspended when sent |
SIGHUP |
Sent to controlling Process with terminal when terminal is disconnect |
Sigill |
Illegal instruction exception |
Siginfo |
BSD signal. Generated by status key, usually ctrl+t. Processes that are sent to all foreground group |
SIGINT |
Generated by interrupt key, usually CTRL + C or delete. Processes that are sent to all foreground group |
SIGIO |
Asynchronous IO Events |
Sigiot |
Implementation of related hardware exceptions, general correspondence SIGABRT |
SIGKILL |
cannot be processed and ignored. Abort a process |
Siglwp |
Used internally by the Solaris Thread Libray |
Sigpipe |
Sent when the pipe was written after reader abort |
Sigpoll |
Send when an event is sent to pollable device |
Sigprof |
Setitimer the specified profiling Interval timer is generated |
Sigpwr |
and system-related. and UPS-related. |
Sigquit |
When you enter Quit key (ctrl+\), the process is sent to all foreground group |
SIGSEGV |
Illegal memory access |
Sigstkflt |
Linux dedicated, math coprocessor stack exception |
SIGSTOP |
Aborts the process. cannot be processed and ignored. |
Sigsys |
Illegal system calls |
SIGTERM |
Request abort process, kill command sent by default |
Sigthaw |
Solaris dedicated, sent from suspend recovery time |
SIGTRAP |
Implements the associated hardware exception. debugging exceptions are generally |
Sigtstp |
Suspend Key, usually Ctrl + Z. Processes that are sent to all foreground group |
Sigttin |
Sent when background group's process attempts to read terminal |
Sigttou |
Sent when background group's process tries to write terminal |
Sigurg |
may be sent when Out-of-band data is received |
SIGUSR1 |
User-definable signal 1 |
SIGUSR2 |
User-definable Signal 2 |
Sigvtalrm |
Virtual Interval Timer Timeout when Setitimer function is set |
Sigwaiting |
Solaris Thread Library Internal Implementation-specific |
Sigwinch |
All processes sent to foreground group when the terminal's window size changes |
Sigxcpu |
When the CPU time limit expires |
Sigxfsz |
Process exceeds file size limit |
Sigxres |
Solaris dedicated, sending when process exceeds resource limit |
Signal () function describes Linux functions in detail