1 信号函数
代码示例
#include "ourhdr.h" #include "signal.h" static void sig_usr(int); /* one handler for both signals */ 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) /* argument is signal number */ { if (signo == SIGUSR1) printf("received SIGUSR1\n"); else if (signo == SIGUSR2) printf("received SIGUSR2\n"); else err_dump("received signal %d\n", signo); }
运行结果:
[root@rac1 signal]# ./a.out & [1] 10516 [root@rac1 signal]# kill -USR1 10516 received SIGUSR1 [root@rac1 signal]# kill -USR2 10516 [root@rac1 signal]# received SIGUSR2 [root@rac1 signal]# kill 10516 [root@rac1 signal]# [1]+ Terminated ./a.out