3

For example, let's examine the value of PRI of firefox with ps, and then see what is the value stored in procfs.

$ ps -o pid,comm,pri,ni 7000
  PID COMMAND         PRI  NI
 7000 firefox          19   0

$ cat /proc/7000/stat
7000 (firefox) S 1 6447 6447 0 -1 4194304 3162595 624998 158 10 30467 6903 3360 488 20 0 63 0 464836 9472659456 123045 18446744073709551615 94866409246720 94866409429052 140727418541056 0 0 0 0 4096 33572095 0 0 0 17 2 0 0 342 0 0 94866411526576 94866411528296 94866422095872 140727418542495 140727418542520 140727418542520 140727418544095 0

According to man proc, we will find the value of PRI in the 18th value (counting from 1), so in this case PRI = 20

I want to know why there is such difference between the output of the ps command and the value stored in the /proc stat file?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250

1 Answers1

5

Uh, apparently the pri field is exactly 39 minus the value that is visible in /proc/$pid/stat (so 39 - 20 = 19). It's also commented as 'not legal as UNIX "PRI"', since

Unix98 only specifies that a high "PRI" is low priority.

And that doesn't apply there.

But! There are no less than six other output formats for the priority, all of which have the raw value either negated or not, plus some constant. Take a pick. Here's three cats with different nice values:

$ ps -o pid,rtprio,pri,opri,priority,pri_foo,pri_bar,pri_baz,pri_api,ni,args -Ccat
  PID RTPRIO PRI PRI PRI FOO BAR BAZ API  NI COMMAND
18418      -   0  99  39  19  40 139 -40  19 cat /dev/zero
18419      -  19  80  20   0  21 120 -21   0 cat /dev/zero
18420      -  39  60   0 -20   1 100  -1 -20 cat /dev/zero

The comments in the code say that

Sun and SCO add the -c behavior. Sun defines "pri" and "opri".

So there's probably some historical reason to fix the output range to match. ps -c uses the pri valu here. priority is the raw value as the kernel presents it.

The relevant source code file is ps/output.c:
https://gitlab.com/procps-ng/procps/blob/master/ps/output.c#L585

Also: https://superuser.com/questions/286752/unix-ps-l-priority/286761
and https://stackoverflow.com/questions/18829350/linux-thread-priority-value

ilkkachu
  • 133,243
  • 15
  • 236
  • 397
  • Thank you for your answer, however I still didn't get some details here. Would you mind telling me why there are so much different priority values? (pri,opri,priority,pri_foo,pri_bar,pri_baz,pri_api) what is the use of all of this? Do we actually need all those different values? @ilkkachu – Amine Marzouki Dec 14 '17 at 17:55
  • 1
    Horrifying. Excellent. – ACK_stoverflow May 05 '20 at 23:14
  • 1
    @ACK_stoverflow, I just re-read this now, almost two and a half years after it was written. I had to stop and stare for a while, and try to convince myself I hadn't seen this in a horrifying dream. Thanks for pinging with the comment... – ilkkachu May 05 '20 at 23:32