Set and Oldset in sigprocmask()

Sushant picture Sushant · Aug 24, 2008 · Viewed 30.1k times · Source

I haven't completely understood, how to use sigprocmask(). Particularly, how the set and oldset and its syntax work and how to use them.

int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);

Please explain with an example, to block, say SIGUSR1 for a few seconds and then unblock and handle it. ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­

Answer

Nathan Fellman picture Nathan Fellman · Aug 24, 2008

The idea is that you provide a mask in set, effectively a list of signals. The how argument says what you should do with the mask in set.

You can either use SIG_BLOCK to block the signals in the set list, or SIG_UNBLOCK to unblock them. Neither of these changes the signals that aren't set in the list. SIG_SETMASK blocks the signals in the list, and unblocks the ones that aren't set in the list.

For instance, assume that the old blocking list was {SIGSEGV, SIGSUSP} and you call sigprocmask with these arguments:

sigset_t x;
sigemptyset (&x);
sigaddset(&x, SIGUSR1);
sigprocmask(SIG_BLOCK, &x, NULL)

The new blocking list will now be {SIGSEGV, SIGSUSP, SIGUSR1}.

If you call sigprocmask with these arguments now:

sigprocmask(SIG_UNBLOCK, &x, NULL)

The new blocking list will go back to being {SIGSEGV, SIGSUSP}.

If you call sigprocmask with these arguments now:

sigprocmask(SIG_SETMASK, &x, NULL)

The new blocking list will now be set to {SIGUSR1}.

The oldset argument tells you what the previous blocking list was. If we have this declaration:

sigset_t y;

and we call the code in the previous examples like this:

    sigprocmask(SIG_BLOCK, &x, &y)

now we have:

y == {SIGSEGV, SIGSUSP}

If we now do:

    sigprocmask(SIG_UNBLOCK, &x, &y)

we'll get

y == {SIGSEGV, SIGSUSP, SIGUSR1}

and if we do:

    sigprocmask(SIG_SET, &x, &y)

we'll get this:

y == {SIGSEGV, SIGSUSP}

because this is the previous value of the blocking set.