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

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.

Show the nice value of your current shell
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.

Start a command with a nice value of ten
nice -n 10 sleep 60

The 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.

Change the nice value of a running process
renice -n 5 -p 4321

The top command shows the same values for every process. Its NI column is the nice value.

Show the nice values of busy processes once
top -b -n 1 | head -n 12

Where nice does not help

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.

  1. 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.

  2. Start the sleep command with a nice value of ten. It waits for sixty seconds.

    nice -n 10 sleep 60

    What you should see: The sleep command runs with a nice value of ten, so it gives its CPU time to other processes first.

  3. 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 4321

    What you should see: An ordinary user can only raise the value, which lowers the process's priority.

  4. Show the busiest processes and their nice values once, then stop.

    top -b -n 1 | head -n 12

    What 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.