Comprehensive Case
1) Create child process and parent process;
2) Register SIGINT non-real-time signal and sigrtmin real-time signal, and add these two signals to the process shielding signal Group;
3) Register user-defined signals;
4) The sub-process sends 5 non-real-time signals and sends 5 real-time signals;
5) The sub-process then sends the SIGUSR1 to unblock the sigint,sigtrmin signal.
6) Observe the difference between real-time and non-real-time signals
program example void onsigaction (int signalnumber, siginfo_t *siginfostruct, void *) {//Get received data int receivenumber = Siginfost ruct->si_int; If the SIGUSR1 signal is received, the Sigint,sigrtmin shield if (signalnumber = = SIGUSR1) {sigset_t unblockset is lifted; Sigemptyset (&unblockset); Sigaddset (&unblockset,sigint); Sigaddset (&unblockset,sigrtmin); Sigprocmask (Sig_unblock,&unblockset,null); Value will be garbled! cout << "Receive SIGUSR1, Value =" << receivenumber << Endl; cout << "Unblock, Receive SIGUSR1" << Endl; } else if (Signalnumber = = SIGINT) {cout << Receive SIGINT, Value = "<< receivenumber << Endl } else if (Signalnumber = = sigrtmin) {cout << Receive sigrtmin, Value = "<< Receivenumber < < Endl; }}//error exit Function inline void Err_exit (string str); int main () {struct Sigaction act; If you need to allow the signal handler to receive additional data,//the sa_flags position must be sa_siGinfo act.sa_flags = Sa_siginfo; Sigemptyset (&act.sa_mask); Act.sa_sigaction = onsigaction; Register Signal Processing function if (sigaction (Sigint,&act,null) < 0) Err_exit ("Sigaction SIGINT"); if (Sigaction (Sigrtmin,&act,null) < 0) Err_exit ("Sigaction sigrtmin"); if (Sigaction (Sigusr1,&act,null) < 0) Err_exit ("Sigaction SIGUSR1"); Blocking the signal sigint,sigrtmin signal sigset_t blockSet; Sigemptyset (&blockset); Sigaddset (&blockset,sigint); Sigaddset (&blockset,sigrtmin); if (Sigprocmask (Sig_block,&blockset,null) < 0) err_exit ("Sigprocmask error"); pid_t pid = fork (); if (PID = =-1) err_exit ("fork Error"); else if (PID = = 0) {Union sigval Value; Value.sival_int = 200; Send a non-reliable signal to the parent process five times with extra data (in fact only once) for (int i = 0; i < 5; ++i) {+ + Value.sival_int; if (Sigqueue (Getppid (), sigint,value)! = 0) err_exit ("Sigqueue error"); }//sends a reliable signal to the parent process five times with additional data (five last received!!!) Value.sival_int = 0; for (int i = 0; i < 5; ++i) {+ + Value.sival_int; if (Sigqueue (Getppid (), sigrtmin,value)! = 0) err_exit ("Sigqueue error"); }//sends a SIGUSR1 signal to the parent process, lifting the sigint,sigrtmin signal blocking Kill (Getppid (), SIGUSR1); } while (true) {pause (); }}void err_exit (String str) {perror (Str.c_str ()); Exit (exit_failure);}
Operation result :
Actually, I just received a non-reliable signal sigint!
Attached-View System restrictions command: Ulimit
[Email protected]:~$ ulimit-acore File size (blocks,-c) unlimiteddata seg size (Kbytes,-D) unlimitedscheduling Priority (-e) 0file size (blocks,-f) unlimitedpending signals (-I ) 47131max locked memory (Kbytes, L) 64max Memory Size (Kbytes,-m) unlimitedopen files (-N) 1024pipe size (bytes,-p) 8POSIX message Queues
(bytes,-Q) 819200real-time priority (-R) 0stack size (Kbytes,-s) 8192cpu time (seconds,-t) Unlimitedmax User Processes (-u) 47131virtual memory (Kbytes,-V) unlimitedfile locks (-X) Unlimited
Linux reliable/unreliable signal programming practice