3 ms·
> A new pid is always more than the previously assigned pid during an OS uptime This isn't true. The pid increments most of the time, but occasionally the new
by jlokier 3y ago
> A new pid is always more than the previously assigned pid during an OS uptime
This isn't true. The pid increments most of the time, but occasionally the new pid jumps down to a seemingly arbitrary value to start a new range. It is switching between unused ranges. This happens on my Linux machines all the time.
However, it doesn't matter for what the article describes: it doesn't depend on the pid always increasing, it only depends on the pid for each process being unique.
Pid reuse doesn't matter often, but it matters occasionally with how sigals are often used. Many scripts are written to signal a process that might have terminated already, e.g. using a pid stored in an environment variable or file. This usually just returns an error, but if there has been enough system activity meanwhile, the pid may belong to a new process, which the script does not intend to signal, so a random process may be killed unexpectedly.
- duped 3y agoI feel a bit nerd sniped: On Linux PIDs are only unique within a PID namespace. But that doesn't matter because the entire point of a PID namespace is to isolate processes. But it's worth mentioning because it can mitigate the problem you've laid out. If you have a long running process that may need to kill its children, you can start with within a new PID namespace such that it may only kill its children (or descendant).
- rcr 3y agoLinux introduced pidfds for this reason.
- goodyduru 3y agoAuthor here. Thanks for the feedback! Corrected the error.
- somat 3y agoAlso note that openbsd uses random pids.
- Lammy 3y agoAnd FreeBSD if so configured: $ sysctl kern.pid_max kern.randompid kern.pid_max: 99999 kern.randompid: 594 D12238 sez “If set to a value of (100 <= kern.randompid <= (PID_MAX - 100)), it will be used as modulus when kern_fork.c tries to allocate new PID. Formula is lastpid += arc4random() % kern.randompid”