155

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?

Byte Commander
  • 107,489

3 Answers3

138

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
    
pl_rock
  • 11,297
  • 2
    Are you saying, they are almost the same thing in reverse (both represent priority) BUT niceness can be used to give a negative priority so that task with the niceness gets out of the way of high priority tasks ? (ie it is being nice and letting other access resources ? Or did this just confuse me ? – Mark Kirby Aug 05 '15 at 09:59
  • 2
    An example of what I mean, two tasks, both have pr of 20, so equal, task one has a ni of 0 and task two has an ni of 20, so, this would mean that task two would give up resources for task one because it is nicer – Mark Kirby Aug 05 '15 at 10:05
  • 1
    As i know , priority calculated as PR = 20 + NI . so task one priority = 20 +0 . maximum value of nice will +19 – pl_rock Aug 05 '15 at 10:36
  • 4
    PR=20+NI maximum value can be 39 . actually in linux system there is 139 priority in which 0 to 99 is real time priority and for user there is 100 to 139 . so , NI value -20 to +19 maps to priority 100 to 139 . that you can adjust . but still kernel not sure if you change NI value then it will change priority , NI value is a just suggestion to kernel . kernel some time ignore it – pl_rock Aug 05 '15 at 10:50
  • 2
    So is PR and NI are actually equivalent, as they've just a different offset? Why do we have both values then? And you could add that setting niceness of a process <0 requires root rights. – Byte Commander Aug 05 '15 at 12:22
  • yes it require root privilege. – pl_rock Aug 05 '15 at 12:40
  • 1
    @ByteCommander: Note that nice is part of the POSIX standard, which may be a reason why nice is exposed alongside the non-standard priority. I'm not sure if this is the primary reason, however. – Fengyang Wang Aug 05 '15 at 12:53
  • If you set a process priority, is its niceness value ignored? Or does Linux it still combine the 2 values to get a final priority value? Ex. If I have a process with priority 20 and a nice value of 0, is that the same as priority of 19 with a nice value of 1? – Tal Apr 24 '18 at 17:30
  • Correct me If I make any mistake:
    1. If relation between nice and priority is given by equation pri = nc + 20, then it's in contradiction with fact "The lower NC, the higher PRI". From this equation, we can see pri and nc are actually proportional, but not reversed in proportion.

    2. Also, the higher PRI means that CPU will try to execute that process as soon as it can.

    – mk1024 Sep 15 '18 at 15:06
  • 1
    @mk1024 The contradiction is only created by language: here "higher priority" means actually lower number, but the meaning is mapped the opposite way for "niceness". – Alois Mahdal Jan 16 '19 at 22:13
  • @AloisMahdal I'm clear with NICE (the higher nice, the less important process). So in short: If for instance Firefox launched in my system has PRI = 3 and Chrome has PRI = 15, that means Firefox is more important process at the moment? – mk1024 Jan 17 '19 at 05:55
  • @MarkKirby nope. Its some short of conversion: NI 19 actually means PR 39, which means that the kernel will interpret it as priority 139. kernel threads have priorities from 0 to 99, which gives them preference over ANY task you can schedule in user space. Take it as some short of celsius -> kelvin degrees conversion. Now, why nice can have negative values? It is just for convenience: 0 is the default value of nice, so if you want to set a high priority task you use negative numbers, while a low priority uses positive. It makes it easier to temember than making 20 default. – DGoiko Feb 02 '19 at 13:17
  • or even worse: imagine we didn't have the PR convention: devs and users would need to remember that they can only set priorities between 100 and 139 and that 120 is the default one. Sounds odd, doesn't it? Conside NI as a way to simplify things. Almost forgot: kernel is not allways forced to obey NI – DGoiko Feb 02 '19 at 13:19
  • By default when a program is launched in Linux, it gets launched with the priority of '0'.`you mean niceness of 0, right? – Alois Mahdal Aug 03 '23 at 16:34
39

What is Priority and Why Should I Care?

When talking about processes priority is all about managing processor time. The Processor or CPU is like a human juggling multiple tasks at the same time. Sometimes we can have enough room to take on multiple projects. Sometimes we can only focus on one thing at a time. Other times something important pops up and we want to devote all of our energy into solving that problem while putting less important tasks on the back burner.

In Linux we can set guidelines for the CPU to follow when it is looking at all the tasks it has to do. These guidelines are called niceness or nice value. The Linux niceness scale goes from -20 to 19. The lower the number the more priority that task gets. If the niceness value is high number like 19 the task will be set to the lowest priority and the CPU will process it whenever it gets a chance. The default nice value is zero.

By using this scale we can allocate our CPU resources more appropriately. Lower priority programs that are not important can be set to a higher nice value, while high priority programs like daemons and services can be set to receive more of the CPU’s focus. You can even give a specific user a lower nice value for all of his/her processes so you can limit their ability to slow down the computer’s core services.

Source

Set the priority for new processes with nice, eg

nice -n 10 firefox

for existing processes

renice 10 -p $(pgrep firefox)

To set the priority <0 you need sudo, eg:

renice -1 -p $(pgrep firefox)
renice: failed to set priority for 2769 (process ID): Permission denied

but not for a priority >=0


Example

% ps -o pid,comm,pri,nice -p $(pgrep firefox)
  PID COMMAND         PRI  NI
 2769 firefox          19   0

% renice 10 -p 2769     # note, we don't need sudo here
2769 (process ID) old priority 0, new priority 10

% ps -o pid,comm,pri,nice -p $(pgrep firefox)
  PID COMMAND         PRI  NI
 2769 firefox           9  10

% sudo renice -19 -p 2769                    
 2769 (process ID) old priority 10, new priority -19

% ps -o pid,comm,pri,nice -p $(pgrep firefox)
  PID COMMAND         PRI  NI
 2769 firefox          38 -19

Other example

To renice all running processes for a specific user

renice 20 -u user_name
A.B.
  • 90,397
  • In your example, the renice 10 would also work without sudo, right? And could you please add an example to make all processes of a specific user more nice? That would be actually pretty useful if somebody else is logged in at the same time and has some unnecessary but resource-eating processes running (like a paused flash-game in Firefox, little brothers do such stuff... :-/ ) – Byte Commander Aug 05 '15 at 12:25
  • @ByteCommander done =) – A.B. Aug 05 '15 at 13:02
  • How do I restore the old niceness values of that user after a while? Setting it back to 0? That won't take into account that some processes might have had a different niceness value before. Is there any simple possibility to do this? – Byte Commander Aug 05 '15 at 13:04
  • @ByteCommander No, it's not simple. Either you use 0 or … there is the problem ;) – A.B. Aug 05 '15 at 13:11
  • @ByteCommander but I could write a script for that ;) – A.B. Aug 05 '15 at 15:09
  • 2
    After renice -19, the result with ps is: PRI: 38, NI: -19, however, with top it is PR:1, NI -19 why is that? – wakeup Feb 20 '18 at 16:27
33

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 is 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, but it is possible for the root user to launch 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

Which shows the following output:

top screenshot

In the figure the PR and NI values are displayed. It is good to note the process with PR value -51 that 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.

Evan
  • 105
  • 3