The function of the sigaction function is to inspect or modify the processing action associated with the specified signal (both operations can be done simultaneously).
He is the POSIX signal interface, and signal () is the standard C signal interface (this interface should be used if the program must be running on a non-POSIX system)
A new signal processing function is set for the Signal Signum Act, while preserving the original signal processing function of the signal oldact
intsigaction(int signo,conststruct sigaction*restrict Act, struct sigaction*restrict oact); |
The structure sigaction is defined as follows:
structsigaction{ void (*sa_handler) (int); sigset_t Sa_mask; int Sa_flag; void (*sa_sigaction) (int,siginfo_t*,void*); }; |
The Sa_handler field contains the address of a signal capture function
The Sa_mask field describes a set of signals that is added to the signal screen word of the process before the signal capture function is called. The signal screen word of the process is reset to its original value only when it is returned from the signal capture function.
Sa_flag is an option that mainly understands two
Sa_nodefer: When the signal processing function is in progress, do not block the signal processing function itself signal function. Sa_resethand: When the signal processing function of the user registration is executed once, the processing function of the signal is set to the system default handler function. Sa_siginfo provides additional information, a pointer to the SIGINFO structure, and a pointer to the process context identifier |
The last parameter is an alternative signal handler that will be used when setting sa_siginfo.
Examples of use are:
[HTML]View Plaincopy
- #include <stdio.h>
- #include <signal.h>
- void wrkprocess (int nsig)
- {
- printf ("Wrkprocess. I get signal.%d threadid:%d/n ", nsig,pthread_self ());
- int i=0;
- while (I<5) {
- printf ("%d/n", I);
- Sleep (1);
- i++;
- }
- }
- int main ()
- {
- struct Sigaction act,oldact;
- Act.sa_handler = wrkprocess;
- Sigaddset (&act.sa_mask,sigquit);
- Sigaddset (&act.sa_mask,sigterm)
- act.sa_flags = Sa_nodefer | Sa_resethand;
- act.sa_flags = 0;
- Sigaction (sigint,&act,&oldact);
- printf ("Main threadid:%d/n", Pthread_self ());
- while (1) sleep (5);
- return 0;
- }
1) When the program is executed, CTRL + C, the first time will not cause the end of the program. Instead, it continues to execute, and when the user executes CTRL + C again, the program takes over.
2) If you make a slight change to the program, there will be another case.
Changed to: Act.sa_flags = Sa_nodefer;
After this change, no matter how many times the ctrl+d operation, the program will not end.
3) The third case occurs if you make another change to the program.
for example:act.sa_flags = 0;
During the execution of the signal processing function, the operation of CTRL + C, the program will not call the signal processing function, but after the completion of this signal processing function, in the execution of a signal processing function (regardless of the number of times before the CTRL + C signal generated).
If the signal processing function is executed during 2, the signal processing function is called again when the CTRL + C signal is given again.
4) if Sigaddset (&act.sa_mask,sigquit) is set in the program, the program sends the ctrl+/signal during the execution of the signal processing function, and the program does not exit. Instead, the signal processing function of the sigquit is executed after the signal processing function is executed, and then the program exits. If you do not add this setting, the program will perform an exit immediately after receiving the ctrl+/signal, whether or not it is in the process of the signal handler for CTRL + C.
The reasons are as follows:
1) In the case, the first time the CTRL + C signal is generated, the signal is set by its own signal processing function to be processed. In the process, because we set the SA_RESETHAND flag bit, and the signal processing function is set as the default signal processing function (the system default processing mode is IGN), so the second time the ctrl+d signal is sent by the default signal processing function, resulting in the end of the program;
2) In the case, we removed the Sa_resethand flag bit, resulting in the program all the CTRL+D signal is processed by our own signal processing function, so we send how many times the CTRL + C signal program will not quit;
3) We have removed the SA_NODEFER flag bit. During the execution of the signal processing function, the CTRL + C signal will be blocked, but the CTRL + C signal sent during the execution of the signal processing function will be blocked, knowing that the signal processing function is completed to be able to handle the CTRL + C generated during the signal function execution. However, when the signal function is executed multiple times CTRL + C, the result is only CTRL + C. 2) case, the sa_nodef,ctrl+c signal will not be blocked because it is set. So the next signal processing function can be executed in parallel.
4) In the case, we are setting the signal processing function in the process, we will block the signal, when the processing function to block the signal is completed before processing the signal.
Report:
When we press CTRL + C, the operation is: Send SIGINT signal to the system, SIGINT the default processing of the signal, exit the program.
When we press ctrl+/, the operation is: send the sigquit signal to the system, the default processing of this signal is to exit the program.
from:http://blog.csdn.net/jiang1013nan/article/details/5409684
Reference: sigaction function parsing
Http://blog.chinaunix.net/uid-1877180-id-3011232.html
Sigaction usage Examples