Advanced Programming in UNIX environment: Signal Processing

Source: Internet
Author: User

The book "Advanced Programming in UNIX environments" comes with many small and exquisiteProgramWhen I read this bookCodeI have rewritten it according to my own understanding (most of it is on the copybook) and deepened my understanding (it is too difficult to read a book, haha ). This example is successfully tested on ubuntu10.04.


Program Introduction: in UNIX environment, we can let the program shield some signals (except sigkill signals and sigstop). This example demonstrates this function.


// Apue program 10-11: Signal Processing and sigprocmask instance # include <unistd. h> # include <stdio. h> # include <stdlib. h> # include <signal. h> // output the error message and exit void error_quit (const char * Str) {fprintf (stderr, "% s \ n", STR); exit (1 );} // void sig_quit (INT signo) {printf ("caught sigquit \ n"); If (sig_err = signal (sigquit, sig_dfl )) error_quit ("can't reset sigquit");} int main (void) {sigset_t newmask, oldmask, pendmask; // set the processing function for sigquit signals if (sig_err = signal (sigquit, sig_quit) error_quit ("can't catch sigquit "); // initialize the newmask signal set to an empty sigemptyset (& newmask); // Add the sigquit signal sigaddset (& newmask, sigquit) in the newmask signal set; int temp; // set oldmask to the current blocked Signal Set (for future recovery) // Add newmask signal set temp = sigprocmask (sig_block, & newmask, & oldmask); If (temp <0) error_quit ("sig_block error"); sleep (5); // returns the current blocked signal set temp = sigpending (& pendmask ); if (temp <0) error_quit ("sigpending error"); // check whether the sigquit signal is in the pendmask signal set if (sigismember (& pendmask, sigquit )) printf ("\ nsigquit pending \ n"); // restores the blocked signal set temp = sigprocmask (sig_setmask, & oldmask, null); If (temp <0) error_quit ("sig_setmask error"); printf ("sigquit unblocked \ n"); sleep (5); Return 0 ;}

Running example:

 
Qch @ Ubuntu :~ /Code $ GCC temp. C-o tempqch @ Ubuntu :~ /Code $. /temp ^ \ # generate a sigquit signal sigquit pending # Caught sigquit after returning from sleep # sigquit unblocked in the signal processing program # exit ^ \ after returning from sigpromask # generate a signal qch again @ ubuntu: ~ /Code $. /temp # run the program again ^ \ # generate multiple sigquit signals sigquit pendingcaught sigquit # receive only one signal sigquit unblocked ^ \ quit

Conclusion:

1: The process blocks the sigquit signal. The sigquit signal generated during this period will be blocked until the signal is no longer blocked.
2: If a signal is generated during sleep, the signal is pending but not blocked (so the sig_quit function can be called before exiting)
3: The system does not queue the signal when the signal is blocked.

Note (This section is excerpted from the Internet ):
The "pending" Status of the signal refers to the period from the generation of the signal to the time before the signal is processed. The "blocking" Status of the signal is a switching action, it indicates that the signal is blocked but not generated.
The example of apue blocks the exit signal with sigprocmask before sleep, then sleep, and generates an exit signal during sleep, but the exit signal is blocked at this time, ("blocking" in Chinese is easy to be misunderstood as a State. It is actually a switch-like action, so "blocked" rather than "blocked ") so it is in the pending status. After sleep, use sigprocmask to turn off the blocking switch of the exit signal, because the exit signal generated previously remains in the pending status. When the blocking switch is closed, exit the "pending" status immediately and get processed. This happens before sigprocmask returns.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.