6.5 Process Priorities: nice & renice (103.6)
Key Takeaways
- The Linux kernel scheduling priority ranges across static niceness values from `-20` (highest priority / least nice) to `19` (lowest priority / most nice), with default niceness for new processes set to `0`.
- Under the Completely Fair Scheduler (CFS), the dynamic kernel priority (PR) corresponds directly to niceness: $\text{PR} = 20 + \text{NI}$.
- Regular unprivileged users can only *increase* niceness (range 0 to 19, lowering their own scheduling priority); only the superuser (`root`) can assign negative niceness values (-20 to -1).
- The `nice` command launches a NEW process with an adjusted priority offset, defaulting to +10 if invoked without arguments.
- The `renice` command alters the ABSOLUTE niceness of ALREADY RUNNING processes by PID (`-p`), User (`-u`), or Process Group (`-g`).
6.5 Process Priorities: nice & renice
Quick Summary: In a multi-tasking operating system, the Linux Completely Fair Scheduler (CFS) dynamically allocates CPU execution time among competing processes. Scheduling decisions are guided by niceness (
NI), an integer value spanning from-20(highest priority / least nice to others) to19(lowest priority / most nice to others). Administrators launch new programs with custom priority offsets usingniceand alter the absolute priority of active processes usingrenice. Unprivileged users may only decrease their own priority, whilerootretains unrestricted priority control.
1. Linux CPU Scheduling, Dynamic Priority & Niceness
The standard Linux scheduler is the Completely Fair Scheduler (CFS). Rather than using fixed time slices, CFS models an "ideal multi-tasking CPU" on hardware by tracking the virtual runtime (vruntime) of each task. Tasks with smaller virtual runtimes are scheduled next on available CPU cores.
Dynamic Priority vs. Static Niceness
When viewing process tables in top or ps, two related priority columns appear:
NI(Niceness): The user-configurable static priority modifier spanning from-20to19(default:0).PR/PRI(Kernel Priority): The internal dynamic priority value used by the kernel scheduler.
For standard user-space processes scheduled under SCHED_OTHER / SCHED_NORMAL, the kernel computes dynamic priority using the direct mapping formula:
Niceness-to-Kernel Priority Mapping Spectrum:
Niceness (NI): -20 ─── -15 ─── -10 ─── -5 ──── 0 ──── +5 ─── +10 ─── +15 ─── +19
│ │ │
▼ ▼ ▼
Priority (PR): 0 ───── 5 ──── 10 ──── 15 ──── 20 ──── 25 ──── 30 ──── 35 ──── 39
[Highest CPU Time] [Default] [Lowest CPU Time]
[Root Privilege Only] [Available to All Users]
The Niceness Spectrum & Privilege Rules
Niceness Value (NI) | Dynamic Priority (PR) | CPU Scheduling Share | Who Can Set This Value? |
|---|---|---|---|
-20 to -1 | 0 to 19 | High Priority: Receives significantly larger CPU time slices; preempts standard processes. | Superuser (root) ONLY |
0 | 20 | Standard Priority: Default value assigned to all new processes launched by users and system services. | All Users |
1 to 19 | 21 to 39 | Low Priority ("Nice"): Yields CPU time to standard processes; ideal for background batch jobs, backups, and video rendering. | All Users (can only increase) |
⚠️ LPIC-1 Trap — Niceness Logic is Inverted:
- A LOWER numerical value means HIGHER CPU priority (a value of
-20is aggressive, not nice to other processes).- A HIGHER numerical value means LOWER CPU priority (a value of
19is polite and nice to other processes).- Unprivileged users can only make their processes more nice (increase NI from 0 up to 19). They cannot lower niceness (even back to 0 once raised!).
2. Launching New Processes with nice
The nice command launches a new process with a specified niceness offset. It cannot modify an already running process.
Command Syntax
nice [OPTION] [COMMAND [ARG]...]
Specifying Niceness Values
Modern Linux systems support the clean -n (or --adjustment) flag:
# 1. Launch a CPU-intensive backup with lower priority (NI = 10):
$ nice -n 10 tar -czf /backup/archive.tar.gz /data
# 2. Launch a high-priority database process with negative niceness (Root only):
$ sudo nice -n -10 mysqld_safe &
# 3. Default behavior when no flag is provided (Increments niceness by 10):
$ nice tar -czf backup.tar.gz /data
# Runs with niceness NI = 10
Legacy Syntax Traps on the LPIC-1 Exam
On the LPIC-1 exam, you will encounter both modern -n syntax and legacy dash syntax:
nice -10 command→ Runs command with a positive niceness of10(low priority).nice --10 command→ The first dash is the flag specifier; the second dash is the negative sign! This runs the command with a negative niceness of-10(high priority, root only).
# Legacy syntax comparison:
$ nice -15 gzip bigfile.iso # Nice = +15 (Low priority)
$ sudo nice --15 gzip bigfile.iso # Nice = -15 (High priority)
3. Modifying Running Processes with renice
The renice command alters the scheduling priority of one or more already active processes.
Key Architectural Difference Between nice and renice
Exam Rule — Relative vs. Absolute Values:
niceapplies a relative adjustment / offset when launching a new program.renicesets the ABSOLUTE target niceness value on running processes!
renice Command Syntax & Target Flags
renice [-n] priority [[-p] pids] [[-g] pgrps] [[-u] users]
| Flag | Target Scope | Real-World Example |
|---|---|---|
-p <PID> | Target by Process ID (default if no flag specified) | renice -n 5 -p 1042 1043 |
-u <user> | Target all processes owned by Username or UID | sudo renice -n 10 -u developer |
-g <PGRP> | Target all processes in a Process Group ID | renice -n 8 -g 2048 |
Practical renice Demonstrations
# Lower the priority of running PID 4812 to an absolute niceness of 15:
$ renice -n 15 -p 4812
4812 (process ID) old priority 0, new priority 15
# Alternative shorthand syntax without -n or -p:
$ renice 15 4812
# Elevated priority (Root only): Set niceness of PID 1024 to -5:
$ sudo renice -n -5 -p 1024
1024 (process ID) old priority 0, new priority -5
# Lower the priority of all processes belonging to user 'john':
$ sudo renice 10 -u john
Privilege Error Demonstration
If an unprivileged user attempts to assign a negative niceness or lower an existing niceness value, the kernel denies the syscall:
$ renice -n -5 -p 4812
renice: failed to set priority for 4812 (process ID): Permission denied
💡 LPIC-1 Exam Fill-in-the-Blank Alert: What command is used to change the priority of an already running Linux process? Answer:
renice
A system administrator needs to start a resource-intensive data compression job with the highest possible CPU scheduling priority. Which command accomplishes this?
What is the resulting niceness value of a process executed with the command nice ./batch_job.sh if the parent shell has a default niceness of 0?
An administrator wishes to lower the scheduling priority of all active processes currently executing under the username developer to a nice value of 12. Which command executes this modification?