Chinaunix首页 | 论坛 | 博客
  • 博客访问: 119355
  • 博文数量: 41
  • 博客积分: 1695
  • 博客等级: 上尉
  • 技术积分: 430
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-21 22:50
文章分类

全部博文(41)

文章存档

2010年(1)

2007年(23)

2006年(17)

我的朋友

分类: C/C++

2007-04-15 16:30:59

#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返回,恢复原先的屏蔽信号

阅读(1347) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~