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 ?
Asked
Active
Viewed 3.0k times
81
-
5I'm curious too, was just wondering about how looking up a process by a pid will work if something new took the old pid – notbad.jpeg Jul 04 '12 at 06:19
-
2Seems 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. – allyourcode Nov 06 '15 at 03:15
-
8@allyourcode try writing a program like `top` without worrying about how PIDs are recycled. – notbad.jpeg Jan 11 '16 at 23:21
-
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. – Phoebe Nov 15 '20 at 22:14
2 Answers
68
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

cnicutar
- 178,505
- 25
- 365
- 392
-
OK. So this increasing policy is followed strictly ? Or it can reuse a PID before the limit has been reached ? – Cygnus Jul 04 '12 at 06:21
-
1I 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. – Aquarius Power Jun 12 '14 at 21:12
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.

Community
- 1
- 1

Osmium USA
- 1,751
- 19
- 37