CreateProcess使子进程在父进程被杀死时被杀死? [英] CreateProcess such that child process is killed when parent is killed?
问题描述
有无法调用 CreateProcess 的方法,杀死父进程会自动终止子进程?
Is there a way to call CreateProcess such that killing the parent process automatically kills the child process?
也许使用创建过程标志?
编辑
解决方案是创建一个作业对象,将父对象和子对象放在作业对象中。当父母被杀时,孩子就被杀了。我从这里得到代码:
杀死父进程进程被终止
注意@ wilx对于继承句柄的注释。
Edit
The solution is to create a job object, place both parent and child in the job object. Whent he parent is killed the child is killed. I got the code from here:
Kill child process when parent process is killed
Take note of @wilx's comment about inherited handles.
推荐答案
a href =http://stackoverflow.com/questions/6259055/createprocess-such-that-child-process-is-killed-when-parent-is-killed/6259099#6259099> Neil说是IMHO最好的办法。通过设置可以使作业拥有进程死亡时子进程被杀死使用 JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE 。 85%29.aspx> SetInformationJobObject()
。当父进程退出/死掉时,作业对象句柄将关闭。为了使其工作,必须由子进程继承作业句柄不。如果你还想跟踪子进程,那么你必须创建你的子进程被暂停,将它们添加到你的job对象,然后让它们运行。
Using jobs as Neil says is IMHO the best way. You can make the child processes get killed when the job owning process dies by setting JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
on the job object using SetInformationJobObject()
. The job object handle will be closed when your parent process exits/dies. For this to work it is essential that the job handle is not inherited by the child processes. If you want to track also the grand-child processes then you will have to create your child processes suspended, add them to your job object and only then let them run.
这篇关于CreateProcess使子进程在父进程被杀死时被杀死?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!