Ubuntu – Niceness and Priority of Processes

niceprocessprocess-priority

According to the some resources I have read, the process with low nice value has more priority. Because it has more priority, CPU allocates more time for that process.

However, it sounds a little bit weird because priority value of a process is calculated by summing nice value with 20.

PR = NI + 20. This can be seen easily with the command top

According to this formula, the processes with higher nice values have higher priorities, but some resources says that the process with low nice value has more priority.

Which one is true ?

Best Answer

According to this formula, the processes with higher nice values have higher priorities, but some resources says that the process with low nice value has more priority.

Close, but no cookie. That the numeric value of priority is high doesn't mean the priority is high. top reads the priority from /proc/<pid>/stat. See man 5 proc for the explanation of that file:

(18) priority  %ld
    (Explanation  for Linux 2.6) For processes running a real-time scheduling
    policy (policy below; see sched_setscheduler(2)),  this  is  the  negated
    scheduling  priority,  minus  one;  that  is, a number in the range -2 to
    -100, corresponding to real-time  priorities  1  to  99.   For  processes
    running  under  a  non-real-time  scheduling policy, this is the raw nice
    value (setpriority(2)) as represented in the kernel.  The  kernel  stores
    nice  values  as numbers in the range 0 (high) to 39 (low), corresponding
    to the user-visible nice range of -20 to 19.

    Before Linux 2.6,  this  was  a  scaled  value  based  on  the  scheduler
    weighting given to this process.

So: PR goes from 0 (high) to 39 (low).