Chapter 2.5 - Process Management
Learning Objectives
At the end of this chapter, you will be able to:
- Understand how Linux manages processes
- Explain process states
- Understand scheduling
- Identify priorities and nice values
What is a Process?
A process is an instance of a running program. Each process has:
- A unique PID (Process ID)
- A PPID (Parent Process ID)
- Its own memory space
- Open files
- System resources
Process States
A process can be in different states:
States:
- Running (R): Executing or ready
- Sleeping (S): Waiting for an event
- Stopped (T): Suspended (Ctrl+Z)
- Zombie (Z): Finished but not cleaned up
- Uninterruptible (D): Waiting for disk I/O
Process Scheduling
The Linux scheduler (CFS - Completely Fair Scheduler) decides which process to run:
Scheduling Criteria:
- Priority (nice value)
- CPU time used
- Waiting time
- CPU affinity
Priorities and Nice Values
A process's priority determines its access to the CPU:
Nice values:
- -20 to +19 (lower = more priority)
- 0: Default priority
- -20: High priority (root only)
- +19: Very low priority
# View priority
ps -o pid,ni,comm
# Change priority at start
nice -n 10 command
# Change priority of an existing process
renice 10 PID
# Real-time process (root)
chrt -f 50 command
Process Hierarchy
Processes form a hierarchy (tree):
Characteristics:
- Parent process can create children (fork)
- If parent dies, children become orphans (adopted by init)
- Zombie process if parent does not retrieve status
Management Commands
# View processes
ps aux
top
htop
# Kill a process
kill PID
kill -9 PID # Force
killall process_name
# Suspend/Resume
kill -STOP PID
kill -CONT PID
# View process tree
pstree
ps auxf
Inter-Process Communication
Processes can communicate via:
- Signals: kill, SIGTERM, SIGKILL
- Pipes:
|(command line) - Sockets: Network communication
- Shared files: Shared memory
- Message queues: IPC System V
Summary
In this chapter, you learned:
Process: Instance of a running program
States: Running, Sleeping, Stopped, Zombie
Scheduler: CFS schedules process execution
Priorities: Nice values from -20 to +19
Hierarchy: Tree with init as root
Management: ps, top, kill, nice, renice
Next Steps
Chapter 2.6: Init and systemd Processes
Chapter 2.7: Services and Daemons
Chapter created on: December 2024