免费注册 查看新帖 |

Chinaunix

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

[其他] 同时trap到多个相同信号中断是作为一个信号处理吗 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-10-09 11:30 |只看该作者 |倒序浏览
trap 处理信号时,如同时收到多个信号,会忽略后面的信号作为一个信号处理吗,试验好像是这样的,如果不想忽略,应如何设置?

论坛徽章:
7
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:57:092015小元宵徽章
日期:2015-03-06 15:58:18程序设计版块每日发帖之星
日期:2015-08-09 06:20:00每日论坛发贴之星
日期:2015-08-09 06:20:00程序设计版块每日发帖之星
日期:2015-08-22 06:20:00程序设计版块每日发帖之星
日期:2015-08-27 06:20:00
2 [报告]
发表于 2015-10-09 12:20 |只看该作者
本帖最后由 tuyajie 于 2015-10-09 12:21 编辑

帮顶一下。trap有什么好的应用场景啊。我一直用不好

论坛徽章:
16
CU十二周年纪念徽章
日期:2013-10-24 15:41:3415-16赛季CBA联赛之广东
日期:2015-12-23 21:21:55青铜圣斗士
日期:2015-12-05 10:35:30黄金圣斗士
日期:2015-11-26 20:42:16神斗士
日期:2015-11-19 12:47:50每日论坛发贴之星
日期:2015-11-18 06:20:00程序设计版块每日发帖之星
日期:2015-11-18 06:20:002015亚冠之城南
日期:2015-11-10 19:10:492015亚冠之萨济拖拉机
日期:2015-10-28 18:47:282015亚冠之柏太阳神
日期:2015-08-30 17:21:492015亚冠之山东鲁能
日期:2015-07-07 18:48:39摩羯座
日期:2014-08-29 23:01:42
3 [报告]
发表于 2015-10-09 12:37 |只看该作者
供参考:
man signal
......

Upon  arrival  of  a signal with number signum the following happens.  If the corre-
       sponding handler is set to SIG_IGN, then the signal is ignored.  If the  handler  is
       set  to  SIG_DFL, then the default action associated with the signal (see signal(7))
       occurs. Finally, if the handler is set to a function sighandler then  first  either
       the  handler is reset to SIG_DFL or an implementation-dependent blocking of the sig-
       nal is performed and next sighandler is called with argument signum.


       Using a signal handler function for a signal is called "catching the  signal".   The
       signals SIGKILL and SIGSTOP cannot be caught or ignored.

RETURN VALUE
       The  signal()  function returns the previous value of the signal handler, or SIG_ERR
       on error.

PORTABILITY
       The original Unix signal() would reset the handler to SIG_DFL, and System V (and the
       Linux  kernel and libc4,5) does the same.  On the other hand, BSD does not reset the
       handler, but blocks new instances of this signal from occurring during a call of the
       handler.  The glibc2 library follows the BSD behaviour.

......

论坛徽章:
0
4 [报告]
发表于 2015-10-09 15:38 |只看该作者
本帖最后由 小渔儿 于 2015-10-09 15:43 编辑

12行28行为两个$

  1. #!/bin/bash

  2. mytrap()
  3. {
  4.   ((a++))
  5.   echo 'SIGUSR1 run'
  6. }

  7. a=0
  8. trap mytrap  SIGUSR1

  9. ffile="/tmp/fifo.$"
  10. mkfifo  ${ffile}
  11. exec 6<>${ffile}
  12. rm -fr ${ffile}

  13. for ((n=1;n<=5;n++))
  14. do
  15.     echo $n >&6
  16. done


  17. for ((i=1;i<=10;i++))
  18. do
  19.     read -u6 abc
  20.     {
  21.       echo succes$i;sleep 1
  22.       kill -s SIGUSR1 $
  23.       echo ${abc}>&6
  24.     }&
  25. done
  26. wait
  27. sleep 3

  28. echo "finish"
  29. echo $a

  30. exec 6<&-
  31. exec 6>&-
复制代码
我是看了shell模拟多进程程序想让变量传到主进程中来,用信号量的方法,但实际测试下来,a的值不是期望的值,每次结果不同。
想问一下大神们信号的处理模式。在trap处理信息的过程中如果又收到信号此时是忽略此信号还是让该信号等待处理?

所贴代码中两个$怎么变成一个$了?

论坛徽章:
84
每日论坛发贴之星
日期:2015-12-29 06:20:00每日论坛发贴之星
日期:2016-01-16 06:20:00每周论坛发贴之星
日期:2016-01-17 22:22:00程序设计版块每日发帖之星
日期:2016-01-20 06:20:00每日论坛发贴之星
日期:2016-01-20 06:20:00程序设计版块每日发帖之星
日期:2016-01-21 06:20:00每日论坛发贴之星
日期:2016-01-21 06:20:00程序设计版块每日发帖之星
日期:2016-01-23 06:20:00程序设计版块每日发帖之星
日期:2016-01-31 06:20:00数据库技术版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-14 06:20:00
5 [报告]
发表于 2015-10-10 16:14 |只看该作者
trap 重复信号这块不清楚;

只是回答一下两个 $ 变一个 $ 的问题: 这个是CU bbs的一个bug (很多年了,一直没有修复)

论坛徽章:
16
CU十二周年纪念徽章
日期:2013-10-24 15:41:3415-16赛季CBA联赛之广东
日期:2015-12-23 21:21:55青铜圣斗士
日期:2015-12-05 10:35:30黄金圣斗士
日期:2015-11-26 20:42:16神斗士
日期:2015-11-19 12:47:50每日论坛发贴之星
日期:2015-11-18 06:20:00程序设计版块每日发帖之星
日期:2015-11-18 06:20:002015亚冠之城南
日期:2015-11-10 19:10:492015亚冠之萨济拖拉机
日期:2015-10-28 18:47:282015亚冠之柏太阳神
日期:2015-08-30 17:21:492015亚冠之山东鲁能
日期:2015-07-07 18:48:39摩羯座
日期:2014-08-29 23:01:42
6 [报告]
发表于 2015-10-10 20:16 |只看该作者
回复 5# yjh777


    There is a bug called won't fix....
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP