I've been trying to understand how scheduling priorities work in linux.
Here's what I've got:
- The kernel uses a priority value for each process which ranges from 0 to 139. The lower the number, the more priority the process has.
- Priority values from 0 to 99 are reserved, all user space processes have a priority from 100 to 139.
- Linux provides the
nice
interface which is the priority exposed to and modifiable by the user. Thenice
value ranges from -20 to 19, and maps to priorities 100 to 139. As with the priority value, the lower thenice
value, the more priority the process has.
My question now is: What does the PRI column of ps
indicate?
The man page of ps
says:
pri PRI priority of the process. Higher number means lower priority.
But the value ranges from 0 to 39, and I empirically determined it is equal to 19 - nice
.
It is impossible, given the relationship, that both nice
and the priority shown in the PRI
column satisfy "higher number means lower priority".
What am I missing?
Example showing this behaviour:
root@kali:~# ps -ao pid,comm,pri,nice
PID COMMAND PRI NI
6153 cat 19 0
(···)
root@kali:~# renice -n -10 -p 6153
6153 (process ID) old priority 0, new priority -10
root@kali:~# ps -ao pid,comm,pri,nice
PID COMMAND PRI NI
6153 cat 29 -10
(···)
Best Answer
The
PRI
fromps -o pri
is39 - priority
, wherepriority
is the 18th field from/proc/PID/stat
.If you want the unmangled field from
proc/PID/stat
, you can get it withps -o priority
.If you want the real priority, you can obtain it with
ps -o pri_baz
.Other interesting manglings of that value could be obtained with
ps -o pri_foo
,ps -o pri_bar
andps -o opri
.I kid you not. You can look at the source here.
Note about
/proc/PID/stat
:The priority field (18th) in
/proc/PID/stat
is set by the kernel infs/proc/array.c
totask_struct->prio - 100
(viatask_prio()
;MAX_RT_PRIO
is defined as100
).