Linux 父进程发送信号杀死子进程

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
void handler(int arg) { printf("receive SIGCHLD "); } int main(int argc, const char *argv[]) { signal(SIGCHLD,handler); //注册信号回调函数,当信号发生会调用handler pid_t pid; pid = fork(); if(pid < 0) { perror("fork fail "); exit(1); } else if(pid == 0) //子进程 { while(1) { printf("child "); sleep(1); } } else //父进程 { sleep(1); //睡 1 秒 kill(pid,SIGKILL);//杀死 pid 发送进程的信号,kill 给其他进程发送信号,指定进程号 printf("child killed "); printf("father "); wait(NULL); //等待回收子进程的资源 raise(SIGKILL); //杀死自己的信号,函数raise 给自己发送信号 } return 0; }

 测试:

原文地址:https://www.cnblogs.com/electronic/p/10945532.html