# kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX #
#include <signal.h> typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler);
#include <sys/types.h> #include <signal.h> int kill(pid_t pid, int sig);
参数pid的值 | 信号的接收进程 |
---|---|
pid>0 | 进程ID为pid的进程 |
pid=0 | 同一个进程组的进程 |
pid<-1 | 进程组ID为 -pid的所有进程 |
pid=-1 | 除进程1外,所有有向目标进程发送信号权限的进程 |
void sig_func(int signo) { printf("====%s== [child] handle signo: %d===\n", __func__, signo); } void child_process_do(void) { printf("====%s==child pid: %d===\n", __func__, getpid()); signal(SIGRTMIN, sig_func); //子进程注册信号SIGRTMIN处理函数sig_func //signal(SIGALRM, sig_func); while (1) { sleep(10); } } void parent_process_do(pid_t pid) { int i; sleep(1); //等待子进程先注册完信号处理函数,否则系统如果先调用父进程先执行,在调用kill之后子进程会退出,看不到我们需要的效果 printf("====%s==parent pid: %d===\n", __func__, getpid()); for (i = 0; i < 5; i++) { printf("====%s==[parent] send signal <%d> to pid <%d>==\n", __func__, SIGRTMIN, pid); kill(pid, SIGRTMIN); //父进程连续每隔一秒向子进程发一个SIGRTMIN信号 //kill(pid, SIGALRM); sleep(1); } waitpid(pid, NULL, 0); //父进程等待子进程结束 } int main(int argc, const char *argv[]) { pid_t pid; pid = fork(); if (pid < 0) { fprintf(stderr, "fork: %s\n", strerror(errno)); return -1; } if (0 == pid) { child_process_do(); //子进程 } else { parent_process_do(pid); //父进程 } return 0; }
void sig_func(int signo) { sleep(6); printf("====%s== [child] handle signo: %d===\n", __func__, signo); }
# ./target_bin ====child_process_do==child pid: 7159=== ====parent_process_do==parent pid: 7158=== ====parent_process_do==[parent] send signal <34> to pid <7159>== ====parent_process_do==[parent] send signal <34> to pid <7159>== ====parent_process_do==[parent] send signal <34> to pid <7159>== ====parent_process_do==[parent] send signal <34> to pid <7159>== ====parent_process_do==[parent] send signal <34> to pid <7159>== ====sig_func== [child] handle signo: 34=== ====sig_func== [child] handle signo: 34=== ====sig_func== [child] handle signo: 34=== ====sig_func== [child] handle signo: 34=== ====sig_func== [child] handle signo: 34===
void child_process_do(void) { printf("====%s==child pid: %d===\n", __func__, getpid()); //signal(SIGRTMIN, sig_func); signal(SIGALRM, sig_func); while (1) { sleep(10); } } void parent_process_do(pid_t pid) { int i; sleep(1); printf("====%s==parent pid: %d===\n", __func__, getpid()); for (i = 0; i < 5; i++) { printf("====%s==[parent] send signal <%d> to pid <%d>==\n", __func__, SIGRTMIN, pid); //kill(pid, SIGRTMIN); kill(pid, SIGALRM); sleep(1); } waitpid(pid, NULL, 0); }
# ./target_bin ====child_process_do==child pid: 7259=== ====parent_process_do==parent pid: 7258=== ====parent_process_do==[parent] send signal <34> to pid <7259>== ====parent_process_do==[parent] send signal <34> to pid <7259>== ====parent_process_do==[parent] send signal <34> to pid <7259>== ====parent_process_do==[parent] send signal <34> to pid <7259>== ====parent_process_do==[parent] send signal <34> to pid <7259>== ====sig_func== [child] handle signo: 14=== ====sig_func== [child] handle signo: 14===
void sig_func(int signo) { //sleep(6); printf("====%s== [child] handle signo: %d===\n", __func__, signo); } void child_process_do(void) { printf("====%s==child pid: %d===\n", __func__, getpid()); //signal(SIGRTMIN, sig_func); signal(SIGALRM, sig_func); /* while (1) { sleep(10); } */ sleep(10); }
# ./target_bin ====child_process_do==child pid: 7450=== ====parent_process_do==parent pid: 7449=== ====parent_process_do==[parent] send signal <34> to pid <7450>== ====sig_func== [child] handle signo: 14=== ====parent_process_do==[parent] send signal <34> to pid <7450>== ====parent_process_do==[parent] send signal <34> to pid <7450>== ====parent_process_do==[parent] send signal <34> to pid <7450>== ====parent_process_do==[parent] send signal <34> to pid <7450>==
void child_process_do(void) { printf("====%s==child pid: %d===\n", __func__, getpid()); //signal(SIGRTMIN, sig_func); signal(SIGALRM, sig_func); /* while (1) { sleep(10); } */ while (sleep(10) > 0) { } }
原文地址:http://blog.csdn.net/shallnet/article/details/41451601