2006-11-10 10:38
wanghi
为什么用sigaction,不能反复捕抓信号?
[size=13px]想反复地捕捉信号。
代码如下:
[b]CODE:
[align=right][url=http://bbs.chinaunix.net/viewthread.php?tid=854074&extra=page%3D1###][Copy to clipboard][/url][/align]
[/b]
[font=fixedsys]#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);
}[/font]
显示结果:
alarm n = 0
receive signal 14
alarm n = 0
wait for the signal
wait for the signal
wait for the signal
........[/size][size=2] [/size]
[[i] 本帖最后由 wanghi 于 2006-11-10 11:32 编辑 [/i]]