杀父母的所有子进程

杀父母的所有子进程

本文介绍了杀父母的所有子进程,但留下父活着的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是杀父母的所有进程的最佳途径,但不是父?比方说,我有,我已经分叉并在指定的报警子进程的人数不详,在我的信号处理程序,我想杀死我的所有子进程,但让自己运行各种原因。

What would be the best way to kill all the processes of a parent but not the parent? Let's say I have an undetermined number of child processes that I've forked and on a given alarm, in my signal handler, I'd like to kill all my child processes but keep myself running for various reasons.

截至目前,我使用kill(-1 * parentPid,SIGKILL),但这个杀死我的父母,其子女一起。

As of now, I am using kill(-1*parentPid, SIGKILL) but this kills my parent along with its children.

推荐答案

要做到这一点的方法之一是带来一些信号可以被捕获(未 SIGKILL )。然后,安装,可检测当前进程的父进程或没有信号处理程序,并调用 _exit()如果不是父进程。

One way to accomplish this is to deliver some signal that can be caught (not SIGKILL). Then, install a signal handler that detects if the current process is the parent process or not, and calls _exit() if it is not the parent process.

您可以使用 SIGUSR1 SIGUSR2 ,或者 SIGQUIT

我已经说明了这个技术。

I've illustrated this technique here.

可选(由立冬的建议),父进程可以使用 SIG_IGN 签发前的信号杀()命令。

Optionally (as suggested by Lidong), the parent process can use SIG_IGN on the signal before issuing the kill() command.

signal(SIGQUIT, SIG_IGN);
kill(-parent_pid, SIGQUIT);

这篇关于杀父母的所有子进程,但留下父活着的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 12:46