Does Windows 7 reuse process IDs?

7,439

Solution 1

From my testing it appears that you have one false assumption, PID numbers are not given out in sequential order. This is very easy to prove, do the following command from the command line. It should open 3 copies of notepad.

notepad & notepad & notepad

On my machine here are the PID's of the 3 copies that where all opened at the same time.

enter image description here

As you can see the PID's jump around a lot, If you open them up one at a time you will also see that the next PID is not always larger than the previous one. For example I opened up a 4th copy of notepad and got this

enter image description here

So it appears that Windows 7 will just pick a random unused PID every time it starts a process, so there very well could have a PID reused throughout the running of windows without a reboot.


I wrote up a simple powershell script (requires v2 or newer, see this answers edit history for a C# version) to prove it for sure

$h = new-object 'System.Collections.Generic.HashSet[string]'
do {
    $proc = Start-Process 'notepad' -PassThru
    $id = $proc.Id
    Stop-Process $id
} while ($h.Add($id))
$count = $h.Count
Write-Host "Took $count PIDs to hit a duplicate, the duplicate was $id."

Running the program 10 times it always took between 134 and 147 launches of notepad for the same PID to be re-used (Why is this number so small? GO-GO Gadget Birthday Problem!)

Solution 2

I ran a test for an hour and in that time 302 processes exited. Of those, 70 had a PID in common, so I would say the PID gets reused frequently.

Share:
7,439

Related videos on Youtube

isildur
Author by

isildur

Long time IT grunt. One of those people who knows being called "expert" means you actually know that you don't know what everyone thinks you know. :)

Updated on September 18, 2022

Comments

  • isildur
    isildur almost 2 years

    Does Windows 7 reuse process IDs?

    The reason I ask this question is due to my experience that Windows XP and Linux never seems to generate process IDs higher than 20–30k. However, my Windows 7 machine will reach IDs as high as 5–10k or so within a few hours after a reboot, which is my normal experience from the past. The next morning I check and some processes are 250k or higher, which is not.

    I activated the security auditing feature to log process creation and termination. Nothing is generating hundreds or thousands or processes. Only 513 of these events are registered for a 24 hour period, yet hundreds of thousands of process IDs have been used, it appears.

    I tried a search for my question and one of the suggested questions previously asked pointed to a Mark Russinovich's marvelous blog. But this article, while very interesting reading, has left me puzzled.

    • Austin T French
      Austin T French almost 11 years
      I bet they reset after a reboot...
    • user1686
      user1686 almost 11 years
      Of course they do, because there are no processes at all when boot starts, therefore all PIDs > 4 are unused.
    • J. Shmoe
      J. Shmoe almost 11 years
      I think his/her point was that unless Windows reuses PIDs eventually, it will run out and require a reboot.
  • user1686
    user1686 almost 11 years
    Windows NT makes it even faster because PIDs are always multiples of four.
  • isildur
    isildur almost 11 years
    Thanks Scott. Is there a shell script/utility that could track all processes PID numbers and log it to a file with the name of the process they were assigned too?
  • Scott Chamberlain
    Scott Chamberlain almost 11 years
    You could, but I don't see the usefulness of it. Are you just wanting to track every processes started or do you really care about the PID?
  • isildur
    isildur almost 11 years
    At this point, I just want to know what process(s) is being run so many times that it pushed the process id numbers into the 500k+ range. Perhaps a process is spawning processes even. This is just a basic win 7 desktop, antivirus and firefox, a game or two and we do not do much else on it. Thanks for the replies.
  • isildur
    isildur over 10 years
    So I removed an old utility that indexed disk drives using batch files that someone had written years ago and it turns out this was the culprit generating such high PID numbers. Now the same machine has ran for weeks and the highest PID number to date is 12252.
  • Tom Andraszek
    Tom Andraszek over 5 years
    An interesting thing is that in Windows 10 this behavior changed. On Windows 7 and 8.1 the above ps script returns a duplicate in about 2 seconds after about 110-130 process starts/stops. On Windows 10 it keeps going and going, usually incrementing the PID by multiplies of 4 and sometimes by jumping up or down by thousands. I stopped it at 221896 after a few minutes.