需要捕获SIGUSR信号需要调用kill函数来实现!!!
kill -s SIGUSR1 pid
//signal函数
// void(*signal(int signo,void (*func)(int)))(int)
//func的值可以是接到信号后调用的函数的地址
#include "apue.h"
static void sig_usr(int);
int main(void){
if(signal(SIGUSR1,sig_usr)==SIG_ERR)
err_sys("can't catch SIGUSR1");
if(signal(SIGUSR2,sig_usr)==SIG_ERR)
err_sys("can't catch SIGUSR2");
for(;;)
pause();//使调用进程在接到一个信号前挂起
}
static void sig_usr(int signo){
if(signo==SIGUSR1)
printf("received SIGUSR1\n");
else if(signo==SIGUSR2)
printf("received SIGUSR2\n");
else
err_dump(received signal %d\n",signo);
}
测试:
$./a.out &
[1] 7216
$kill -USR1 7216
received SIGUSR1
$kill -USR2 7216
received SIGUSR2
$kill 7216
[1]+ Terminated ./a.out