Linux Labs

Inside the system: resources, kernel, processes / Stage 1 of 12

Load average and CPU load

Not running Starting usually ~20 s
Leave the lesson

Step 5 of 7 · theory · 4 min

The nice priority

When the CPU is busy, the kernel shares it among the processes in R. You can tell each process whether to give way to others.

A process's nice value runs from −20 to 19, 0 by default. The larger it is, the smaller the share of the CPU the kernel scheduler gives the process when several processes compete for the CPU. If the CPU is free, a process with nice 19 runs at full speed. top shows nice in the NI column, and the time of such processes goes into the ni field of the %Cpu(s) line.

bash
nice -n 19 tar -czf /var/backups/www.tar /var/www  # nice 19 at start
sudo renice -n 19 -p 1234       # nice of a running process
ps -o pid,ni,comm -p 1234       # check

The owner can raise nice for their own process, that is, give way. Lowering it — taking CPU from others — is for root only: as student, renice -n 0 for a process with nice 5 answers Permission denied.

Note

nice compares processes of one service or one terminal session. Different systemd services share the CPU equally whatever their nice: each runs in its own cgroup.

Check yourself 1 question

Your process runs with nice 10. What will renice -n 5 -p <PID>, typed as student without sudo, print?

  • The owner can only raise nice — give way on the CPU. 5 is below 10: the process would take CPU from others, and that is root's right.

  • renice -n sets a new value: renice -n 10 for a process with nice 5 prints old priority 5, new priority 10.

Lowering nice means taking CPU from other processes, which only root may do. Raising nice for your own process works without sudo.

Key points

  • nice runs from −20 to 19: the larger it is, the less CPU a process gets when processes compete for the CPU.
  • nice -n sets nice at start, renice -n … -p PID for a running process; only root can lower nice.
  • nice works between processes of one service or one session.
Ubuntu 24.04
Loading…