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.
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.
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.
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 -nsets a new value:renice -n 10for a process with nice 5 printsold 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 -nsets nice at start,renice -n … -p PIDfor a running process; only root can lower nice. - nice works between processes of one service or one session.