免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3459 | 回复: 2

[Linux] 为什么SIGALRM信号捕捉不到了 [复制链接]

论坛徽章:
0
发表于 2012-08-21 21:52 |显示全部楼层
最近在学习linux信号时遇到一点问题,百思不得其解,其代码如下:
  1. #include<setjmp.h>                                                         
  2.   2 #include "../include/apue.h"
  3.   3
  4.   4 static void sig_alrm(int);
  5.   5 static jmp_buf env_alrm;
  6.   6
  7.   7 int main(void)
  8.   8 {
  9.   9     int n;
  10. 10     char line[MAXLINE];
  11. 11
  12. 12     if(signal(SIGALRM, sig_alrm) == SIG_ERR)
  13. 13     {
  14. 14         err_sys("signal error");
  15. 15     }
  16. 16     if(setjmp(env_alrm) != 0)
  17. 17         ;
  18. 18     //  err_quit("set_jmp error");
  19. 19     alarm(10);
  20. 20     printf("read start\n");
  21. 21     if((n = read(STDIN_FILENO, line,MAXLINE)) < 0)
  22. 22         err_sys("read error");
  23. alarm(0);
  24. 24     write(STDOUT_FILENO, line, n);
  25. 25     exit(0);
  26. 26 }
  27. 27
  28. 28 static void sig_alrm(int signo)
  29. 29 {
  30. 30     signal(SIGALRM,sig_alrm);
  31. 31     printf("signal start\n");;
  32. 32     //write(STDOUT_FILENO, line, n);
  33. 33     longjmp(env_alrm,1);
  34. 34 }   
复制代码
其运行的结果如下:
  1. [root@localhost signal]# gcc -o lread lread.c ../include/err.c
  2. [root@localhost signal]# ./lread
  3. read start
  4. signal start
  5. read start
复制代码
会捕捉到第一个定时信号,但是为什么跳转回去的时候对SIGALRM的信号就不处理了呢?望懂的童鞋指点一下,万分感激

论坛徽章:
0
发表于 2012-08-22 00:08 |显示全部楼层
打印下信号屏蔽子看看吧~~  

论坛徽章:
0
发表于 2012-08-22 00:10 |显示全部楼层
  1. 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.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP