#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<wait.h>
#include<unistd.h>
#include<signal.h>
void Myfun(int num) {
printf("捕捉到的信号%d\n", num);
while(1) {
int ret = waitpid(-1, NULL, WNOHANG);
if(ret > 0) {
printf("child die, pid = %d\n", ret);
} else if(ret == 0) {
break;
}
}
}
int main() {
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGCHLD);
sigprocmask(SIG_BLOCK, &set, NULL);
pid_t pid;
for(int i = 0; i < 20; i++) {
pid = fork();
if(pid == 0) {
break;
}
}
if(pid > 0) {
struct sigaction act;
act.sa_flags = 0;
act.sa_handler = Myfun;
sigemptyset(&act.sa_mask);
sigaction(SIGCHLD, &act, NULL);
sigprocmask(SIG_UNBLOCK, &set, NULL);
while(1) {
printf("parent process pid%d\n", getpid());
sleep(2);
}
} else if(pid == 0) {
printf("child process pid:%d\n", getpid());
}
return 0;
}
Linux SIGCHLD信号
最新推荐文章于 2024-05-17 16:41:20 发布