#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
void handler(int arg) { printf("receive SIGCHLD\n"); } 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 \n"); sleep(1); } } else //父进程 { sleep(1); //睡 1 秒 kill(pid,SIGKILL);//杀死 pid 发送进程的信号,kill 给其他进程发送信号,指定进程号 printf("child killed\n"); printf("father \n"); wait(NULL); //等待回收子进程的资源 raise(SIGKILL); //杀死自己的信号,函数raise 给自己发送信号 } return 0; }

 测试:

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

 

相关文章:

  • 2022-03-04
  • 2021-12-14
  • 2021-11-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-29
  • 2021-09-11
  • 2021-10-30
  • 2021-10-29
  • 2021-09-07
相关资源
相似解决方案