Does Windows 7 recycle process id (PID) numbers?
Asked Answered
D

3

23

I have this little test program that tracks PID's as they are created and shut down. I am investigating a problem that my program 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

Disaccharide answered 10/10, 2014 at 14:8 Comment(2)
Yes, it does. So you have to keep a handle to the process to make sure it's still alive - as long as it is, the ID will stay the same.Abutment
I wonder if a HANDLE (an int) remains unique.Callow
U
30

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.

Uranyl answered 10/10, 2014 at 14:24 Comment(3)
So that means that instead of process identifier it should be called Running Process Identifier as it does not identifies a single process.Dandruff
In tools like ProccessExplorer I can see, that a process also have a "ParentProcessId" even if that one does not exist any longer. Is the re-use of such PID also blocked as long as any process had this PID in its "ParentProcessId"?Ecotone
The re-use of PIDs is not blocked. I think you have got your logic the wrong way round. PIDs get re-used.Uranyl
M
25

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.

Maillol answered 7/7, 2015 at 0:41 Comment(1)
Excellent, I wonder if the process HANDLE is unique.Callow
S
9

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.

Salita answered 10/10, 2014 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.