Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2093470
  • 博文数量: 361
  • 博客积分: 10828
  • 博客等级: 上将
  • 技术积分: 4161
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-20 14:34
文章分类

全部博文(361)

文章存档

2011年(132)

2010年(229)

分类: C/C++

2010-04-12 19:27:39

进程部分介绍过了setjmp和longjmp函数, 这两个函数在跳转时会带信号屏蔽字跳转, 在信号处理程序(hanlder)中使用longjmp会导致后来产生的这种信号被屏蔽.
POSIX.1 也没有具体说明setjmp和longjmp对信号屏蔽字的作用, 而是定义了两个新函数: sigsetjmp和siglongjmp.
1. 原型:
#include
int sigsetjmp(sigjmp_buf env, int savemask);
直接调用则返回0, 从siglongjmp调用返回则返回非0值.
void siglongjmp(sigjmp_buf env, int val);
可见发现sigsetjmp比setjmp多了一个参数savemask, 如果非0, 则sigsetjmp在env中保存进程的当前信号屏蔽字.

2. 实例:
还是老习惯, 用代码来验证
#include
#include
#include
#include
/* Jump buffer */
static sigjmp_buf jmpbuf;
/* Signal handler */
static void myfunc(int signo)
{
    printf("SIGQUIT ");
    sleep(1);
    siglongjmp(jmpbuf, 1);
}
int main()
{
    char *p = NULL;
    struct sigaction act;
    act.sa_handler = myfunc;
    act.sa_flags = SA_INTERRUPT;
    sigemptyset(&act.sa_mask);
    if (sigaction(SIGQUIT, &act, NULL) < 0)
        perror("sigaction");
    if (sigsetjmp(jmpbuf, 1) == 1)
    {
        printf("I'm jumped ");
    }
    else
    {
        /* SIGSEGV */
        raise(SIGQUIT);
    }
    /* JUMP */
    printf("I'm here ");
    return 0;
}
这段代码首先用sigaction设定SIGQUIT信号的处理函数, 然后sigsetjmp设定sigjmp_buf, 当它从siglongjmp返回时显示I'm jumped, 第一次设定完成后产生SIGQUIT信号.
运行结果:
SIGQUIT
I'm jumped
I'm here
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/dai_weitao/archive/2007/09/28/1804610.aspx
阅读(1066) | 评论(0) | 转发(0) |
0

上一篇:sigaction

下一篇:MII接口详解

给主人留下些什么吧!~~