1. 程式人生 > >linux自定義訊號,並kill測試

linux自定義訊號,並kill測試

1. 自定義訊號的說明

有時候我們需要在程式中利用訊號來控制程式行為,linux為我們提供了2個已經定義的訊號SIGUSR1和SIGUSR2,一般的程式利用這2個訊號已經能滿足需要,不過我最近需要一些其他訊號來避免覆蓋原來的訊號處理函式。
    上網查了一下,看到了下面的程式片段:  
1 2 #define MYSIG_MSG        (SIGUSR2 + 1) <p>// 定義訊號然後註冊處理函式</p>

   
    然後到系統裡查了一下,MYSIG_MSG其實將其他的訊號給覆蓋了:
$kill -l 顯示 10) SIGUSR1    11) SIGSEGV    12) SIGUSR2 13) SIGPIPE    14) SIGALRM
    雖然SIGPIPE和SIGALRM在這個程式中沒有用到,但是這並不是我想要的效果。
    我發現在後面有 34) SIGRTMIN 35) SIGRTMIN+1    36) SIGRTMIN+2 ,man 7 signal頁面同樣也說明可以用 SIGRTMIN作為自定義訊號。然後程式裡就多了下面的程式碼: 

1 #define MYSIG_MSG        (SIGRTMIN+ 1)


    結果當然是出錯了咯,但是並不是這個定義方式的問題。在我程式中有下面的程式碼:


1 2 3 4 switch(signo){ case MYSIG_MSG: break; }

    
    編譯時才發現原來SIGRTMIN並不是一個常量,看了標頭檔案裡才知道:

1 2 // centos5.9 /usr/include/bits/signum.h #define SIGRTMIN        (__libc_current_sigrtmin ())


    原來是函式呼叫,執行時確定的。
    要用這個SIGRTMIN巨集是不行,只能自己定義了:

1 2 #define MYSIGRTMIN    34 #define MYSIG_MSG      (MYSIGRTMIN + 1)


    在找到系統定義的SIGRTMIN值之前,根據man 7 signal裡面的說明:
    Linux supports 32 real-time signals, numbered from 32 (SIGRTMIN) to 63 (SIGRTMAX).
    我把自定義的訊號值定義成了32,但是一直註冊不了這個訊號,後來赫然發現在 man 7 signal下面有一行說明,
    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)
    這個說明在ubuntu12.04裡面看見的,估計centos也有類似的情況。同時標頭檔案下面也有:

1 2 3 4 /* 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)

    改成34之後就沒有問題了。雖然在man裡面說明了程式不應該直接用常量標識訊號,不過為了達到我的目的也顧不了了。

總結:

__SIGRTMIN基礎上的前3個最好不要用,它是linuxthread用的

Sample:

//自定義訊號
#define SIG_MY_DEFINE_TEST    __SIGRTMIN+10

2. 程式設計例子

#include <sys/wait.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>

#include <signal.h>


//自定義訊號
#define SIG_MY_DEFINE_TEST    __SIGRTMIN+10

void    sigactionProcess(int nsig)
{
	//================================================================
	//TODO:ADD YOU CODE

	printf("son process sigactionProcess end !pid is:%d\n", getpid()) ;
	//================================================================
	exit(0);
}
//訊號處理函式註冊
void    sigactionReg(void)
{
	struct sigaction act,oldact;

	act.sa_handler  = sigactionProcess;
	act.sa_flags = 0;

	//sigaction(SIGINT,&act,&oldact);
	sigaction(SIG_MY_DEFINE_TEST,&act,&oldact);
}



int main()
{
  int ret ;
  pid_t pid;/*pid 程序id號*/

  pid=fork();/*建立一個新程序*/

  if(pid==0) /*返回0為子程序*/
  {
      printf("son get Return pid is %d\n",pid);
      printf("This is son process!  pid is:%d\n",getpid());
      //訊號處理函式註冊
      sigactionReg();
      //raise(SIGSTOP) ;//子程序暫停
      while(1){}
      printf("son process end !pid is:%d\n", getpid()) ;
      exit(0) ;
  }
  else if(pid>0)/*返回大於0為父程序*/
  {
	  printf("parent get Return pid is %d\n",pid);
      printf("This is parent process!  pid is:%d\n",getpid());
      //
	  usleep(1000*10);
	    //獲取到pid子程序沒有退出,指定WNOHANG不會阻塞,沒有退出會返回0
      if ((waitpid(pid, NULL, WNOHANG)) == 0)
      {
          //if ((ret = kill(pid, SIGINT)) == 0)//向子程序發出SIGKILL訊號
          if ((ret = kill(pid, SIG_MY_DEFINE_TEST)) == 0)//向子程序發出SIG_MY_DEFINE_TEST訊號
          {
               printf("parent kill %d\n", pid) ;
          }
      }
      waitpid(pid,NULL,0);/*等待子程序退出*/
      exit(0) ;
  }
  else
  {
       perror("fork() error!");
       exit(-1) ;
  }
}

測試

parent get Return pid is 10990
This is parent process!  pid is:10986
son get Return pid is 0
This is son process!  pid is:10990
parent kill 10990
son process sigactionProcess end !pid is:10990

參考:

   Linux下發送自定義訊號 C++程式碼
   http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=989499
   
   [轉]linux 自定義訊號(1)
   http://m.blog.csdn.net/blog/jmflovezlf/18217213
   
       linux自定義訊號的處理
       http://my.oschina.net/u/566882/blog/174414
       
        linux程序通訊---幾個傳送訊號的函式(kill,raise,alarm,pause)
        http://blog.csdn.net/zzyoucan/article/details/9235685           
       
        Linux程序 Fork函式
        http://1793109.blog.51cto.com/1783109/593183