简体   繁体   English

通过清理杀死子进程

[英]Kill child process with cleanup

是否有办法杀死我的应用程序的子进程并执行其清理(调用解构函数和atexit函数),类似于exit(exit_code) ,但是在另一个进程上呢?

If you are on windows, you probably start your child processes by CreateProcess, which has a PROCESS_INFORMATION as the last parameter. 如果您在Windows上,则可能通过CreateProcess启动子进程,该进程的最后一个参数为PROCESS_INFORMATION。

CreateProcess on MSDN MSDN上的CreateProcess

Process Information on MSDN MSDN上的过程信息

Option 1: 选项1:

This process information contains a handle to the process started in the hProcess member. 此过程信息包含在hProcess成员中启动的过程的句柄。

You can store this handle and use it to kill your child processes. 您可以存储此句柄并使用它杀死您的子进程。

Insert You probably want to send WM_CLOSE and / or WM_QUIT? 插入您可能想发送WM_CLOSE和/或WM_QUIT? to "cleanly" end the process: 以“干净”地结束该过程:

Here is a KB Article on what to do KB how to cleanly kill win32 processes 这是有关如何执行KB的文章, KB如何彻底清除win32进程

** End Insert** **末端插入**

Option 2: 选项2:

Here is an discussion on how to properly kill a process tree: Terminate a process tree on windows 这是有关如何正确终止进程树的讨论: 在Windows上终止进程树

There's no simple Win32 API for that kind of thing. 这类事情没有简单的Win32 API。 The OS doesn't care what language your program's source code was written in, the compiled program appears to it as just a sequence of CPU instructions plus data. 操作系统不在乎您的程序源代码是用什么语言编写的,编译后的程序似乎只是一系列CPU指令和数据。

The cleanest way would be to establish some kind of a communication channel between the processes (eg via shared memory) and simply request process termination. 最干净的方法是在进程之间建立某种通信通道(例如,通过共享内存),并简单地请求进程终止。

You can achieve the same by starting the child process as a debugged process and then using debug APIs to alter the child's behavior, but that's too intrusive and not very straightforward to implement. 您可以通过将子进程作为已调试的进程启动,然后使用调试API来更改子进程的行为来达到相同目的,但这太麻烦了,实现起来也不是那么简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM