Signal--sigpending function

Source: Internet
Author: User
Tags sleep
Signal--sigpending function

The sigpending function returns a signal that is blocked and is pending for the calling process. The signal set is returned by the set parameter.



#include <signal.h>

int sigpending (sigset_t *set);

Successful return 0, error returned-1


The following code shows the characteristics of all the signals we have described.



#include <signal.h>

static void Sig_quit (int);

Int
Main (void)
{
sigset_t Newmask, Oldmask, Pendmask;

if (Signal (sigquit, sig_quit) = = Sig_err) {
printf ("Can ' t catch sigquit");
Exit (1);
}

/*
* Block Sigquit and save current signal mask.
*/
Sigemptyset (&newmask);
Sigaddset (&newmask, sigquit);
if (Sigprocmask (Sig_block, &newmask, &oldmask) < 0) {
printf ("Sig_block error\n");
Exit (1);
}

Sleep (5); /* Sigquit here'll remain pending */
if (sigpending (&pendmask) < 0) {
printf ("sigpending error\n");
Exit (1);
}
if (Sigismember (&pendmask, Sigquit))
printf ("\nsigquit pending\n");

/*
* Reset Signal mask which unblocks sigquit.
*/
if (Sigprocmask (Sig_setmask, &oldmask, NULL) < 0) {
printf ("Sig_setmask error\n");
Exit (1);
}
printf ("Sigquit unblocked\n");

Sleep (5); /* Sigquit here would terminate with core file */
Exit (0);
}

static void
Sig_quit (int signo)
{
printf ("Caught sigquit\n");
if (Signal (sigquit, SIG_DFL) = = Sig_err) {
printf ("Can ' t reset sigquit\n");
Exit (1);
}
}
The process blocks the Sigquit, saves its current signal mask (so it can be reset later), and then sleeps for 5 seconds. Any exit signals during this period will not be distributed until the signal is blocked. After 5 Seconds of sleep, we check to see if the signal is pending and blocks the signal.

Note that we saved the old mask before blocking the signal. To counter-block the signal, we executed the old mask of the sig_setmask. In another way, we can only sig_unblock signals that we have blocked. However, to know that if we write a function that can be called, and we need to block a signal in our function, we cannot use Sig_unblock to block the signal. In this case, we must use Sig_setmask and reset the signal mask to its previous value, because the caller may have blocked the signal before calling our function. We will see an example of this in the system function in section 10.18.


If we produce an exit signal during sleep, then the signal is now pending without blocking, so it is distributed in the Sigprocmask return value. We will see this happen because printf in the signal processor outputs before the Sigprocmask call to printf.


Process then slept for another 5 seconds. If we generate an exit signal during this sleep, then the signal should terminate the process because we have to reset the signal to the default value after capturing it. In the output below, when we enter the terminal exit character Control-backslash, the terminal prints out the ^\:

$./a.out
^\
Sigquit pending
Caught Sigquit
Sigquit unblocked
$./a.out
^\^\^\^\^\^\^\^\^\^\
Sigquit pending
Caught Sigquit
Sigquit unblocked


Note When you run the program for the second time, we generate 10 times times the exit signal when the process sleeps, but the signal is only distributed to the process once it is blocked. This proves that the signal is not queued on this system.

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.