Does Windows 7 recycle process id (PID) numbers?

限于喜欢 提交于 2019-12-06 18:11:02

问题


I have this little test proggy that tracks PID's as they are created and shut down. I am investigating a problem that my proggy has found and would like to ask you about this in order to have a better idea on what's going on.

When a windows process is started, it gets a PID but when the process is shut down, does the PID become retired (like a star basketballer's jersey number) or is it possible for a new, entirely unrelated, process to be created under that released PID?

Thanks


回答1:


Yes, process IDs may be recycled by the system. They become available for this as soon as the last handle to the process has been closed.

Raymond Chen discussed this matter here: When does a process ID become available for reuse?

The process ID is a value associated with the process object, and as long as the process object is still around, so too will its process ID. The process object remains as long as the process is still running (the process implicitly retains a reference to itself) or as long as somebody still has a handle to the process object.

If you think about it, this makes sense, because as long as there is still a handle to the process, somebody can call WaitForSingleObject to wait for the process to exit, or they can call GetExitCodeProcess to retrieve the exit code, and that exit code has to be stored somewhere for later retrieval.

When all handles are closed, then the kernel knows that nobody is going to ask whether the process is still running or what its exit code is (because you need a handle to ask those questions). At which point the process object can be destroyed, which in turn destroys the process ID.




回答2:


I ran a test for about an hour and in that time 302 processes exits and 70 of them had PIDs in common (same PID was used for a new process). So that would say they are reused frequently.




回答3:


Evidently, if the process is terminated, its PID is available for reuse.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms683215%28v=vs.85%29.aspx

Remarks

Until a process terminates, its process identifier uniquely identifies it on the system. For more information about access rights, see Process Security and Access Rights.



来源:https://stackoverflow.com/questions/26301382/does-windows-7-recycle-process-id-pid-numbers

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