If we type ps -ef then we get a list of processes. Why are the PID numbers not continuous?
- 6,316
- 6
- 28
- 42
- 193
4 Answers
On Ubuntu they are continuous. On other operating systems it might differ.
The kernel allocates PIDs in the range of (RESERVED_PIDS, PID_MAX_DEFAULT). It does so sequentially in each namespace (tasks in different namespaces can have the same IDs). In case the range is exhausted, pid assignment wraps around.
( https://stackoverflow.com/questions/3446727/how-does-linux-determine-the-next-pid )
Mind though ...
- Kernel scheduling can fork a process so it might appear to skip numbers.
- A PID will disappear when that task ends.
- PIDs are not re-used until PID_MAX_DEFAULT is reached.
- A reserverd PID is skipped.
Some topics on stackoverflow:
In the comments is a command to test the assigment of PIDs:
for i in {1..20}; do sh -c 'echo $$'; done
The process IDs missing in between are already dead and their PIDs will be reused by kernel in the later processes.
The dead processes won't be shown in the process table (except for zombies), hence ps -ef will not show them.
- 93,925
Normally PID is continuous but some process will be dead just by the time you run the command ps -ef.
Also some processes may be just a subprocess of another process which is not shown in the ps -ef command. To see some expanded result and you can check the continuous PID use the pstree
pstree -p
Sample output:
├─teamviewerd(3468)─┬─{teamviewerd}(3474)
│ ├─{teamviewerd}(3475)
│ ├─{teamviewerd}(3476)
│ ├─{teamviewerd}(3477)
│ ├─{teamviewerd}(3478)
while if you run ps -ef you just see the parent process.
$ ps -ef | grep teamviewerd
root 3468 1 0 Jul15 ? 00:07:38 /opt/teamviewer9/tv_bin/teamviewerd -f
- 87,123
They are continuous. The PID are assigned in sequential order until maximum limit is reached. After this limit it will start over again from zero.
So it is just that the missing PIDs in ps -ef are of dead processes. Note that ps -ef lists only running processes.
- 879