foreword
Hehe, the process number is something we often use, but we have never delved into it
Let's take a look at how the specific process number is generated.
Linux creates a new process in the form of fork + exec
The child process is copied from the parent process
How is the pid generated?
The call stack where the pid is allocated is as follows. After fork, the pid will be applied for the new process. The details are as follows
Allocation is based on pid_namespace
Among them, the size of the last allocated pid is recorded, and the pid is the last pid +1
Then try to update the pid in the pid_bitmap of pid_ns to use, if the update is successful, use the current pid
If the update is unsuccessful, get the next available index in the pid_bitmap of pid_ns, and try again
If the current pid_bitmap does not find an available pid until the end, try to find the next pid_bitmap [if the current pid_bitmap is the last one, then the next pid_bitmap is the first pid_bitmap]
Regarding the multiple pid_bitmaps here, by default only one pid_bitmap will be used
BITS_PER_PAGE is 4k * 8 = 32768, pid_max defaults to 32768, so the pid_bitmap here will use at most one element
The memory of pid_bitmap here is maintained by using a physical page
According to most normal circumstances, a process number is the maximum process number + 1
If assigned to pid_max [the default is 32768], try from the first pid of the first pid_bitmap
To test the relevant rules of pid, write a script that has been "ps -l" + "grep ps"
The script is roughly as follows
ps -l | grep ps
ps -l | grep ps
ps -l | grep ps
ps -l | grep ps
ps -l | grep ps
ps -l | grep ps
ps -l | grep ps
ps -l | grep ps
ps -l | grep ps
ps -l | grep ps
ps -l | grep ps
The test output is roughly as follows
...
R 0 14978 287 4680 844 0:0 06:41 00:00:00 {exe} ps -l
R 0 14979 287 4676 700 0:0 06:41 00:00:00 {exe} grep ps
S 0 287 275 4676 1468 0:0 03:33 00:01:47 {exe} ash ./psCmd.sh
R 0 14980 287 4680 840 0:0 06:41 00:00:00 {exe} ps -l
R 0 14981 287 4676 840 0:0 06:41 00:00:00 {exe} grep ps
S 0 287 275 4676 1468 0:0 03:33 00:01:47 {exe} ash ./psCmd.sh
R 0 14982 287 4680 712 0:0 06:41 00:00:00 {exe} ps -l
R 0 14983 287 4676 784 0:0 06:41 00:00:00 {exe} grep ps
S 0 287 275 4676 1468 0:0 03:33 00:01:47 {exe} ash ./psCmd.sh
R 0 14984 287 4680 792 0:0 06:41 00:00:00 {exe} ps -l
R 0 14985 287 4676 732 0:0 06:41 00:00:00 {exe} grep ps
S 0 287 275 4676 1468 0:0 03:33 00:01:47 {exe} ash ./psCmd.sh
R 0 14986 287 4680 680 0:0 06:41 00:00:00 {exe} ps -l
R 0 14987 287 4676 740 0:0 06:41 00:00:00 {exe} grep ps
S 0 287 275 4676 1468 0:0 03:33 00:01:47 {exe} ash ./psCmd.sh
R 0 14988 287 4680 784 0:0 06:41 00:00:00 {exe} ps -l
R 0 14989 287 4676 792 0:0 06:41 00:00:00 {exe} grep ps
S 0 287 275 4676 1468 0:0 03:33 00:01:47 {exe} ash ./psCmd.sh
R 0 14990 287 4680 696 0:0 06:41 00:00:00 {exe} ps -l
R 0 14991 287 4676 792 0:0 06:41 00:00:00 {exe} grep ps
S 0 287 275 4676 1468 0:0 03:33 00:01:47 {exe} ash ./psCmd.sh
R 0 14992 287 4680 692 0:0 06:41 00:00:00 {exe} ps -l
R 0 14993 287 4676 836 0:0 06:41 00:00:00 {exe} grep ps
S 0 287 275 4676 1468 0:0 03:33 00:01:47 {exe} ash ./psCmd.sh
R 0 14994 287 4680 696 0:0 06:41 00:00:00 {exe} ps -l
R 0 14995 287 4676 712 0:0 06:41 00:00:00 {exe} grep ps
S 0 287 275 4676 1468 0:0 03:33 00:01:47 {exe} ash ./psCmd.sh
R 0 14996 287 4680 716 0:0 06:41 00:00:00 {exe} ps -l
R 0 14997 287 4676 692 0:0 06:41 00:00:00 {exe} grep ps
S 0 287 275 4676 1468 0:0 03:33 00:01:47 {exe} ash ./psCmd.sh
R 0 14998 287 4680 676 0:0 06:41 00:00:00 {exe} ps -l
R 0 14999 287 4676 712 0:0 06:41 00:00:00 {exe} grep ps
S 0 287 275 4676 1468 0:0 03:33 00:01:47 {exe} ash ./psCmd.sh
R 0 15000 287 4680 688 0:0 06:41 00:00:00 {exe} ps -l
R 0 15001 287 4676 740 0:0 06:41 00:00:00 {exe} grep ps
over