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. The nice value ranges from -20 to 19, and maps to priorities 100 to 139. As with the priority value, the lower the nice 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
 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
 6153 cat              29 -10

The PRI from ps -o pri is 39 - priority, where priority is the 18th field from /proc/PID/stat.

If you want the unmangled field from proc/PID/stat, you can get it with ps -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 and ps -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 in fs/proc/array.c to task_struct->prio - 100 (via task_prio(); MAX_RT_PRIO is defined as 100).

  • Thank you! Its quite interesting that there are so many different manglings of the priority value. This still doesn't answer why the manual wrongly states the meaning of PRI, though... it should read "lower number means lower priority", given that it's 39 - priority. – Adrià Rico Blanes Nov 18 '18 at 19:42
  • I think that you should submit a bug report -- maybe to the effect of changing the standard format specifier for PRI from pri to priority, since that's the one that gets the value exposed by the kernel to userland. – mosvy Nov 19 '18 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.