c - 父进程终止后如何杀死所有子进程?
问题描述
我可以通过父进程杀死一个子进程。但是如果一个父进程有多个子进程怎么办?
例如在下面的代码中,有 1 个父进程和 6 个子进程。如何在父进程终止后立即杀死其他六个子进程?
如果您运行此代码,则父进程将在 5 秒后终止。在该子进程再过 5 秒(总共 10 秒)后终止。
但是我想在父进程终止后立即杀死 6 个子进程。所以父进程和 6 个子进程应该在 5 秒后终止。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
for(int i=0;i<6;i++) // loop will run 6 times(there are 6 child processes.)
{
if(fork() == 0)
{
printf("Started [son] pid %d from [parent] pid %d\n",getpid(),getppid());
sleep(10); //child waits 10 seconds,then it exitted.
printf("Exitted [son] pid %d from [parent] pid %d\n",getpid(),getppid());
exit(0);
}
}
//parent
sleep(5); //parent will wait 5 seconds than it will exit
printf("Parent terminated\n");
exit(0); //parent terminated.(how can I exit the the other 6 child processes too?)
}
解决方案
在 Linux 上,您可以prctl
通过信号(已跳过错误检查)请求通知您父母的死亡。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/prctl.h> //<<<<<<<<
#include <signal.h> //<<<<<<<<
int main()
{
for(int i=0;i<6;i++) // loop will run 6 times(there are 6 child processes.)
{
if(fork() == 0)
{
prctl(PR_SET_PDEATHSIG, SIGTERM); //<<<<<<
printf("Started [son] pid %d from [parent] pid %d\n",getpid(),getppid());
sleep(2);
printf("Exitted [son] pid %d from [parent] pid %d\n",getpid(),getppid());
exit(0);
}
}
//parent
sleep(1);
printf("Parent terminated\n");
exit(0);
//<<< Linux auto-sends the deathsignal to all children
}
对于不要求父进程在死亡时显式杀死其子进程的 POSIX 兼容解决方案,您可以使用 async-IO 管道。
Async-IO 依赖于在文件描述符事件上发送的信号。在这种情况下,只要您确保自动关闭关闭对管道端文件的最后引用(跳过错误检查),您就可以收到由内核自动关闭垂死进程的文件描述符引起的关闭事件的通知:
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/ioctl.h>
int main()
{
int pipes[6][2];
for(int i=0;i<6;i++) // loop will run 6 times(there are 6 child processes.)
{
pipe(pipes[i]); //create a pipe
if(fork() == 0)
{
//get notified on an event on the read-end (we're aiming for the EOF event)
fcntl(pipes[i][0],F_SETOWN,getpid());
ioctl(pipes[i][0], FIOASYNC, &(int){1});
for(int j=0; j<=i; j++) close(pipes[j][1]); //close all write-end ends so the refcount is 1 and the parent has the last ref
printf("Started [son] pid %d from [parent] pid %d\n",getpid(),getppid());
sleep(2);
printf("Exitted [son] pid %d from [parent] pid %d\n",getpid(),getppid());
exit(0);
}
}
//parent
sleep(1);
printf("Parent terminated\n");
exit(0);
//<<<this closes all the last write ends of the pipes and so the children will get notified with a signal
//the signal is SIGIO by default, whose default disposition is to kill the process (this can be changed by fcntl(fd,F_SETSIG,TheSignal))
}
推荐阅读
- git - 如何从 git 历史中提取模块到新的 repo
- python - 使用 Django 的每个订单行和全球的商品总价
- javascript - Javascript 中的循环是如何执行的?
- azure-data-factory - Azure DataFactory 将数据从一个数据库复制到另一个数据库,数据类型问题
- ruby-on-rails - 向 has_and_belongs_to_many 关系添加多条记录
- android - 在 android studios 上开发应用程序时是否有做笔记的约定
- python - 基于 Python 的 UDP 交叉开关多路复用器
- python - 如何使用 Manim 为对象颜色变化设置动画?
- nservicebus - NServiceBus:访问功能中的可恢复性
- julia - 在 Julia 的 CSV 文件中解析 DateTime