问题
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 ?
回答1:
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
回答2:
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.
回答3:
New pidfd functionality to help service managers to deal with PID reuse problems
To solve the issue of PID reuse in Unix systems,
Linux 5.1 added the pidfd_send_signal(2)
, which let processes send signals to pidfd
handles that are stable even after PID reuse.
Linux 5.2 added the CLONE_PIDFD
to clone(2)
, which let users to create PIDs that were usable with pidfd_send_signal(2)
. But there are many processes creates with fork()
or clone()
without CLONE_PIDFD
, which can cause problems for the Android's low memory killer (LMK) or service managers such as systemd.
Linux 5.3 added the pidfd_open(2)
syscall, to complete the functionality needed to deal with the PID reuse issue. It allows a caller to retrieve pollable pidfd's for a process which did not get created using the CLONE_PIDFD
clone(2)
flag.
Additionally, Linux 5.3 release adds polling support for pidfds
. This allows process managers to know when a (non-parent) process dies in a race-free way. The notification mechanism used follows the same logic that is currently used when the parent of a task is notified of a child's death. With this patchset it is possible to put pidfds
in an {e}poll loop and get reliable notifications for process (i.e. thread-group) exit.
Recommended LWN article: New system calls: pidfd_open() and close_range()
来源:https://stackoverflow.com/questions/11323410/linux-pid-recycling