How is process priority calculated?
In ps
’s output, pri_baz
is calculated as pp->priority + 100
, and pp->priority
is the prio
value from the kernel. This is described as
Priority of a process goes from 0..
MAX_PRIO
-1, valid RT priority is 0..MAX_RT_PRIO
-1, andSCHED_NORMAL
/SCHED_BATCH
tasks are in the rangeMAX_RT_PRIO
..MAX_PRIO
-1. Priority values are inverted: lowerp->prio
value means higher priority.The
MAX_USER_RT_PRIO
value allows the actual maximum RT priority to be separate from the value exported to user-space. This allows kernel threads to set their priority to a value higher than any user task. Note:MAX_RT_PRIO
must not be smaller thanMAX_USER_RT_PRIO
.
So the range in the kernel does cover 140 values, from 0 to MAX_PRIO
–1 (139).
However, the minimum FIFO and RT priority is 1, and this explains the missing value: the input values (at least, that can be set from userspace, using sched_setscheduler
) go from 1 to 99, and the kernel converts those to prio
values using the formula MAX_RT_PRIO
– 1 – priority, giving values from 0 to 98.