linux自定义信号的处理

<p>&#160;&#160;&#160; 有时候我们需要在程序中利用信号来控制程序行为,linux为我们提供了2个已经定义的信号SIGUSR1和SIGUSR2,一般的程序利用这2个信号已经能满足需要,不过我最近需要一些其他信号来避免覆盖原来的信号处理函数。 <br />&#160;&#160;&#160; 上网查了一下,看到了下面的程序片段: <br />&#160;</p> <div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:0ef6b40e-d284-4414-96f4-5f9e86ac8e20" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: cpp; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 400px; height: 34px;" style=" width: 400px; height: 34px;overflow: auto;">#define MYSIG_MSG (SIGUSR2 + 1) // 定义信号然后注册处理函数</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>

<p>&#160;&#160;&#160; <br />&#160;&#160;&#160; 然后到系统里查了一下,MYSIG_MSG其实将其他的信号给覆盖了:

<br />$kill -l 显示 10) SIGUSR1    11) SIGSEGV    12) SIGUSR2 13) SIGPIPE    14) SIGALRM

<br />    虽然SIGPIPE和SIGALRM在这个程序中没有用到,但是这并不是我想要的效果。

<br />    我发现在后面有 34) SIGRTMIN 35) SIGRTMIN+1    36) SIGRTMIN+2 ,man 7 signal页面同样也说明可以用 SIGRTMIN作为自定义信号。然后程序里就多了下面的代码:  </p>

<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:8c52f536-cd1e-4946-9415-d2ed9b716322" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: cpp; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 400px; height: 18px;" style=" width: 400px; height: 18px;overflow: auto;">#define MYSIG_MSG (SIGRTMIN+ 1)</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>

<p> <br />&#160;&#160;&#160; 结果当然是出错了咯,但是并不是这个定义方式的问题。在我程序中有下面的代码:

<br />    </p>

<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:b26ca4ea-6c47-4461-9a18-a5e9978fad0c" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: cpp; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 400px; height: 67px;" style=" width: 400px; height: 67px;overflow: auto;">switch(signo){ case MYSIG_MSG: break; }</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>

<p>&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160; 编译时才发现原来SIGRTMIN并不是一个常量,看了头文件里才知道:

<br /></p>

<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:3ea710e4-7690-41a7-8056-96012deaa8cd" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: cpp; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 433px; height: 45px;" style=" width: 433px; height: 45px;overflow: auto;">// centos5.9 /usr/include/bits/signum.h #define SIGRTMIN (__libc_current_sigrtmin ())</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>

<p> <br />&#160;&#160;&#160; 原来是函数调用,运行时确定的。

<br />    要用这个SIGRTMIN宏是不行,只能自己定义了:

<br /></p>

<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:610c29e4-145a-48bd-804a-1cdb43a56da0" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: cpp; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 349px; height: 41px;" style=" width: 349px; height: 41px;overflow: auto;">#define MYSIGRTMIN 34 #define MYSIG_MSG (MYSIGRTMIN + 1)</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>

<p> <br />&#160;&#160;&#160; 在找到系统定义的SIGRTMIN值之前,根据man 7 signal里面的说明:

<br />    Linux supports 32 real-time signals, numbered from 32 (SIGRTMIN) to 63 (SIGRTMAX).

<br />    我把自定义的信号值定义成了32,但是一直注册不了这个信号,后来赫然发现在 man 7 signal下面有一行说明,

<br />    However, the glibc POSIX threads implementation internally uses two (for NPTL) or three  (for  LinuxThreads)  real-time signals  (see  pthreads(7)), and adjusts the value of SIGRTMIN suitably (to 34 or 35)

<br />    这个说明在ubuntu12.04里面看见的,估计centos也有类似的情况。同时头文件下面也有:</p>

<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:7af67293-c2a3-4172-8c19-f0fa01e4b37e" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: cpp; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 800px; height: 89px;" style=" width: 800px; height: 89px;overflow: auto;">/* These are the hard limits of the kernel. These values should not be used directly at user level. */ #define __SIGRTMIN 32 #define __SIGRTMAX (_NSIG - 1)</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>

<p>&#160;&#160;&#160; 改成34之后就没有问题了。虽然在man里面说明了程序不应该直接用常量标识信号,不过为了达到我的目的也顾不了了。 <br />&#160;&#160;&#160; 记录一点东西来当作回忆。</p>

转载于:https://my.oschina.net/u/566882/blog/174414

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值