Read

Module 6 · Processes and jobs

nice and renice: priorities

The nice value decides who gets the CPU when processes compete: start work with a lower priority using nice, change a running process with renice, and read the value back from ps, top and /proc.

What you will learn

  • Explain the nice scale from -20 to 19 and who may raise or lower it.
  • Start a command with a chosen niceness using `nice -n`.
  • Change a running process with `renice`, absolute or relative (`-n`), and verify the result.

Sharing one CPU

A CPU core runs one thing at a time; the kernel's scheduler creates the illusion of parallel work by handing out short slices of time. When several processes want the CPU, each one's nice value tilts the share. The scale goes from -20 (greediest, highest priority) to 19 (nicest, lowest priority), with 0 as the default. The name is literal: a process with a high nice value is being *nice* to the others. Note what nice does *not* do: on an idle machine a process at 19 still runs at full speed. Priority only matters when there is competition.

~% yes > /dev/null &
~% nice -n 19 yes > /dev/null &
~% ps -o pid,stat,args | grep '[y]es'
  924 R    yes
  926 RN   yes
~% top -b -n 1 | head -6
CPU:  93% usr   6% sys   0% nic   0% idle ...
  PID  PPID USER     STAT   VSZ %VSZ %CPU COMMAND
  924   923 root     R     1340   1%  80% yes
~% killall yes

Two copies of yes fight for the single core of the lab VM. The normal one takes about 80 % and the niced one is starved so badly it does not even reach the top of the list. ps marks it with N in STAT (low priority), the flag you met in lesson 52; a negative nice value shows as < instead.

nice: start with a priority

~% nice
0
~% nice -n 5 nice
5
~% nice sleep 600 &
~% nice -n 15 sleep 600 &
~% nice -n 25 nice
19
~% nice -n -5 sleep 600 &

Alone, nice prints the current niceness. In front of a command it adds to it: nice CMD adds 10, nice -n N CMD adds N, and the result is clamped to the -20…19 range. Children inherit the value, so nice -n 10 sh gives you a whole shell whose every command is niced. Only root may go down: a normal user can make their own processes nicer but never greedier, and cannot undo a raise. In the lab you are root, so nice -n -5 works; on a shared server it would fail with *Permission denied*.

renice: change a running process

~% sleep 900 &
[1] 1062
~% renice 15 1062
~% awk '{print $19}' /proc/1062/stat
15
~% renice -n 2 1062
~% awk '{print $19}' /proc/1062/stat
17
~% renice 5 -p 1062 1063

renice changes processes that are already running. In BusyBox, renice VALUE PID sets the nice value to exactly VALUE, while renice -n STEP PID adds STEP to the current one. -p (the default) says the IDs are PIDs; -g and -u apply the change to a whole process group or to every process of a user. Other implementations disagree about -n, so on any unfamiliar system check the result afterwards. BusyBox ps has no column for the number itself, only the N and < flags; the exact value is field 19 of /proc/PID/stat, which is where the awk above reads it (lesson 60 explores /proc).

Nice valueMeaning
-20Highest priority; only root can set it
0Default for everything you start
10What plain nice CMD gives
19Lowest priority: runs only when nothing else wants the CPU

Commands in this lesson

CommandWhat it does
nicePrint the current nice value.
nice CMDRun CMD with nice +10.
nice -n 15 CMDRun CMD with nice +15.
renice 19 PIDSet a running process's nice value to 19.
renice -n 3 PIDAdd 3 to its current nice value (BusyBox).
renice 10 -u USERRenice every process of a user.
awk '{print $19}' /proc/PID/statRead the exact nice value.

Quiz

  1. Which nice value gives a process the lowest priority?

    • -20
    • 0
    • 19
  2. What nice value does `nice sleep 600` run with, started from a shell at 0?

    • 1
    • 10
    • 19
  3. A normal (non-root) user runs `renice -5` on one of their own processes. What happens?

    • It works: it is their process
    • Permission denied: only root may lower the nice value
    • The value is silently set to 0
  4. In BusyBox, a process has nice 10. What is it after `renice -n 3 PID`?

    • 3
    • 13
    • 7
  5. On an otherwise idle machine, how fast does a process with nice 19 run?

    • At full speed: priority only matters under competition
    • At about 5 % of the CPU
    • It does not run until load drops to 0

Practice

  1. Start `sleep 1700` in the background with a nice value of 15.

  2. A `sleep 1701` is running with the default priority. Set its nice value to 19 without restarting it.

  3. A `sleep 1702` is running with nice 5. Make it 3 steps nicer, using renice's relative mode, so it ends up at 8.

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.