APUE: Signal Processing

  
 

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.

Copyright © Windows knowledge All Rights Reserved