免费注册 查看新帖 |

Chinaunix

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

[Linux] 在linux中当调用一个信号处理函数时被捕捉的信号没有添加到当前进程的信号屏蔽字中 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-08-14 11:49 |只看该作者 |倒序浏览
我的系统参数是:Linux 2.6.32-41-generic Ubuntu 10.04  i686 GNU/Linux

我在自己的机子上运行apue中的程序清单10-15时的结果与书上的结果不一样,程序如下
  1. #include "../apue.h"

  2. static void sig_int(int);

  3. int main(void)
  4. {
  5.     sigset_t newmask, oldmask, waitmask;

  6.     pr_mask("program start: ");

  7.     if (signal(SIGINT, sig_int) == SIG_ERR)
  8.         err_sys("signal(SIGINT) error");
  9.     sigemptyset(&waitmask);
  10.     sigaddset(&waitmask, SIGUSR1);
  11.     sigemptyset(&newmask);
  12.     sigaddset(&newmask, SIGINT);

  13.     if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
  14.         err_sys("SIG_BLOCK error");

  15.     pr_mask("in critical region: ");

  16.     if (sigsuspend(&waitmask) != -1)
  17.         err_sys("sigsuspend error");

  18.     pr_mask("after return from sigsuspend: ");

  19.     if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
  20.         err_sys("SIG_SETMASK error");

  21.     pr_mask("program exit: ");

  22.     exit(0);
  23. }

  24. static void sig_int(int signo)
  25. {
  26.     pr_mask("\nin sig_int: ");
  27. }
复制代码
书上的结果是:
  1. $./a.out
  2. program start:
  3. in critical region: SIGINT
  4. ^?
  5. in sig_int:SIGINT SIGUSR1
  6. after return from sigsuspend:SIGINT
  7. program exit:
复制代码
而我的结果在in sig_int:一行是:in sig_int: SIGUSR1,没有SIGINT。
请问:为什么SIGINT没有添加到信号屏蔽字中去?

论坛徽章:
0
2 [报告]
发表于 2012-08-14 21:50 |只看该作者
高手,想问一下,书上的例子怎末弄才能编译啊,按照readme.txt中的内容试了几次,还是没成功,希望能给我这个小菜鸟一点指导,多谢

论坛徽章:
0
3 [报告]
发表于 2012-08-15 11:01 |只看该作者
回复 2# maxlidahan


    是不是apue.h头文件的问题,如果是,书后目录B中有头文件及大部分函数代码,头文件中的部分函数代码在书中也能过找到。

论坛徽章:
0
4 [报告]
发表于 2012-08-15 13:46 |只看该作者
lz 上面那个问题找到了没? 是怎么个问题呀~ 手头没环境没法看呀~

论坛徽章:
0
5 [报告]
发表于 2012-08-15 21:55 |只看该作者
回复 4# xiyoulaoyuanjia


    还没找到是什么问题,按书上说每当调用一个信号处理函数,在该处理函数中必定会阻塞此信号,可在我的系统中就不会阻塞,好几个例子都这样,不知道是不是系统要设置下,目前还不知道怎么弄

论坛徽章:
0
6 [报告]
发表于 2012-08-15 23:58 |只看该作者
奥,确实比较费解呀~~

论坛徽章:
0
7 [报告]
发表于 2012-08-17 09:32 |只看该作者
回复 6# xiyoulaoyuanjia

我查看了signal的man手册,是signal实现机制的原因。


     In the original Unix systems, when a handler that was established using
       signal()  was  invoked  by the delivery of a signal, the disposition of
       the signal would be reset to SIG_DFL, and  the  system  did  not  block
       delivery  of  further  instances of the signal.  System V also provides
       these semantics for signal().  This was bad because the signal might be
       delivered  again before the handler had a chance to reestablish itself.
       Furthermore, rapid deliveries of the same signal could result in recur‐
       sive invocations of the handler.

The situation on Linux is as follows:

       * The kernel's signal() system call provides System V semantics.

论坛徽章:
0
8 [报告]
发表于 2012-08-17 11:06 |只看该作者
奥,学习了~~

论坛徽章:
0
9 [报告]
发表于 2012-08-18 18:53 |只看该作者
回复 1# SHYAYY
这个问题 我还没有碰到过 我编译的结果和书上的一样


   

论坛徽章:
0
10 [报告]
发表于 2012-08-18 18:59 |只看该作者
回复 2# maxlidahan
我都没看他的那个APUE.h的那个头文件  那里面无非是定义了一些常量 还有常用的头文件 我都是自己动手写代码自己添加 目前看到线程 木有遇到问题 有问题叫我


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP