15

top, by default, lists both columns. I am curious as to what is the difference. I checked out the man pages and cannot figure it out:

Priority:

   h: PR  --  Priority
      The priority of the task.

Nice value:

   i: NI  --  Nice value
      The nice value of the task.  A negative nice value means higher  priority,
      whereas  a  positive  nice value means lower priority.  Zero in this field
      simply means priority will not be adjusted in determining  a  task’s  dis-
      patchability.

I understand that Nice value is related to the Kernel's CPU scheduler queue; then what does Priority indicate? Something regarding I/O perhaps?

3 Answers 3

10

The nice value is a "global" mechanism, whereas priority is relevant for the task switcher right now.

2
  • What do you mean by task switcher? Oct 27, 2010 at 0:21
  • 1
    The task switcher (properly called the "scheduler") is a little bit of code within the kernel that decides what task will run next. Oct 27, 2010 at 4:12
37

The difference is that PR is a real priority of a process at the moment inside of the kernel and NI is just a hint for the kernel what the priority the process should have.

In most cases PR value can be computed by the following formula: PR = 20 + NI. Thus the process with niceness 3 has the priority 23 (20 + 3) and the process with niceness -7 has the priority 13 (20 - 7). You can check the first by running command nice -n 3 top. It will show that top process has NI 3 and PR 23. But for running nice -n -7 top in most Linux systems you need to have root privileges because actually the lower PR value is the higher actual priority is. Thus the process with PR 13 has higher priority than processes with standard priority PR 20. That's why you need to be root. But minimum niceness value allowed for non-root process can be configured in /etc/security/limits.conf.

Theoretically the kernel can change PR value (but not NI) by itself. For example it may reduce the priority of a process if it consumes too much CPU, or it may increase the priority of a process if that process had no chance to run for a long time because of other higher priority processes. In these cases the PR value will be changed by kernel and NI will remain the same, thus the formula "PR = 20 + NI" will not be correct. So the NI value can be interpreted as hint for the kernel what the priority the process should have, but the kernel can choose real priority (PR value) on its own depending on the situation. But usually the formula "PR = 20 + NI" is correct.

Exact rules how the kernel changes priority aren't clear. setpriority (the function that change nice value) manual says:

The effect of changing the nice value may vary depending on the process-scheduling algorithm in effect.

Pthread manual says the following:

The dynamic priority is based on the nice value (set by nice(2), setpriority(2), or sched_setattr(2)) and increased for each time quantum the thread is ready to run, but denied to run by the scheduler.

It seems that PR value corresponds to dynamic priority.

The range of the NI value is -20..19. Thus the PR value can have the values from 0 (20 - 20) to 39 (20 + 19). But it is correct only for the processes with default scheduling policy (SHED_OTHER). There may be also processes with so called "real time" scheduling policies. These policies are SCHED_RR and SCHED_FIFO. Such processes have a PR value less than 0. You can check this by running chrt -r 1 top command (need to be root). The top process will have PR -2. You even can run chrt -r 90 top in which case the top process will have PR -91.

It seems that for SCHED_RR processes the PR value can be calculated by the formula:

PR = - 1 - sched_rr_priority.

Thus a SCHED_RR process has at least PR -1 which means that any SCHED_RR process has higher priority than any SCHED_OTHER. This corresponds to pthread manual:

SCHED_FIFO can be used only with static priorities higher than 0, which means that when a SCHED_FIFO threads becomes runnable, it will always immediately preempt any currently running SCHED_OTHER, SCHED_BATCH, or SCHED_IDLE thread.

SCHED_RR is a simple enhancement of SCHED_FIFO. Everything described above for SCHED_FIFO also applies to SCHED_RR,

The priority of real time processes is referred as static priority that cannot be changed by the kernel. So positive PR values can be treated as dynamic priority for non-realtime (SCHED_OTHER, SCHED_BATCH) processes and negative PR value as static priority for realtime processes (SCHED_RR, SCHED_FIFO).

I also tried to run nice -n 10 chrt -r 50 top (and chrt -r 50 nice -n 10 top). The NI value was 10, but the PR still was -51. So it seems that NI value doesn't affect priority of SCHED_RR processes. This corresponds to setpriority manual:

Any processes or threads using SCHED_FIFO or SCHED_RR shall be unaffected by a call to setpriority(). This is not considered an error. A process which subsequently reverts to SCHED_OTHER need not have its priority affected by such a setpriority() call.

One funny note. If you run chrt -r 99 top, you will see RT value instead of a number in PR column.

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
28489 root      RT   0  2852 1200  896 R    0  0.1   0:00.01 top

I don't think that this means that the process is now special. I thinks that this means that top just don't print -100 because it would take 4 character to print.

You can also use htop instead of top in all examples which can be more convenient. ps -l can be used too, but it base point that separates realtime and non-realtime priorities is not 0, but 60, so nice -n -20 ps -l will print

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 R     0 28983 28804  0  60 -20 -  1176 -      pts/6    00:00:00 ps
2
  • Oddly enough, if I run 5 infinite loops ( int main{ while(1); } ) on a 2 core hyperthreaded i3, their priorities remain constant. This in debian sid testing.
    – Vorac
    Dec 9, 2016 at 10:04
  • 4
    @BelminFernandez I think it will be fair to make this answer "accepted".
    – z0lupka
    Dec 12, 2018 at 10:08
1

Short Answer

PR is the priority level. The lower the PR, the higher the priority of the process will be.

PR is calculated as follows:

  • for normal processes: PR = 20 + NI (NI is nice and ranges from -20 to 19)
  • for real time processes: PR = - 1 - real_time_priority (real_time_priority ranges from 1 to 99)

Long Answer

There are 2 types of processes, the normal ones and the real time For the normal ones (and only for those), nice is applied as follows:

Nice

The "niceness" scale goes from -20 to 19, whereas -20 it's the highest priority and 19 the lowest priority. The priority level is calculated as follows:

PR = 20 + NI

Where NI is the nice level and PR is the priority level. So as we can see, the -20 actually maps to 0, while the 19 maps to 39.

By default, a program nice value is 0 bit it is possible for a root user to lunch programs with a specified nice value by using the following command:

nice -n <nice_value> ./myProgram 

Real Time

We could go even further. The nice priority is actually used for user programs. Whereas the UNIX/LINUX overall priority has a range of 140 values, nice value enables the process to map to the last part of the range (from 100 to 139). This equation leaves the values from 0 to 99 unreachable which will correspond to a negative PR level (from -100 to -1). To be able to access to those values, the process should be stated as "real time".

There are 5 scheduling policies in a LINUX environment that can be displayed with the following command:

chrt -m 

Which will show the following list:

1. SCHED_OTHER   the standard round-robin time-sharing policy
2. SCHED_BATCH   for "batch" style execution of processes
3. SCHED_IDLE    for running very low priority background jobs.
4. SCHED_FIFO    a first-in, first-out policy
5. SCHED_RR      a round-robin policy

The scheduling processes could be divided into 2 groups, the normal scheduling policies (1 to 3) and the real time scheduling policies (4 and 5). The real time processes will always have priority over normal processes. A real time process could be called using the following command (The example is how to declare a SCHED_RR policy):

chrt --rr <priority between 1-99> ./myProgram

To obtain the PR value for a real time process the following equation is applied:

PR = -1 - rt_prior

Where rt_prior corresponds to the priority between 1 and 99. For that reason the process which will have the higher priority over other processes will be the one called with the number 99.

It is important to note that for real time processes, the nice value is not used.

To see the current "niceness" and PR value of a process the following command can be executed:

top

It is good to note that processes with PR value -51 for example corresponds to a real time value. There are also some processes whose PR value is stated as "rt". This value actually corresponds to a PR value of -100.

(PS: I would have posted a picture showing top result but I don't have the reputation to do that)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .