Confused by 'ps' output

Thanks in advance to anyone that can offer me guidance on my questions… Which is about the priority (PR PRI) column in the ‘ps’ output

From what I’ve read… I’d expect to see a numerical value fall within the range of 1 - 139 for both processes and kernel threads

But I’m seeing negative numerical values on some kernel threads… ‘ps -el’ displays the priority (PR, PRI) for both the migration and watchdog kernel threads as -40… Yet top displays them as ‘rt’… Which I thought meant the numerical value of 99

Also the ‘ps -el’ output displays the value of priority for the irq/18-vmwgfx kernel thread with a value of 0… But top displays it as -51… I’m also see the card0-crtc0 kernel thread with a priority of 2 in the output of ‘ps -el’… But with a value of -51 in top

I’ve searched and haven’t seen anything about negative values for priority on processes or kernel threads… Could someone explain why I’m seeing negative values please? Also why am I seeing different values between the ps and top utilities?

Thanks!

For each PID (process ID), the Linux kernel sets a priority number and then stores such priority number at the 18th field of a stat file located at /proc/PID/

For instance, if the PID of a certain application is 15, then you are going to find a stat file located at /proc/15/ in your Linux filesystem. Hence, if you run this command on the shell:

cat /proc/15/stat

…then the cat program is going to print the content of /proc/15/stat (i.e. the content of the stat file located at /proc/15/) and you will therefore see something like this, on the shell:

15 (migration/0) S 2 0 0 0 -1 69238848 0 0 0 0 8 0 0 0 -100 0 1 0 45 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 0 0 0 17 0 99 1 0 0 0 0 0 0 0 0 0 0 0

The 18th field/column above is highlighted in bold: it’s the number -100. This number -100 is the priority set by the Linux kernel for the PID 15.

The priority shown by ps at the PRI column for a certain PID equals 60 + kernel priority, where kernel priority is that number located at the 18th field of the file /proc/PID/stat

Hence, if e.g. the Linux kernel set a kernel priority -100 for the PID 15, then the priority shown by ps at the PRI column is going to be 60 + -100, i.e. -40.


Two additional examples:

  • If cat /proc/24/stat gives you this:

    24 (kworker/1:0H-events_highpri) I 2 0 0 0 -1 69238880 0 0 0 0 0 0 0 0 0 -20 1 0 45 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 0 0 0 17 1 0 0 0 0 0 0 0 0 0 0 0 0 0

    …then ps is going to show you 60, at the PRI column of PID 24, because 60 + 0 equals 60.

  • If cat /proc/25/stat gives you this:

    25 (kdevtmpfs) S 2 0 0 0 -1 2130240 0 0 0 0 0 0 0 0 20 0 1 0 45 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 0 0 0 17 1 0 0 0 0 0 0 0 0 0 0 0 0 0

    …then ps is going to show you 80, at the PRI column of PID 25, because 60 + 20 equals 80.

top, on the other hand, shows you the kernel priority of the PID, i.e. it shows you the number placed by the Linux kernel at that 18th column of the /proc/PID/stat file.

I forgot to mention that my ps version uses 60 as increment. It seems that your ps version uses different increment numbers.

This is one of the reasons why developers don’t rely on the ps priority number to write programs. They usually read priority straight from /proc/PID/stat.

By the way, check if this rule applies to your case (which is another common way that ps and top behave in many Linux systems): the priority shown by top equals 20 + nice value, while the priority shown by ps equals 19 - nice value.

PS: the nice value is shown in a column titled NI.