10 对sigprocmask,sleep 函数的理解(sigsuspend)

10.12 stevens 在阐述sigprocmask函数时 “在调用sigprocmask后如果有任何未决的,不在阻塞的信号,则在sigprocmask返回前,至少会将其中一个信号递送给该进程”

10.14 stevens "在休眠期间(sleep函数执行期间)如果产生了推出信号,那么此时该信号是未决的,但是不在受阻塞,所以在sigprocmask返回之前,它被递送到调用进程。

10.19 sleep 函数的实现

#include "apue.h"
static void
sig_alrm(int signo)
{
/* nothing to do, just returning wakes up sigsuspend() */
}
unsigned int
sleep(unsigned int nsecs)
{
struct sigaction newact, oldact;
sigset_t newmask, oldmask, suspmask;
unsigned int unslept;
/* set our handler, save previous information */
newact.sa_handler = sig_alrm;
sigemptyset(&newact.sa_mask);
newact.sa_flags = 0;
sigaction(SIGALRM, &newact, &oldact);
/* block SIGALRM and save current signal mask */
sigemptyset(&newmask);
sigaddset(&newmask, SIGALRM);
sigprocmask(SIG_BLOCK, &newmask, &oldmask);
alarm(nsecs);
suspmask = oldmask;
sigdelset(&suspmask, SIGALRM); /* make sure SIGALRM isn't blocked */
sigsuspend(&suspmask); /* wait for any signal to be caught */
/* some signal has been caught, SIGALRM is now blocked */
unslept = alarm(0);
sigaction(SIGALRM, &oldact, NULL); /* reset previous action */
/* reset signal mask, which unblocks SIGALRM */
sigprocmask(SIG_SETMASK, &oldmask, NULL);
return(unslept);
}

在第25行,使用了sigsuspend函数,所以sleep函数的调用内部调用了sigsuspend函数,解释了为什么在   阻塞的区间内,sleep 函数 执行过程中 递送该信号可以被处理,而不会被阻塞

代码位于 10.13 中的程序10.11


你可能感兴趣的:(10 对sigprocmask,sleep 函数的理解(sigsuspend))