1. 使用signal(int signal, function)向内核注册信号处理函数
2. 使用raise()向本进程发送信号,通过kill()向其他进程发送信号
#include <stdio.h> #include <signal.h> #include <unistd.h> #include <stdlib.h> void signal_handle(int signal); int main(int argc, char **argv) { signal(SIGUSR1, signal_handle); int cpid, ppid; if ((cpid = fork()) == 0) { ppid = getppid(); cpid = getpid(); printf("cid:%d, ppid:%d\n", cpid, ppid); kill(ppid, SIGUSR1); printf("send signal to parent\n"); } else { int pid = wait(NULL); printf("pid after wait:%d\n", pid); } } void signal_handle(int signal) { printf("receive signal:%d\n", signal); }
来源:https://www.cnblogs.com/luckygxf/p/12302958.html