CreateProcess 使得当父进程被杀死时子进程也被杀死?
有没有办法调用 CreateProcess这样杀死父进程会自动杀死子进程吗?
也许使用创建进程标志?
编辑
解决方案是创建一个作业对象,将父对象和子对象都放置在作业对象中。当父母被杀时,孩子也被杀。我从这里得到了代码: 当父进程被终止时终止子进程 请注意 @wilx 关于继承句柄的评论。
Is there a way to call CreateProcess such that killing the parent process automatically kills the child process?
Perhaps using Create Process Flags?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将作业用作 Neil 说< /a> 恕我直言,这是最好的方法。您可以通过设置
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
使用SetInformationJobObject()
。当您的父进程退出/终止时,作业对象句柄将关闭。为此,子进程不继承作业句柄至关重要。如果您还想跟踪孙进程,那么您将必须创建挂起的子进程,将它们添加到您的作业对象中,然后才让它们运行。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 usingSetInformationJobObject()
. 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.您能做的最好的事情就是将两个进程放在同一个作业中,这样杀死该作业就会杀死这两个进程。
The best you can do is to put both processes in the same job, so that killing the job kills both processes.
您是否需要杀死子进程,或者只是检测父进程退出以便它可以干净地终止?父进程可以为其自身创建一个可继承的句柄,然后子进程可以将其与其自己的对象一起传递给
WaitForMultipleObjects(Ex)
。如果子进程不是专门为此编写的,您可以将其标准输入附加到管道,该管道的另一端由父进程保存。如果父级死亡,管道将自动关闭。
这与 Unix 行为非常相似,其中子级不会在其父级死亡时被杀死,它通常会响应
SIGHUP
而退出(但它可以处理该信号并实现任何行为)。在 Linux 上,孤儿的父 PID 更改为 1 (init)。Do you need the child process to be killed, or merely detect the parent process exit so it can terminate cleanly? The parent process can create an inheritable handle to itself, which the child can then pass to
WaitForMultipleObjects(Ex)
along with its own objects.If the child process isn't written specifically for this, you could attach its stdin to a pipe, the other end of which is held by the parent process. If the parent dies, the pipe is automatically closed.
This closely parallels the Unix behavior, in which a child isn't killed when its parent dies, it generally exits in response to
SIGHUP
(but it can handle that signal and implement any behavior). On Linux, the parent PID of an orphan is changed to 1 (init).一种不同的方法
在所有情况下都没有用,但是我有一个特定的场景,其中应用程序完全控制子进程。用于通讯和API 拦截,需要 DLL 注入,以便 DLL 在实际子进程中运行以向父进程报告。
如果您控制子进程并拥有注入的 DLL,则可以通过监视父进程来检查它是否正在运行来扩展此 DLL。如果没有,
ExitProcess
。如果子进程中不需要 DLL,其他解决方案很可能会更好。
例如,您可以通过应用程序已使用的注册表项来传输
parentProcessID
。或者,如果这不会破坏子进程,您可以传递命令行。这必须在自己的线程中运行。我的 DLL 有两个线程:这里的代码和控制 & 线程。通讯代码。
DLL
A different approach
Not useful in all situations, however I had one particular scenario where an application was controlling a child process completely. For communication & API interception, a DLL injection was required so a DLL is running in the actual child processes to report back to the parent.
If you control the child processes and have an injected DLL anyway, you can extend this DLL by monitoring the parent process to check if it is running. If not,
ExitProcess
.If you don't need a DLL in the child process, the other solutions are most likely far better.
You can transfer the
parentProcessID
through, for instance, a registry key that you use by your application already. Or you can pass a commandline, if this doesn't break the child process.This has to run in its own thread. My DLL had two threads: This code here and the controlling & communication code.
DLL
我想
DEBUG_PROCESS
或DEBUG_ONLY_THIS_PROCESS
会产生几乎偶然的副作用。 Windows 并不像类 Unix 系统那样在树中创建进程。I suppose
DEBUG_PROCESS
orDEBUG_ONLY_THIS_PROCESS
would do that as an almost-accidental side-effect. Windows doesn't create processes in a tree the way Unix-like systems do though.不知道windows,但这可以在linux上运行:
prctl(PR_SET_PDEATHSIG, SIGHUP);
Dont know about windows, but this will work on linux:
prctl(PR_SET_PDEATHSIG, SIGHUP);