<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>