Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How Are Linux PIDs Generated?
When a Linux command is executed, the system creates a separate process to carry out that command. Each process is assigned a unique Process ID (PID), which the operating system uses to track and manage the process throughout its lifecycle.
Note Linux commands are case-sensitive.
Process Table
The process table is a data structure stored in the system's RAM that contains information about all processes currently managed by the operating system. Each entry in the process table stores comprehensive details about a specific process.
Key information stored in the process table includes
Process ID (PID) Unique identifier for the process
Process Owner User who owns the process
Process Priority Scheduling priority level
Environment Variables Process-specific environment settings
Parent Process ID (PPID) ID of the parent process
CPU Time Total CPU time consumed
Memory Pointers References to executable machine code
You can view currently running processes and their resource usage using the top command
$ 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 zombie
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.0 7.4 16:51.40 firefox
19935 greys 20 0 133m 14m 10m S 0.0 0.4 2:38.52 smplayer
331 root 20 0 4020 880 592 S 0.0 0.0 0:00.96 init
2 root 15 -5 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root RT -5 0 0 0 S 0.0 0.0 0:00.04 migration/0
How PIDs Are Generated
Linux uses a simple sequential counter mechanism to generate PIDs. When the system boots, the init process is assigned PID 1, making it the first user-space process. Subsequent processes receive incrementally higher PID values.
PID Maximum Limit
The system defines a maximum PID value to prevent overflow. You can check this limit by examining the /proc/sys/kernel/pid_max file
$ cat /proc/sys/kernel/pid_max
Output
4194304
PID Generation Example
Here's a demonstration showing how new processes receive sequential PIDs
largest=0
for pid in /proc/[0-9]*; do
pid="${pid##*/}" # Extract PID from path
[ "$pid" -gt "$largest" ] && largest="$pid"
done
printf "Largest PID is %d<br>" "$largest"
for _ in $(seq 4); do
printf "New process PID %d<br>" "$(exec sh -c 'echo $$')"
done
Output
Largest PID is 12648 New process PID 12650 New process PID 12651 New process PID 12652 New process PID 12653
Viewing Process PIDs
Using ps Command
The ps command displays detailed information about running processes, including their PIDs
$ ps aux
Output
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 1136 4 ? Ss 16:23 0:00 /sbin/init root 7 0.1 0.1 2012 936 ? Rl 16:23 0:52 /usr/sbin/cron webuser 15430 0.0 0.0 2888 908 ? S 16:35 0:00 /bin/sh -c python3 webuser 15431 0.0 0.2 13788 4036 ? Sl 16:35 0:02 python3 main.py webuser 91894 0.0 0.0 1028 836 ? S 17:20 0:00 gnuplot
Using pgrep Command
The pgrep command searches for processes by name and returns their PIDs
$ pgrep python3
Output
15431 173607 189388
You can also use pattern matching with pgrep
$ pgrep -f "python.*main" # Find processes with "python" and "main" in command line
Key Points
Sequential Assignment PIDs are assigned sequentially starting from 1
PID 1 Special Role Always assigned to the init process
Wraparound Behavior When max PID is reached, counter wraps around to find available PIDs
Uniqueness Guarantee No two active processes share the same PID
Reusability PIDs from terminated processes can be reused
Conclusion
Linux PIDs are generated using a simple sequential counter mechanism that ensures each process receives a unique identifier. The system maintains a maximum PID limit and wraps around when necessary, always guaranteeing process uniqueness. Understanding PID generation is fundamental for system administration and process management in Linux environments.
