Section 10.13. sigpending Function

team bbl


10.13. sigpending Function

The sigpending function returns the set of signals that are blocked from delivery and currently pending for the calling process. The set of signals is returned through the set argument.

 #include <signal.h> int sigpending(sigset_t *set); 

Returns: 0 if OK, 1 on error


Example

Figure 10.15 shows many of the signal features that we've been describing.

The process blocks SIGQUIT, saving its current signal mask (to reset later), and then goes to sleep for 5 seconds. Any occurrence of the quit signal during this period is blocked and won't be delivered until the signal is unblocked. At the end of the 5-second sleep, we check whether the signal is pending and unblock the signal.

Note that we saved the old mask when we blocked the signal. To unblock the signal, we did a SIG_SETMASK of the old mask. Alternatively, we could SIG_UNBLOCK only the signal that we had blocked. Be aware, however, if we write a function that can be called by others and if we need to block a signal in our function, we can't use SIG_UNBLOCK to unblock the signal. In this case, we have to use SIG_SETMASK and reset the signal mask to its prior value, because it's possible that the caller had specifically blocked this signal before calling our function. We'll see an example of this in the system function in Section 10.18.

If we generate the quit signal during this sleep period, the signal is now pending and unblocked, so it is delivered before sigprocmask returns. We'll see this occur because the printf in the signal handler is output before the printf that follows the call to sigprocmask.

The process then goes to sleep for another 5 seconds. If we generate the quit signal during this sleep period, the signal should terminate the process, since we reset the handling of the signal to its default when we caught it. In the following output, the terminal prints ^\ when we input Control-backslash, the terminal quit character:

     $ ./a.out          ^\                       generate signal once (before 5 seconds are up)     SIGQUIT pending          after return from sleep     caught SIGQUIT           in signal handler     SIGQUIT unblocked        after return from sigprocmask     ^\Quit(coredump)         generate signal again     $ ./a.out          ^\^\^\^\^\^\^\^\^\^\     generate signal 10 times (before 5 seconds are up)     SIGQUIT pending     caught SIGQUIT           signal is generated only once     SIGQUIT unblocked     ^\Quit(coredump)         generate signal again 

The message Quit(coredump) is printed by the shell when it sees that its child terminated abnormally. Note that when we run the program the second time, we generate the quit signal ten times while the process is asleep, yet the signal is delivered only once to the process when it's unblocked. This demonstrates that signals are not queued on this system.

Figure 10.15. Example of signal sets and sigprocmask
 #include "apue.h" static void sig_quit(int); int main(void) {     sigset_t    newmask, oldmask, pendmask;     if (signal(SIGQUIT, sig_quit) == SIG_ERR)         err_sys("can't catch SIGQUIT");     /*      * Block SIGQUIT and save current signal mask.      */     sigemptyset(&newmask);     sigaddset(&newmask, SIGQUIT);     if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)         err_sys("SIG_BLOCK error");     sleep(5);   /* SIGQUIT here will remain pending */     if (sigpending(&pendmask) < 0)         err_sys("sigpending error");     if (sigismember(&pendmask, SIGQUIT))         printf("\nSIGQUIT pending\n");     /*      * Reset signal mask which unblocks SIGQUIT.      */     if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)         err_sys("SIG_SETMASK error");     printf("SIGQUIT unblocked\n");     sleep(5);   /* SIGQUIT here will terminate with core file */     exit(0); } static void sig_quit(int signo) {     printf("caught SIGQUIT\n");     if (signal(SIGQUIT, SIG_DFL) == SIG_ERR)         err_sys("can't reset SIGQUIT"); } 

    team bbl



    Advanced Programming in the UNIX Environment
    Advanced Programming in the UNIX Environment, Second Edition (Addison-Wesley Professional Computing Series)
    ISBN: 0321525949
    EAN: 2147483647
    Year: 2005
    Pages: 370

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net