- 论坛徽章:
- 0
|
本帖最后由 星辉流动 于 2012-09-06 16:10 编辑
在看apue信号这一章时,运行程序10-16出现和书本不一样的结果,代码如下- 1 #include "../include/apue.h"
- 2 volatile sig_atomic_t quitflag;
- 3
- 4 static void
- 5 sig_int(int signo)
- 6 {
- 7 if(signo == SIGINT)
- 8 {
- 9 pr_mask("SIGINT: ");
- 10 printf("\ninterrupt\n");
- 11 }
- 12 else if(signo == SIGQUIT)
- 13 {
- 14 pr_mask("SIGQUIT: ");
- 15 quitflag = 1;
- 16 printf("SIGQUIT\n");
- 17 }
- 18 else
- 19 {
- 20 printf("%s",strsignal(signo));
- 21 }
- 22 }
- 23
- 24 int
- 25 main(void)
- 26 {
- 27 sigset_t newmask, oldmask, zeromask;
- 28
- 29 if(signal(SIGINT, sig_int) == SIG_ERR)
- 30 err_sys("signal(SIGINT) error");
- 31 if(signal(SIGQUIT, sig_int) == SIG_ERR)
- 32 err_sys("signal(SIGQUIT) error");
- 33
- 34 sigemptyset(&newmask);
- 35 sigemptyset(&zeromask);
- 36 sigaddset(&newmask,SIGQUIT);
- 37
- 38 if(sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
- 39 err_sys("SIG_BLOCK error");
- 40 while(quitflag == 0)
- 41 {
- 42 pr_mask("loop: ");
- 43 sigsuspend(&zeromask);
- 44 printf("quitflag = %d\n",quitflag);
- 45 }
- 46 pr_mask("after loop: ");
- 47 quitflag = 0;
- 48
- 49 if(sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
- 50 err_sys("SIG_SETMASK error");
- 51 exit(0);
- 52 }
复制代码 编译运行如下:- [root@localhost signal]# gcc -g -o sigsuspendwait sigsuspendwait.c ../include/err.c pr_mask.c
复制代码- [root@localhost signal]# ./sigsuspendwait
- loop: SIGQUIT
- SIGINT:
- interrupt
- quitflag = 0
- loop: SIGQUIT
- [root@localhost signal]#
复制代码 在运行的时候,只能接受SIGINT信号一次,再次按Ctrl+c程序就退出了,而不是书上那样可以连续按ctrl+c,这是为什么呢?? |
|