您的位置:首页 > 运维架构 > Linux

linux自定义信号,并kill测试

2015-07-07 11:49 597 查看
1. 自定义信号的说明

有时候我们需要在程序中利用信号来控制程序行为,linux为我们提供了2个已经定义的信号SIGUSR1和SIGUSR2,一般的程序利用这2个信号已经能满足需要,不过我最近需要一些其他信号来避免覆盖原来的信号处理函数。

上网查了一下,看到了下面的程序片段:

然后到系统里查了一下,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作为自定义信号。然后程序里就多了下面的代码:

结果当然是出错了咯,但是并不是这个定义方式的问题。在我程序中有下面的代码:

编译时才发现原来SIGRTMIN并不是一个常量,看了头文件里才知道:

原来是函数调用,运行时确定的。

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

在找到系统定义的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也有类似的情况。同时头文件下面也有:

改成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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: