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.
The idea is that you provide a mask in
set, effectively a list of signals. Thehowargument says what you should do with the mask inset.You can either use
SIG_BLOCKto block the signals in thesetlist, orSIG_UNBLOCKto unblock them. Neither of these changes the signals that aren’t set in the list.SIG_SETMASKblocks 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 callsigprocmaskwith these arguments:The new blocking list will now be
{SIGSEGV, SIGSUSP, SIGUSR1}.If you call
sigprocmaskwith these arguments now:The new blocking list will go back to being
{SIGSEGV, SIGSUSP}.If you call
sigprocmaskwith these arguments now:The new blocking list will now be set to
{SIGUSR1}.The
oldsetargument tells you what the previous blocking list was. If we have this declaration:and we call the code in the previous examples like this:
now we have:
If we now do:
we’ll get
and if we do:
we’ll get this:
because this is the previous value of the blocking set.