Sigsuspend function of Signals

Source: Internet
Author: User

Changing the signal shielding characters of a process can block the selected signal or lift the blocking on them. Using this technology can protect code critical zones that do not require signal interruption. What if I want to unblock a signal and then wait for the previously blocked signal to happen with pause? Assume that the SIGINT is used when the signal is sent. An incorrect method to achieve this is:

&&(sigprocmask(SIG_BLOCK, &newmask, &oldmask) < (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 

If the signal is sent to the process when the signal is blocked, the transmission of the signal is postponed until the blocking is lifted. For the application, this signal seems to occur between the block to the SIGINT and the pause. If this happens, or if there is a signal between the unblocking moment and pause, the problem arises. Because we may not see this signal again, in this sense, the signal in this time window (between unblocking and pause) is lost, so that the pause will always block.

To correct this problem, you mustRecover the signal shielding characters in an atomic operation and then sleep the process.. This function is provided by the sigsuspend function.

# Include <signal. h> sigsuspend (sigset_t *-, and set errno to EINTR

Sig_int (signal (SIGINT, sig_int) ==&& (sigprocmask (SIG_BLOCK, & newmask, & oldmask) <(sigsuspend (& waitmask )! =-(Sigprocmask (SIG_SETMASK, & oldmask, NULL) <

The result of running the program is as follows:

Sig_atomic_t quitflag; signo) signal (SIGINT, sig_int); signal (SIGQUIT, sig_int );(Signo =; (signal (SIGINT, sig_int) = (signal (SIGQUIT, sig_int) = & (sigprocmask (SIG_BLOCK, & newmask, & oldmask) <(quitflag ==& = (sigprocmask (SIG_SETMASK, & oldmask, NULL) <

Note: The two lines marked as red in the program listing 10-16 are not included in the UNIX environment advanced programming, but are not included, in my running environment (Red Hat Linux 2.6.18), I cannot get the same running result as in the book:

Running result in the book (you can repeatedly type the interrupt character ):

Sig_atomic_t sigflag; signo) = (signal (SIGUSR1, sig_usr) = (signal (SIGUSR2, sig_usr) ==&& & (sigprocmask (SIG_BLOCK, & newmask, & oldmask) <(sigflag ==& zeromask); = (sigprocmask (SIG_SETMASK, & oldmask, NULL) <(sigflag ==& zeromask); = (sigprocmask (SIG_SETMASK, & oldmask, NULL) <

Two user-defined signals are used: SIGUSR1 is sent to the child process by the parent process, and SIGUSR2 is sent to the parent process by the child process.

If you want to sleep when waiting for the signal to happen, it is very suitable to use the sigsuspend function.But what if I want to call other system functions while waiting for the signal? Unfortunately, there is no proper solution to this problem in a single-threaded environment. If multithreading can be used, a thread can be specially arranged to process signals.

If the thread is not used, what we can do best is to set 1 for a global variable in the signal capturing program when a signal occurs.

 

This blog is excerpted from advanced programming for UNIX environments (version 2) and used only for personal learning records. For more information about this book, see:Http://www.apuebook.com/.

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.