How Are Linux PIDs Generated?


Following the system's interpretation of each command, a separate process is established to carry out the command. This new process is assigned a distinct process identification number (PID). To keep track of each process's current state, the system uses the PID.

Note − Linux commands are case-sensitive.

Process Table

The process table in Linux is just a data structure in a computer's RAM like it is in almost every other operating system. It contains details about the processes that the OS is currently managing.

This data contains broad details about each procedure.

  • Process Id

  • Process Owner

  • Process Priority

  • Environment variables for each process

  • Parent Process

  • Elapsed Time

  • Pointers to the executable machine code of a process

Run the following command to display a whole screen (or full window of your terminal) with the current condition of your system and a list of the processes utilizing the majority of its CPU.

$ top

Output

top - 13:29:09 up 2 days,  7:13,  4 users,  load average: 0.07, 0.02, 0.00
Tasks: 148 total,   1 running, 147 sleeping,   0 stopped,   0  zomb
Cpu(s):  0.6%us, 0.5%sy,  0.0%ni, 97.3%id,  1.6%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   4051792k total,  4026104k used,  25688k   free,   359168k buffers
Swap:  4096492k    total, 24296k used, 4072196k  free,   2806484k cached
  PID     USER       PR   NI    VIRT    RES      SHR      S       %CPU    %MEM       TIME+     COMMAND
 7629    greys       20    0    749m  291m       28m      S        1       7.4     16:51.40    firefox
19935    greys       20    0    133m   14m       10m      S        0       0.4      2:38.52    smplayer
  331    root        20    0    4020    880      592      S        0       0.0     0:00.96       init
    2    root        15   -5      0      0        0       S        0       0.0      0:00.00     /bin/sh
    3    root        RT   -5      0      0        0       S        0       0.0      0:00.04     migration/0
 5494    root        15   -5      0      0        0       S        0       0.0      0:00.90    khelper/0
   13    root        RT   -5      0      0        0       S        0       0.0      0:00.00    kacpid
    6    root        RT   -5      0      0        0       S        0       0.0      0:00.06    kacpi_notify
  686    root        15   -5      0      0        0       S        0       0.0      0:01.32    ata/1
  368    root        RT   -5      0      0        0       S        0       0.0      0:00.00    ata/0
   21    root        15   -5      0      0        0       S        0       0.0      0:02.14    events/0
   10    root        15   -5      0      0        0       S        0       0.0      0:01.44    events/1

These are the elements which default top output consists.

PID Generation

Every process has a distinct identifier that serves as a representation of it, known as the process ID (pid). The idle process and its pid number are the names of the initial processes that the kernel launches. With the pid number 1, the init process is the first process to launch after booting.

A system's limit can be determined by consulting the /proc/sys/kernel/pid max file.

$ cat /proc/sys/kernel/pid_max 

Output

420314

Then, we determined the system's highest PID. The following step was to start four readlink processes, each of which checks the new PID that was given to it.

largest=0

for pid in /proc/[0-9]*; do
    pid="${pid##*/}" # Extract PID
    [ "$pid" -gt "$largest" ] && largest="$pid" # -gt means "greater than"
done

printf "Largest PID is %d
" "$largest" for _ in $(seq 4); do printf "New process PID %d
" "$(readlink /proc/self)" done

Output

Largest PID is 12648
New process PID 12650
New process PID 12651
New process PID 12652
New process PID 12653

Due to the seq command's inherent ability to initiate an additional process, we can see that there is a 1 PID delay between checking the highest PID and reporting the new PIDs.

Using ps command

To view the PIDs of running processes using the ps command, you can execute the following command in a terminal.

$ ps aux

Output

USER          PID       %CPU     %MEM        VSZ         RSS      TTY       STAT      START TIME COMMAND
webmast+       1        0.0       0.0        1136        0 ?      Ss        16:23      0:00 /sbin/docker-
webmast+       7       10.1       0.1        1012     233528 ?    Rl        16:23      24:52 GROUND
webmast+     15430      0.0       0.0       2888       908 ?      S         16:35      0:00 /bin/sh -c py
webmast+     15431      0.0       0.0       13788     20436 ?     Sl        16:35      0:02 python3 main.
webmast+     91894      0.0       0.0        1028       22836 ?   S         17:20      0:00 gnuplot
webmast+     91907      0.0       0.0        2888       948 ?     S         17:20      0:00 /bin/sh -c oc
webmast+     91908      0.0       0.0      4316732     45844 ?    Sl        17:20      0:01 /usr/bin/octa

This command displays a list of processes along with their corresponding PIDs, user information, CPU and memory usage, and other details.

Using pgrep command

When you run the pgrep command with a specific search pattern, it scans the running processes and matches them against the provided criteria. It then returns the PIDs of the processes that meet the search criteria.

Here is an example of using the pgrep command to find processes with a specific name −

$ pgrep <process_name>

Replace <process_name> with the name of the process you want to find. The command will return a list of matching process IDs (PIDs).

For instance, if you want to find the PIDs of processes named "python3", you would run −

Input

$ pgrep python3

Output

15431
173607
189388

The output would be a list of the PIDs of processes with the name "python3".

Conclusion

In this article, we learned about Linux process IDs, what is a process table, including how PIDs are generated. PIDs are essential for managing processes in Linux. The kernel generates a unique PID for each process using a counter and a base PID value. Understanding how PIDs are generated is important for system administrators and developers who need to manage and interact with processes in Linux.

Updated on: 01-Aug-2023

64 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements