What happens to a process handle once the process was ended?

久未见 提交于 2019-12-22 12:23:17

问题


if I have a handle to some windows process which has stopped (killed or just ended):

  1. Will the handle (or better the memory behind it) be re-used for another process?
  2. Or will GetExitCodeProcess() for example get the correct result forever from now on?

If 1. is true: How "long" would GetExitCodeProcess() work?
If 2. is true: Wouldn't that mean that I can bring down the OS with starting/killing new processes, since I create more and more handles (and the OS reserves memory for them)?

I'm a bit confused about the concept of handles.

Thank you in advance!


回答1:


The handle indirectly points to an kernel object. As long as there are open handles, the object will be kept alive.

Will the handle (or better the memory behind it) be re-used for another process?

The numeric value of the handle (or however it is implemented) might get reused, but that doesn't mean it'll always point to the same thing. Just like process IDs.

Or will GetExitCodeProcess() for example get the correct result forever from now on?

No. When all handles to the process are closed, the process object is freed (along with its exit code). Note that running process holds an implicit handle to itself. You can hold an open handle, though, as long as you need it.

If 2. is true: Wouldn't that mean that I can bring down the OS with starting/killing new processes, since I create more and more handles (and the OS reserves memory for them)?

There are many ways to starve the system. It will either start heavily swapping or just fail to spawn a new process at some point.




回答2:


Short answer:

GetExitCodeProcess works until you call CloseHandle, after what the process object will be released and may be reused.

Long answer: See Cat Plus Plus's answer.



来源:https://stackoverflow.com/questions/6531620/what-happens-to-a-process-handle-once-the-process-was-ended

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!