When we shut down the Linux system and start the system again, will it use the same PIDs it used before for the processes of the programs? Or these PIDs are just randomly generated? What happens if the system runs out of integers that it can use as PIDs?
-
5Does this answer your question? How are PIDs assigned in RHEL7 and are they reused after a reboot?. The answers are not specific to RHEL. – Eduardo Trápani Jan 19 '22 at 19:13
1 Answers
The PIDs in Linux are generated in order from 0 to maximum PID number contained in the file /proc/sys/kernel/pid_max
. Historically, the max PID is 32767 because a page frame in Linux contains 32,768 bits or 4KB, and can hold the bit vector describing the PIDs under use. PIDs are numbered sequentially, assigned in an increasing order and are reused in the long run.
PIDs are not reused immediately to prevent race conditions. For example, consider a situation where a process sends a signal to another process. However, before the signal is received, the recipient has terminated and the PID is reassigned to another newly created process. In this situation, the signal is sent to the wrong process and hence, Linux does not reuse the PIDs of terminated processes right away.
When the system runs out of PIDs (max PID reached), Linux reuses the PIDs from the beginning that are not in use.

- 6,008
-
1It's actually
1 .. 32768
. No idea where 0 and 32757 come from but that certainly doesn't apply to modern Linux kernels. – Artem S. Tashkinov Jan 19 '22 at 22:03 -
1+1 to @ArtemS.Tashkinov comment regarding pid 0 and 32768 > pid > 32757. In addition, please note that since 2.6.10, 32.768 is no longer an hard limit. Only a default value. Max pid can be tuned via sysctl kernel.pid_max up to… 4,194,304. – MC68020 Jan 19 '22 at 23:03
-
1And BTW pid 0 must exist and be reserved even if it is not associated to some "real" process since... init and kthreadd ppid = 0. – MC68020 Jan 19 '22 at 23:36