Chapter 4.4 - Priorities and Nice Values
Learning Objectives
By the end of this chapter, you will be able to:
- Understand process priorities
- Use nice values
- Change priorities with nice and renice
- Understand the Linux scheduler
What is Priority?
A process's priority determines its access to the CPU. The higher the priority, the more CPU time the process gets.
Nice Values
Scale
Nice values: -20 to +19
- -20: Highest priority (root only)
- 0: Default priority
- +19: Lowest priority
Rule:
- Lower = more priority
- Higher = less priority
Viewing Priority
# With ps
ps -o pid,ni,comm
ps aux | head
# Column NI = Nice value
# Column PRI = Actual priority (calculated)
# With top
top
# Column NI shows nice value
Changing Priority
nice - At Startup
# Start with low priority
nice -n 10 command
nice -n +10 command # Same thing
# Start with high priority (root)
sudo nice -n -10 command
# Verify
ps -o pid,ni,comm | grep command
renice - Existing Process
# Change process priority
renice 10 PID
# By user
sudo renice 10 -u user1
# By group
sudo renice 10 -g developers
# By process and children
sudo renice 10 -p PID
Examples:
# CPU-intensive process → low priority
renice 15 $(pidof backup_script)
# Important process → high priority (root)
sudo renice -10 $(pidof critical_app)
Linux Scheduler (CFS)
Completely Fair Scheduler
The Linux scheduler uses nice values to balance CPU time:
Operation:
- Lower nice = more CPU time
- Fairness among processes of the same priority
- Considers time already used
Real-Time Processes
chrt - Real-Time
For processes requiring time guarantees:
# View policies
chrt -m
# Set FIFO policy
sudo chrt -f 50 command
# -f: FIFO
# 50: Priority (1-99)
# Set Round-Robin policy
sudo chrt -r 50 command
# Change existing process
sudo chrt -f -p 50 PID
Policies:
- SCHED_FIFO: First In First Out
- SCHED_RR: Round Robin
- SCHED_OTHER: Normal (CFS)
Warning: Real-time processes can monopolize the CPU!
Practical Examples
Scenario 1: Nightly Backup
# Run backup with very low priority
nice -n 19 backup_script.sh &
# Will not disturb other processes
Scenario 2: Critical Process
# Increase priority of an important process
sudo renice -10 $(pidof database_server)
# Will get more CPU time
Scenario 3: Balancing
# Multiple processes, balance them
renice 5 $(pidof app1)
renice 5 $(pidof app2)
renice 5 $(pidof app3)
# All with the same priority
Monitoring Priorities
top/htop
# View nice values
top
# Column NI
# Sort by priority
# In top: press 'r' then enter PID
ps with Priority
# View nice and priority
ps -eo pid,ni,pri,comm
# Sort by priority
ps -eo pid,ni,pri,comm --sort=-pri
Summary
In this chapter, you learned:
Nice values: -20 (high) to +19 (low), 0 by default
Change: nice (startup), renice (existing)
Scheduler: CFS balances according to nice values
Real-time: chrt for real-time guarantees
Monitoring: ps, top to view priorities
Practice: Adjust according to process importance
Next Steps
Chapter 4.5: Processes and Resources
Chapter 4.6: Jobs and Background Tasks
Chapter created on: December 2024