这是根据setjmp/longjmp,sigaction signal,sigqueue函数写的一个对信号这章的大体程序:
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<setjmp.h>
- #include<signal.h>
- #include<string.h>
- jmp_buf buf;
- void handler_sigint1(int signo)
- {
- printf("recv SIGINT\n");
- longjmp(buf,1);
- }
- void handler_sigint2(int signum,siginfo_t *info,void *myact)
- {
- int i;
- for(i=0;i<5;i++)
- printf("%d\n",*((int *)(info->si_value.sival_ptr)+i));
- printf("handle signal %d over\n",signum);
- longjmp(buf,2);
- }
- int main(int argc,char **argv)
- {
- struct sigaction act;
- union sigval mysigval;
- int i;
- int sig;
- pid_t pid;
- int data[10];
- memset(data,0,sizeof(data));
- for(i=0;i<5;i++)
- data[i]=2;
- mysigval.sival_ptr=data;
- // sig=atoi(argv[1]);
- pid=getpid();
- sigemptyset(&act.sa_mask);
-
- switch(setjmp(buf))
- {
- case 0:
- break;
- case 1:
- printf("return from handler_sigint1\n");
- break;
- case 2:
- printf("wait for handler_sigint2\n");
- break;
- default:
- break;
- }
- sig=2;
- signal(SIGINT,handler_sigint1);
- raise(sig);
- act.sa_sigaction=handler_sigint2;
- act.sa_flags=SA_SIGINFO;
- sigaction(SIGRTMIN+15,&act,NULL);
- sig=49;
- sigqueue(pid,sig,mysigval);
- while(1);
- return 0;
- }
阅读(1267) | 评论(0) | 转发(0) |