Linux PID recycling [closed]
Asked Answered
B

2

82

Is there any policy in Linux as to the recycling of used PIDs ? I mean, if a PId has been used, how much later will it be used again ?

Bradfordbradlee answered 4/7, 2012 at 6:18 Comment(5)
I'm curious too, was just wondering about how looking up a process by a pid will work if something new took the old pidNovice
Seems to me that PIDs should only be used to identify currently running processes. If you follow that policy, the exact way in which PIDs get recycled is never going to affect you. Trying to rely on how PIDs get recycled is fragile. For that reason alone, you should not do it. Furthermore, I can't imagine how such behavior could be useful. The only thing you might want to know is how large PIDs can be. E.g. one consequence of this is how many processes your system supports. Notice that no knowledge of how recycling works is needed to arrive at this realization.Romanticize
@Romanticize try writing a program like top without worrying about how PIDs are recycled.Novice
To give you a real life example of how often the PID may repeat itself, I'm seeing around every ~2 hours on highly used Ubuntu hosts, sometimes even more often.Illyricum
@Romanticize "Seems to me that PIDs should only be used to identify currently running processes. If you follow that policy, the exact way in which PIDs get recycled is never going to affect you." Really? What if I fork(), get a pid_t back, do something else, go check on the status of the child process, and in the meantime it's terminated and a new process has taken its pid?Linger
B
69

As new processes fork in, PIDs will increase to a system-dependent limit and then wrap around. The kernel will not reuse a PID before this wrap-around happens.

The limit (maximum number of pids) is /proc/sys/kernel/pid_max. The manual says:

/proc/sys/kernel/pid_max (since Linux 2.5.34)

This file specifies the value at which PIDs wrap around (i.e., the value in this file is one greater than the maximum PID). The default value for this file, 32768, results in the same range of PIDs as on earlier kernels

Branca answered 4/7, 2012 at 6:20 Comment(2)
OK. So this increasing policy is followed strictly ? Or it can reuse a PID before the limit has been reached ?Bradfordbradlee
I thought a pid had been reused before the wrap, but after ps -A -L -o lwp |sort -n I found pids about 32372 were still in use; so my guess is it is wrapping around very fast on my system! even if not too often, but at least on boot.Triboluminescence
C
10

https://superuser.com/questions/135007/how-are-pids-generated

This should answer your question - it appears it will recycle PIDs when it runs out, skipping the ones that are still assigned.

Confection answered 25/6, 2013 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.