<p>    有时候咱们须要在程序中利用信号来控制程序行为,linux为咱们提供了2个已经定义的信号SIGUSR1和SIGUSR2,通常的程序利用这2个信号已经能知足须要,不过我最近须要一些其余信号来避免覆盖原来的信号处理函数。 <br />    上网查了一下,看到了下面的程序片断: <br /> </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>linux
<p>    <br />    而后到系统里查了一下,MYSIG_MSG其实将其余的信号给覆盖了: ubuntu
<br />$kill -l 显示 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRMcentos
<br /> 虽然SIGPIPE和SIGALRM在这个程序中没有用到,可是这并非我想要的效果。函数
<br /> 我发如今后面有 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 ,man 7 signal页面一样也说明能够用 SIGRTMIN做为自定义信号。而后程序里就多了下面的代码: </p>ui
<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>orm
<p> <br />    结果固然是出错了咯,可是并非这个定义方式的问题。在我程序中有下面的代码: three
<br /> </p>ci
<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>it
<p>     <br />    编译时才发现原来SIGRTMIN并非一个常量,看了头文件里才知道: io
<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 />    原来是函数调用,运行时肯定的。
<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 />    在找到系统定义的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>    改为34以后就没有问题了。虽然在man里面说明了程序不该该直接用常量标识信号,不过为了达到个人目的也顾不了了。 <br />    记录一点东西来看成回忆。</p>