#include
#include "ourhdr.h"
static void sig_int(int);
int
main(void)
{
sigset_t newmask, oldmask, zeromask;
if (signal(SIGINT, sig_int) == SIG_ERR)
err_sys("signal(SIGINT) error");
if (signal(SIGALRM, sig_int) == SIG_ERR)
err_sys("signal(SIGINT) error");
sigemptyset(&zeromask);
sigemptyset(&newmask);
sigaddset(&newmask, SIGALRM);
sigaddset(&newmask, SIGINT);
/* block SIGINT and save current signal mask */
if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
err_sys("SIG_BLOCK error");
/* critical region of code */
pr_mask("in critical region: ");
alarm(2);
/* allow all signals and pause */
if (sigsuspend(&zeromask) != -1)
err_sys("sigsuspend error");
pr_mask("after return from sigsuspend: ");
/* reset signal mask which unblocks SIGINT */
if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
err_sys("SIG_SETMASK error");
/* and continue processing ... */
exit(0);
}
static void
sig_int(int signo)
{
pr_mask("\nin sig_int: ");
return;
}
执行结果:
in critical region: SIGINT SIGALRM
in sig_int: SIGALRM
after return from sigsuspend: SIGINT SIGALRM
我的分析:
在sigsuspend执行之前,将SIGINT和SIGALRM信号阻塞,执行sigsuspend时,所有的阻塞信号都被取消,进程pause,因此
可以接受到信号SIGALRM,接受到信号后调用sig_int函数,在sig_int中只屏蔽了SIGALRM,因为处理函数中会屏蔽掉所接受到的函
数。然后sigsuspend返回,恢复原先的屏蔽信号
。
阅读(1386) | 评论(0) | 转发(0) |