Program Description: In the Unix environment, we can let the program mask some signals (except SIGKILL signal and SIGSTOP). This example demonstrates this function.
//"APUE" Program 10-11: Signal Processing and sigprocmask Instances #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <signal .h> //Export error message and exit void error_quit(const char *str) { fprintf(stderr, "%s\ ", str); exit(1); } //Processing function of SIGQUIT signal void sig_quit( Int signo) { printf("caught SIGQUIT\ "); if( SIG_ERR == signal(SIGQUIT, SIG_DFL) ) error_quit("can't reset SIGQUIT"); } int main(void) { sigset_t newmask, oldmask , pendmask; //Set the handler for the SIGQUIT signal if( SIG_ERR == signal(SIGQUIT, sig_quit) ) error_quit("can't catch SIGQUIT"); //Initialize the newmask signal set to empty sigemptyset(&newmask ); //In the newmask signal set, add SIGQUIT signal sigaddset(&newmask, SIGQUIT); int temp; //Set oldmask to the current masked signal set (for later recovery) //Add newmask signal to the current mask signal Set temp = sigprocmask(SIG_BLOCK, &newmask, &oldmask); if( temp & Lt; 0) error_quit("SIG_BLOCK error"); sleep(5); //returns the current masked signal set temp = sigpending(&pendmask); if( temp < 0 ) error_quit("sigpending error") //Check if the SIGQUIT signal is in the pendmask signal set if( sigismember(&pendmask, SIGQUIT) ) printf("\ SIGQUIT pending\ "); //Recover the masked signal set temp = sigprocmask(SIG_SETMASK, &oldmask, NULL); if( temp < 0 ) error_quit("SIG_SETMASK error"); printf("SIGQUIT unblocked\ "); sleep(5); return 0; } Run example:
www. Linuxidc.com @ubuntu:~/code$ gcc temp.c -o temp www.linuxidc.com @ubuntu:~/code$ ./temp ^\\ #Generate a SIGQUIT signal SIGQUIT pending #from sleep after catch SIGQUIT #在SIGQUIT unblocked in the signal processing program # sigpromask returns ^\\ quit # again generates the signal www.linuxidc.com @ubuntu:~/code$ ./temp #Run the program again ^\\^\\^\\^\\^\\^\\^ \\^\\^\\^\\^\\^\\ #Generate multiple times SIGQUIT signal SIGQUIT pending caught SIGQUIT # only receive one signal SIGQUIT unblocked ^ \\Exit
Conclusion:
1: The process blocks the SIGQUIT signal, during which the SIGQUIT signal generated is blocked until the signal is no longer blocked.
2: During sleep, if a signal is generated, the signal at this time is pending, but it is no longer blocked (so the sig_quit function can be called before exiting)
3: The system does not queue the signal while the signal is blocked.
Notes (this passage is excerpted from the Internet):
Signal';Pending" is a state that refers to the generation of signals before the signal is processed. This period of time; the signal & rdquo; blocking & ldquo; is a switching action, which means that the blocking signal is processed, but not to prevent the signal from being generated.
APUE example uses sigprocmask to block the exit signal before sleep, then sleep, and then generates an exit signal during the sleep process, but the exit signal is blocked at this time (Chinese & rdquo; blocking & rdquo; It is easy to be misunderstood as a state here, in fact it is a kind of action similar to a switch, so it is said that "blocked" is blocked, instead of "blocked", so it is in the state of "pending" After sleep, use sigprocmask to turn off the blocking switch of the exit signal, because the exit signal generated before is always in the pending state. When the blocking switch is closed, it immediately exits the status of "Pending" and is processed. This happens in sigprocmask. prior to.
date command The function of the date command is to display and set the system date and time. The ge
IAP, the full name is In-Application Programming, Chinese is interpreted as Programming in the prog
Today I made a simple ftp server for the company, and it was the first time I actually did the serv
Managing the performance of a Linux host often seems like magic. Many administrat
Haproxy Installation Configuration Tuning Basics Tutorial
The reason why the driver of the single-chip microcomputer should be set to low level is
Source code reading tool under Windows platform Source Insight
Linux df command parameters detailed
Error:storage class specified for parameter problem
Synchronization in the Linux kernel Knowledge
Linux file name garbled conversion basic tutorial
What is the difference between Linux and BSD?
How to modify the shutdown interface of Windows XP system?
XP prohibits others from modifying file attributes
How can the win7 system not find the input method?
CF November veterans return to the battlefield, blow the offensive assembly number
Win 7 system desktop window side by side shortcut skills
Reinstalling is not troublesome Backup Win XP system activation information
Solve the chassis front panel headphones can not be used
Win7 prompts "The display driver has stopped responding and has been restored" What should I do?
How to adjust the Win10 screen refresh rate Win10 monitor setting screen refresh rate method
Win7/win8.1 After upgrading the win10 system, do you still have to pay annually?