windows 确定进程是否已死 - 通过 PID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1822557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
determine if a process is dead or not - by PID
提问by Jessica
I have two different way to check whether a process is still up and running:
我有两种不同的方法来检查进程是否仍在运行:
1) using GetExitCodeProcess() 2) walking the list of processes using CreateToolhelp32Snapshot() and checking PIDs
1) 使用 GetExitCodeProcess() 2) 使用 CreateToolhelp32Snapshot() 遍历进程列表并检查 PID
now, in both cases I'm still getting that a process that I terminated with TerminateProcess is till alive even tho it is not.
现在,在这两种情况下,我仍然发现我用 TerminateProcess 终止的进程仍然存在,即使它不是。
Is there a way to positively know whether a process is still alive or dead passing the PID? thanks!
有没有办法肯定地知道一个进程是活着还是死了通过PID?谢谢!
回答by Remus Rusanu
Don't use PID for something like this. PIDs are reused and have a verynarrow range, with a veryhigh collision probability. In other words, you will find a running process but will be a differentprocess.
不要将 PID 用于这样的事情。PID 被重用并且具有非常窄的范围,具有非常高的碰撞概率。换句话说,您会发现一个正在运行的进程,但将是一个不同的进程。
回答by Andomar
A call to GetExitCodeProcess
should return STILL_ACTIVE
for active processes. After a call to TerminateProcess
, the process will be dead, and a different value will be returned.
对活动进程的调用GetExitCodeProcess
应该返回STILL_ACTIVE
。调用 后TerminateProcess
,进程将死亡,并返回不同的值。
Another way to check if a process is alive is WaitForSingleObject
. If you call this on the process handle with a timeout of 0, it will immediately return WAIT_TIMEOUT
if the process is still running.
检查进程是否处于活动状态的另一种方法是WaitForSingleObject
. 如果在超时为 0 的进程句柄上调用它,WAIT_TIMEOUT
如果进程仍在运行,它将立即返回。
回答by Hassan Syed
You cannot assume a low level API call functions the way it seems or how you think it should function from its name or high level description. A kernel still has things to do and often calls are just requests to the kernel and there are a multitude of things a kernel needs to do (depending on implementation) before it will actually release the PID. In this case after you issue the call you may assume the process is dead, however the kernel still has to clean up.
您不能假设低级 API 调用的功能与它的名称或高级描述一样,或者您认为它应该如何运行。内核仍然有事情要做,通常调用只是对内核的请求,在真正释放 PID 之前,内核需要做很多事情(取决于实现)。在这种情况下,在您发出调用后,您可能会认为该进程已死,但内核仍需进行清理。
The TerminateProcess function is use to unconditionally cause a process to exit. The state of global data maintained by dynamic-link libraries (DLLs) may be compromised if TerminateProcess is used rather than ExitProcess.
TerminateProcess initiates termination and returns immediately. This stops execution of all threads within the process and requests cancellation of all pending I/O. The terminated process cannot exit until all pending I/O has been completed or canceled.
A process cannot prevent itself from being terminated.
TerminateProcess 函数用于无条件地使进程退出。如果使用 TerminateProcess 而不是 ExitProcess,则由动态链接库 (DLL) 维护的全局数据的状态可能会受到损害。
TerminateProcess 启动终止并立即返回。这会停止进程内所有线程的执行并请求取消所有挂起的 I/O。在所有挂起的 I/O 完成或取消之前,终止的进程无法退出。
进程不能阻止自己被终止。
回答by Jason Evans
Could you make use of the Process Status API? There are functions for enumeratingall running processes on a system - this could help you.