本版版主招募中

 
标题: [已解决] 为什么用sigaction,不能反复捕抓信号?
wanghi
LU新生
Rank: 1



UID 33913
精华 0
积分 9
帖子 17
活跃指数 6
LU金币 2025 个
LU金条 0 个
阅读权限 10
注册 2005-8-8
 
发表于 2006-11-10 10:38  资料  个人空间  短消息  加为好友 
为什么用sigaction,不能反复捕抓信号?

想反复地捕捉信号。

代码如下:

CODE:

[Copy to clipboard]



#include <signal.h>

#include <sys/types.h>

#include <unistd.h>

#include <setjmp.h>
#include <stdio.h>

jmp_buf ebuf;

void new_op(int ,siginfo_t *,void *);



int main(int argc,char**argv)

{

        struct sigaction act;        

        int sig = SIGALRM ;

        int n;

        setjmp(ebuf);

        sigemptyset(&act.sa_mask);

        act.sa_flags=SA_SIGINFO;

        act.sa_sigaction=new_op;

        

        if(sigaction(sig,&act,NULL) < 0)

        {

                printf("install sigal error\n");

        }

        

      //  setjmp(ebuf);

        n = alarm(5);
        printf("alarm n = %d\n",n);

        while(1)

        {

                sleep(6);

                printf("wait for the signal\n");

        }

}

void new_op(int signum,siginfo_t *info,void *myact)

{

        printf("receive signal %d\n", signum);

        //alarm(0);

        longjmp(ebuf,2);

        

}

显示结果:
alarm n = 0
receive signal 14
alarm n = 0
wait for the signal
wait for the signal
wait for the signal
........


[ 本帖最后由 wanghi 于 2006-11-10 11:32 编辑 ]

顶部
wanghi
LU新生
Rank: 1



UID 33913
精华 0
积分 9
帖子 17
活跃指数 6
LU金币 2025 个
LU金条 0 个
阅读权限 10
注册 2005-8-8
 
发表于 2006-11-10 11:32  资料  个人空间  短消息  加为好友 
用SIGSETJMP和SIGLONGJMP

顶部
zjspy
LU新生
Rank: 1



UID 904
精华 0
积分 12
帖子 21
活跃指数 0
LU金币 2020 个
LU金条 0 个
阅读权限 10
注册 2003-10-22
 
发表于 2006-11-25 14:54  资料  个人空间  短消息  加为好友 
POSIX的singal函数中会把程序未处理的多个相同信号当成一个来处理,只会发起一次信号处理。

顶部
 



当前时区 GMT+8, 现在时间是 2008-10-7 21:03
乐悠LoveUnix论坛-京ICP备05005823号

Thanks to Discuz!  © 2001-2007    Power by LoveUnix.net
Processed in 0.058361 second(s), 6 queries , Gzip enabled

清除 Cookies - 联系我们 - 乐悠LoveUnix - Archiver