sigaction
#include <signal.h>int sigaction (int signum, const struct sigaction *act, struct sigaction *oldact);
Function:
The Sigaction function is used to change the behavior of a process after it receives a specific signal.
Parameters
The first parameter is the value of the signal and can be any specific valid signal except Sigkill and sigstop (defining its own processing function for both signals, which will result in a signal installation error)
The second parameter is a pointer to the struct sigaction , which, in the case of the struct sigaction, specifies the processing of a particular signal, which can be null, and the process will handle the signal in the default way
The third parameter, Oldact, points to an object that holds the original processing of the corresponding signal , and can specify Oldact as null.
Return value: Function successfully returned 0, failed to return-1
Sigaction Structural Body
The second parameter is most important, including the processing of the specified signal, the information transmitted by the signal, which functions should be screened out during the execution of the signal processing function, etc.
The struct Sigaction {//signal handler does not accept extra data (compared to obsolete) void (*sa_handler) (int);//signal handlers can accept additional data, and Sigqueue in conjunction with (support signal queuing, signal transmission other information), It is recommended to use void (*sa_sigaction) (int, siginfo_t *, void *); sigset_t Sa_mask; shielding int sa_flags; Indicates the behavior of the signal: Sa_siginfo indicates the ability to accept data void (*sa_restorer) (void); Waste no More};
//simple Example 1: Simple substitution with sigaction signal function void signalaction (int signo, siginfo_t *signalinfo, void *p) {cout << "signal =" << Signo << ", desc:" << strsignal (Signo) << Endl;} int main () {struct Sigaction act; Note: callback function handle Sa_handler, sa_sigaction can only select one!!! Act.sa_handler = Sighandler; Act.sa_sigaction = signalaction; Sigemptyset (&act.sa_mask); act.sa_flags = 0; if (sigaction (sigint,&act,null) = =-1) err_exit ("Sigaction error"); Pause ();}
Simple Example 2: using sigaction to simulate signal function sighandler_t mysignal (int signum, sighandler_t handler) { struct sigaction act; struct sigaction oldact; Act.sa_handler = handler; Sigemptyset (&act.sa_mask); act.sa_flags = 0; if (Sigaction (Signum, &act, &oldact) = =-1) return sig_err; return Oldact.sa_handler;}
Sa_mask Options
SA_MASK Specifies a mask of signals which should be blocked/** specifies a mask for the signal set in which the signal in this mask will be blocked **/(i.e., added to the SI Gnal Mask of the thread in which, the signal handler is invoked)
During execution of the signal handler. In addition, the signal which triggered the handler would be blocked,
Unless the SA_NODEFER flag is used.
/** when executing handler, if the process receives a signal that the sa_mask contains, the signals will not be responded to until the handler function finishes executing
Unlike Sigprocmask, a sigprocmask is a signal screen character that specifies the process to mask the signal, does not receive the signal, and Sa_mask receives the signal, but does not correspond to the signal **/
Example: When the Sa_mask option is set to a later effect/** the corresponding SIGINT signal in the process, if there is a sigquit signal arriving at this time, the sigquit signal will not be responded to, until the SIGINT signal is completed, the corresponding Sigquit signal (as shown in the run ) **/int Main () { struct sigaction act; Act.sa_handler = Sighandler; Sigemptyset (&act.sa_mask); After adding the following line: In response to the SIGINT signal, is not interrupted Sigaddset (&act.sa_mask, sigquit); act.sa_flags = 0; if (Sigaction (SIGINT, &act, NULL) = =-1) err_exit ("sigaction error"); while (true) pause ();} void Sighandler (int signo) { cout << "Catch a signal, Signo =" << signo << ", desc:" << STRs Ignal (Signo) << Endl; Sleep (10);}
Run
Sa_flags: More information
SIGINFO_T structure:
siginfo_t{int Si_signo; /* Signal number */int si_errno; /* An errno value */int si_code; /* Signal code */int si_trapno; /* Trap number that caused hardware-generated signal (Unused on the most architectures) */pid_t si_pid; /* Sending Process ID */uid_t Si_uid; /* Real user ID of sending process */int si_status; /* Exit value or signal */clock_t si_utime; /* User Time consumed */clock_t si_stime; /* System Time consumed */sigval_t si_value; /* Signal value */int si_int; /* POSIX.1B signal */void *si_ptr; /* POSIX.1B signal */int si_overrun; /* Timer overrun count; POSIX.1B timers */int si_timerid; /* Timer ID; POSIX.1B timers */void *si_addr; /* Memory location which caused fault */long si_band; /* Band event (is int in GLIBC 2.3.2 and earlier) */int si_fd; /* File Descriptor */short SI_ADDR_LSB; /* Least significant bit of address (since Linux 2.6.32) */}
Sigqueue
#include <signal.h>int sigqueue (pid_t pid, int sig, Const Union Sigval value);
Function
Sigqueue is a new signaling signalling system called, mainly for real-time signal support signal with parameters , and function sigaction () with the use.
One more parameter than the KILL function:Const Union sigval value (int Kill (pid_t pid, int sig)), so Sigqueue () can pass more information than Kill () , but Sigqueue () can only send a signal to a process and not send a signal to a process group.
Parameters
Parameter 1 is the process ID that specifies the received signal, and parameter 2 determines the signal to be sent;
Parameter 3 is a federated data structure, union Sigval, that specifies the parameters of the signal passing, that is, the 4-byte value that is commonly referred to.
Sigval Consortium
typedef Union sigval{ int sival_int; void *sival_ptr;} sigval_t;
Comprehensive Experiment sigaction+sigqueuevoid onsa_signalaction (int signalnum, siginfo_t *signalinfo, void *p) {cout << signalnum = "<< signalnum << Endl; int recvalue = Signalinfo, si_value.sival_int; Same as int recvalue = Signalinfo, si_int; cout << "Recvvalue =" << recvalue << Endl;} int main () {struct Sigaction act; Sigemptyset (&act.sa_mask); Act.sa_flags = Sa_siginfo; Act.sa_sigaction = onsa_signalaction; if (sigaction (sigint,&act,null) = =-1) err_exit ("Sigaction error"); pid_t pid = fork (); if (PID = =-1) err_exit ("fork Error"); else if (PID = = 0)//in Child {/* typedef union sigval{int Sival_int; void *sival_ptr; } sigval_t; *///union Sigval signalvalue; with lower sigval_t signalvalue; Signalvalue.sival_int = 256; Sleep (2); Send SIGINT signal to Parent process Sigqueue (Getppid (), sigint,signalvalue); } pause (); ReTurn 0;}
Linux signal Practice (4)--reliable signal