Ubuntu – Process ‘niceness’ vs. ‘priority’

niceprocessprocess-priority

When running top, I can see this (shortened) example output:

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 4586 ipc-adm+  20   0 1303900 605152  92844 S  30,6 29,3   3:52.88 firefox
 3985 ipc-adm+  20   0  258588 124508  63072 S  12,2  6,0   0:40.04 compiz
 3092 root      20   0  172392  56164  25980 S   6,1  2,7   0:30.13 Xorg

There are two values that I am interested in: PR (Priority) and NI (Niceness).

If I understood what I already found out correctly, both determine how much CPU time a process will get in contrast to other processes. But what is the difference between those values then?

Could you also please describe how to manipulate those values of a process and under which circumstances this might be useful?

Best Answer

Nice value is a user-space and priority PR is the process's actual priority that use by Linux kernel. In linux system priorities are 0 to 139 in which 0 to 99 for real time and 100 to 139 for users. nice value range is -20 to +19 where -20 is highest, 0 default and +19 is lowest. relation between nice value and priority is :

PR = 20 + NI

so , the value of PR = 20 + (-20 to +19) is 0 to 39 that maps 100 to 139.

According to top manual:

PR -- Priority The scheduling priority of the task. If you see 'rt' in this field, it means the task is running under 'real time' scheduling priority.

NI is nice value of task.

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 dispatch-ability

Edit: By default when a program is launched in Linux, it gets launched with the priority of '0'. However you can change the priority of your programs by either of the following methods.

  1. You can launch a program with your required priority using

    nice -n nice_value program_name
    
  2. you can also change the priority of an already running process using

    renice -n nice_value -p process_id