Priorities and the nice value
Video 073 of 140 in Virtualization Foundations, in the unit “Processes, threads and scheduling”. 3:45, with English and Hindi audio.
Not all work is equally urgent. A stock export can wait, but the screens people use cannot. Linux lets you tell the scheduler which work matters more, using a number called the nice value. This article explains the range of nice values, how a change shifts the share of CPU time, how to set and check a value, and where nice gives no help at all.
What you will learn
- A nice value runs from minus twenty to nineteen, and zero is the default.
- A lower nice value means more priority for CPU time.
- Use nice to start a command with a value, and renice to change a running process.
- Nice only matters when threads compete for CPU time. It does not speed up disk work.
- An ordinary user can only raise a nice value, which lowers the process's priority.
What a nice value is
Each process has a nice value. It ranges from minus twenty to nineteen, and zero is the default. A lower number means more priority. Minus twenty is the most urgent, and the scheduler favours it.
A higher number is more polite. A process with nineteen gives its CPU time to others, which is where the name nice comes from.
How a nice value changes the share of CPU
Suppose two threads both want a CPU. If their nice values are equal, they share the time about equally. Give one of them a lower nice value, and the scheduler gives it a larger share of the time.
The other thread is not stopped. It still runs, but it gets less time while the first one is busy. So a higher nice value slows a process down, without shutting it out.
Note: Nice only matters when threads compete for CPU time. A thread that is waiting for disk gains nothing from it.
Set and check a nice value from the shell
The ps command with the ni keyword shows the nice value of a process. Your own shell usually shows zero.
ps -o pid,ni,comm -p $The nice command starts a program with a chosen value. Here the sleep program starts with a nice value of ten, and it waits sixty seconds.
nice -n 10 sleep 60The renice command changes a process that is already running. You give it the process ID, which is the number Linux uses to name that process.
renice -n 5 -p 4321The top command shows the same values for every process. Its NI column is the nice value.
top -b -n 1 | head -n 12Where nice does not help
- Waiting on disk: nice changes CPU share only. It does not speed up storage.
- Full CPUs: nice only decides who waits. It adds no CPU time.
- How you notice it: screens stay slow while a long unattended batch job takes most of the CPU time.
Note: Nice helps only when CPU time is the thing that is short. If the slow part is storage or memory, look there first.
Check and change a nice value
You need a Linux terminal. You will read the nice value of your shell, start a command with a new value, change a running process, and read the values in top.
Show the nice value of your current shell. Your shell usually shows zero.
ps -o pid,ni,comm -p $What you should see: The NI column is the nice value. Your shell usually shows zero.
Start the sleep command with a nice value of ten. It waits for sixty seconds.
nice -n 10 sleep 60What you should see: The sleep command runs with a nice value of ten, so it gives its CPU time to other processes first.
Change a running process to a nice value of five. Replace 4321 with the process ID of one of your own processes.
renice -n 5 -p 4321What you should see: An ordinary user can only raise the value, which lowers the process's priority.
Show the busiest processes and their nice values once, then stop.
top -b -n 1 | head -n 12What you should see: The NI column in top shows the nice value of each process.
Terms
- Nice value
- A number from minus twenty to nineteen that tells the scheduler how much priority a process gets for CPU time. Zero is the default.
- Process ID (PID)
- The number Linux gives each process, which commands such as renice use to find that process.
Check yourself
What is the default nice value of a process?
Zero. A lower value gives more priority, and a higher value gives CPU time away to other processes.
Which way can an ordinary user change a nice value?
An ordinary user can only raise it, which lowers the process's priority.
Does a nice value speed up a thread that is waiting on disk?
No. Nice changes only the share of CPU time, and it does not speed up storage.
Previous: The run queue and the load average. Next: Counting context switches with vmstat and pidstat.