免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2181 | 回复: 4
打印 上一主题 下一主题

关于信号阻塞,未决 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-21 07:46 |只看该作者 |倒序浏览
  5 #include"apue.h"
  6 static void sig_quit(int);
  7
  8 int main(void)
  9 {
10     sigset_t newmask,oldmask,pendmask;
11
12     if(signal(SIGQUIT,sig_quit) == SIG_ERR)
13         err_sys("can't catch SIGQUIT");
14
15     sigemptyset(&newmask);
16     sigaddset(&newmask,SIGQUIT);
17
18     if(sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
19         err_sys("SIG_BLOCK error");
20
21     sleep(5);
22
23     if(sigpending(&pendmask) < 0)
24         err_sys("sigpending error");
25     if(sigismember(&pendmask, SIGQUIT))
26         printf("\nSIGQUIT pending\n");
27
28     if(sigprocmask(SIG_SETMASK, &oldmask, NULL) <0)
29         err_sys("SIG_SETMASK error");
30     printf("SIGQUIT unblocked\n");
31     sleep(5);
32     exit(0);
33 }
34 static void sig_quit(int signo)
35 {
36     printf("caught SIGQUIT\n");
37     if(signal(SIGQUIT, SIG_DFL) == SIG_ERR)
38         err_sys("can't reset SIGQUIT");
39 }

执行结果为什么是这样的,
$./a.out
^\                      产生信号一次(在5s之内)
SIGQUUIT pending  
caught SIGQUIT
SIGQUIT unblocked
^\退出


为什么输出SIGQUUIT pending  与输出SIGQUIT unblocked 之间调用了一次信号处理程序呢,这个地方还没有给这个信号解开阻塞呢?为什么?求解

论坛徽章:
0
2 [报告]
发表于 2011-12-21 08:17 |只看该作者
第28行函数的系统调用返回的时候就会执行信号处理程序,然后才是29行的printf输出

论坛徽章:
0
3 [报告]
发表于 2011-12-21 11:49 |只看该作者
sigprocmask返回前,信号就被处理了,是吗?

论坛徽章:
0
4 [报告]
发表于 2011-12-21 13:23 |只看该作者
sigprocmask返回之前,会递送一个信号。

论坛徽章:
0
5 [报告]
发表于 2011-12-23 14:57 |只看该作者
谢谢。我明白了,怪自己读书不认真。sigprocmask返回之前,会递送一个信号。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP