#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <signal.h> #include <unistd.h>
void handler(int arg) { printf("receive SIGCHLDn"); } int main(int argc, const char *argv[]) { signal(SIGCHLD,handler); //注册信号回调函数,当信号发生会调用handler pid_t pid; pid = fork(); if(pid < 0) { perror("fork fail "); exit(1); } elseif(pid == 0) //子进程 { while(1) { printf("child n"); sleep(1); } } else //父进程 { sleep(1); //睡 1 秒 kill(pid,SIGKILL);//杀死 pid 发送进程的信号,kill 给其他进程发送信号,指定进程号 printf("child killedn"); printf("father n"); wait(NULL); //等待回收子进程的资源 raise(SIGKILL); //杀死自己的信号,函数raise 给自己发送信号 } return 0; }
测试:
原文:https://www.cnblogs.com/electronic/p/10945532.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!