title: Linux thread priority value
body: <p>I am new to Linux. I am confused by the thread priority.</p>
  
  <p>What is the difference between top PR and ps PRI， why they are difference。</p>
  
  <pre><code>  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
      1 root      20   0  2804 1664 1220 S  0.0  0.3   0:01.30 init
      2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd
      3 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0
      4 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0
      5 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 watchdog/0
      6 root      20   0     0    0    0 S  0.0  0.0   0:00.02 events/0
  
  
  c@c-desktop:~$ ps -el
  F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
  4 S     0     1     0  0  80   0 -   701 poll_s ?        00:00:01 init
  1 S     0     2     0  0  80   0 -     0 kthrea ?        00:00:00 kthreadd
  1 S     0     3     2  0 -40   - -     0 migrat ?        00:00:00 migration/0
  1 S     0     4     2  0  80   0 -     0 ksofti ?        00:00:00 ksoftirqd/0
  5 S     0     5     2  0 -40   - -     0 watchd ?        00:00:00 watchdog/0
  1 S     0     6     2  0  80   0 -     0 worker ?        00:00:00 events/0
  </code></pre>
  
  <p>I found the answer in the souce file procps/output.c</p>
  
  <pre><code>In linux procps, the column labeled "PRI" in ps -l is -o opri
  
  // # ps -C19,_20 -o pri,opri,intpri,priority,ni,pcpu,pid,comm
  // PRI PRI PRI PRI  NI %CPU  PID COMMAND
  //   0  99  99  39  19 10.6 8686 19
  //  34  65  65   5 -20 94.7 8687 _20
  
  // "priority"         (was -20..20, now -100..39)
  static int pr_priority(char *restrict const outbuf, const proc_t *restrict const pp){    /* -20..20 */
      return snprintf(outbuf, COLWID, "%ld", pp-&gt;priority);
  }
  // "intpri" and "opri" (was 39..79, now  -40..99)
  static int pr_opri(char *restrict const outbuf, const proc_t *restrict const pp){        /* 39..79 */
      return snprintf(outbuf, COLWID, "%ld", 60 + pp-&gt;priority);
  }
  // "pri_foo"   --  match up w/ nice values of sleeping processes (-120..19)
  // "pri_bar"   --  makes RT pri show as negative       (-99..40)
  // "pri_baz"   --  the kernel's -&gt;prio value, as of Linux 2.6.8     (1..140)
  // "pri"               (was 20..60, now    0..139)
  // "pri_api"   --  match up w/ RT API    (-40..99)
  </code></pre>
  
  <p>PR in top equals to "priority" <br>
  PRI in ps equals to "opri"</p>