Reprinted: http://blog.chinaunix.net/uid-1877180-id-3011232.html
SigactionThe function is used to check or modify the processing actions associated with the specified signal (two operations can be performed at the same time ).
It is a POSIX signal interface, while signal () is a standard C signal interface (this interface should be used if the program must run on a non-POSIX System)
Set a new signal processing function act for the signal SIGNUM, and retain the original signal processing function oldact of the signal.
IntSigaction(INT signo, const structSigaction* Restrict act, StructSigaction* Restrict oact ); |
StructureSigactionDefinition:
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 specifies a signal set. before calling the signal capturing function, this signal set must be added to the process's signal shielding characters. Only when the function is returned from the signal capture function, the signal shielding character of the process is reset to the original value.
Sa_flag is an option, mainly understanding two
Sa_interrupt the system calls that are interrupted by the signal will not be restarted automatically. Sa_restart the system calls that are interrupted by the signal will be automatically restarted. 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 processing program. It is used only when sa_siginfo is set.
Example:
# Include <stdio. h>
# Include <signal. h>
# Include <unistd. h>
Void show_handler (INT sig)
{
Printf ("I got signal % d \ n", sig );
Int I;
For (I = 0; I <5; I ++ ){
Printf ("I = % d \ n", I );
Sleep (1 );
}
}
Int main (void)
{
Int I = 0;
StructSigactionAct, oldact;
Act. sa_handler = show_handler;
Sigaddset (& act. sa_mask, sigquit); // See note (1)
Act. sa_flags = sa_resethand | sa_nodefer; // See note (2)
// Act. sa_flags = 0; // See note (3)
Sigaction(SIGINT, & act, & oldact );
While (1 ){
Sleep (1 );
Printf ("Sleeping % d \ n", I );
I ++;
}
}
Note:
(1) If the SIGINT (CTRL + C) signal processing function show_handler is executed, the process will block the signal when it receives the sigquit (CRT + \) signal, signal sigquit will not be processed until the show_handler execution ends.
(2)Sa_nodeferGenerally, when the signal processing function is running, the kernel will block <the given signal -- SIGINT>. However, if the sa_nodefer flag is set, the kernel will not block the signal when the signal processing function is running. Sa_nodefer is the formal POSIX name of this tag (there is also a name sa_nomask, which is generally not used for software portability)
Sa_resethandWhen the signal processing function is called, the signal processing function is reset to the default value. Sa_resethand is the formal POSIX name of this tag (there is also a name sa_oneshot, which is generally not used for software portability)
(3) If you do not need to reset the processing function of the given signal to the default value, and do not need to block the given signal (you do not need to set the sa_flags flag), you must reset sa_flags, otherwise, a segment error occurs. However, after sa_flags is cleared, the signal may be lost!
Http://webcache.googleusercontent.com/search? Q = cache: b2hsd1zf2f8j: bytes
Sigaction function Parsing