[Tfug] Nice and Priority

Adrian choprboy at dakotacom.net
Tue Feb 20 17:02:12 MST 2007


On Tuesday 20 February 2007 15:45, Felix Tilley wrote:
> LINUX:
> 
> Please explain the difference between nice and priority.
> 

Niceness and priority are essentually reciprocals of each other. The greater 
the "nice" value, the more willing the program is to give up it's CPU time 
slices to other processes. Niceness values are from -19 to 20 (though this 
can vary depending on how the default priority is setup). Any user can give 
there process a greater nice value, but only root can assign a negative nice 
value (i.e. make it greedier).

The priority number, on the other hand, is the inverse of what you would 
expect. The smaller the priority value, the more important the process and 
the closer to the processor it is placed in the queue. Priority values run 
from 1 (being the most important) to 40 (being the least) (or is it 0-39, I 
forget).

Therefore... priority determines how much/how often a process is given CPU 
time and niceness allows a process to give-up/request more time. Niceness 
directly affects priority by adding/subtracting from the default priority 
value.

Here is an example from my machine. My current setup (seems to be fairly 
standard) assigns an initial priority of 15 to user processes. Children of a 
process are usually assign a priority 1 greater than their parent (i.e. 1 
step less importance). Here is the process tree (partial):

 4433 ?        S      0:15  \_ kdeinit: konsole
 4434 pts/3    Ss     0:00  |   \_ /bin/bash
 4467 pts/3    S+     0:00  |       \_ ping xx.xx.xx.xx
 4468 pts/3    S+     0:01  |       \_ tee ping_xx-xx-xx-xx.log

The kdeinit:konsole is a child off KDE (not shown) handling the window where 
bash is started. Bash was, in turn, used to execute "ping", which spawns a 
child "tee" process (i.e. "ping xx.xx.xx.xx |tee ping.log"). I just happened 
to have this running at the moment.

Here is the nice and priority values from "top":
 PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 4433 ajensen   16   0 30052  15m  11m S  0.0  1.6   0:15.46 kdeinit
 4434 ajensen   15   0  4424 1468 1188 S  0.0  0.2   0:00.14 bash
 4467 ajensen   15   0  1708  536  452 S  0.0  0.1   0:00.83 ping
 4468 ajensen   16   0  3580  472  408 S  0.0  0.0   0:01.06 tee

Note that bash and ping are running with a priority of 15 and a nice of 0. The 
child kdeinit and tee both also have a 0 niceness, but run with a slightly 
larger, less importance, priority of 16.

Now, suppose I am also running decrypt(pr0n) at the same time and that both 
decrypt and ping take up all the free CPU time (100% load)... I don;t really 
care when ping finishes, as long as it does eventually. But I want my pr0n 
now! So I can assign a nice value to my ping process to make it more likely 
to give up CPU time:

renice 10 -p 4467

Now we look at the process tree and see:

 4433 ?        S      0:15  \_ kdeinit: konsole
 4434 pts/3    Ss     0:00  |   \_ /bin/bash
 4467 pts/3    SN+    0:00  |       \_ ping xx.xx.xx.xx
 4468 pts/3    S+     0:01  |       \_ tee ping_xx-xx-xx-xx.log

Note that the ping process now shows it has been niced. An output from top 
shows the values:

 PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 4433 ajensen   16   0 30052  15m  11m S  0.0  1.6   0:15.46 kdeinit
 4434 ajensen   15   0  4424 1468 1188 S  0.0  0.2   0:00.14 bash
 4467 ajensen   25  10  1708  536  452 S  0.0  0.1   0:00.83 ping
 4468 ajensen   16   0  3580  472  408 S  0.0  0.0   0:01.06 tee

So ping has a +10 nice value, which has increased it's priority value, making 
it less likely to run if any other process wants CPU time. If both my decrypt 
and ping processes had a value of 15, both would get approximately equal 
amounts of CPU time. But since decrypt has a value of 15, and ping now has a 
value of 25, decrypt will get a far larger share of the CPU than ping.

Adrian




More information about the tfug mailing list