Sample apue code:
Sigemptyset (& new_mask );
Sigemptyset (& zero_mask); // clears the zero_mask of the signal set.
Sigaddset (& new_mask, sigquit );
Sigprocmask (sig_block, & new_mask, & old_mask); // block sigquit
While (quitflag = 0)
{
Sigsuspend (& zero_mask); // Replace the signal mask with null. Wait until the sigquit signal processing function sets quitflag to 1.
}
Sigprocmask (sig_setmask, & old_mask, null); // restores the signal mask.
Why should sigsuspend be used with sigprocmask?
For atomic operations
If sigprocmask () is not called to shield sigquit signals, sigquit signals can occur at any time.
Assume that the signal occurs exactly after quitflag = 0, and the signal processing program is called. quitflag = 1
Call sigsuspend () after the signal processing program returns ()..
If there is no second sigquit signal since then, the program will be blocked in sigsuspend (), although quitflag = 1 at this time
After sigprocmask () is called to shield the sigquit signal, the request will be delayed even if the signal occurs until sigsuspend () unblocks the signal.