Chinaunix

标题: 同时trap到多个相同信号中断是作为一个信号处理吗 [打印本页]

作者: 小渔儿    时间: 2015-10-09 11:30
标题: 同时trap到多个相同信号中断是作为一个信号处理吗
trap 处理信号时,如同时收到多个信号,会忽略后面的信号作为一个信号处理吗,试验好像是这样的,如果不想忽略,应如何设置?
作者: tuyajie    时间: 2015-10-09 12:20
本帖最后由 tuyajie 于 2015-10-09 12:21 编辑

帮顶一下。trap有什么好的应用场景啊。我一直用不好
作者: tc1989tc    时间: 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.

......
作者: 小渔儿    时间: 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处理信息的过程中如果又收到信号此时是忽略此信号还是让该信号等待处理?

所贴代码中两个$怎么变成一个$了?
作者: yjh777    时间: 2015-10-10 16:14
trap 重复信号这块不清楚;

只是回答一下两个 $ 变一个 $ 的问题: 这个是CU bbs的一个bug (很多年了,一直没有修复)
作者: tc1989tc    时间: 2015-10-10 20:16
回复 5# yjh777


    There is a bug called won't fix....




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2