#22: Processes with high and low priority

Posted by | Comments (4) | Trackbacks (3)

Processes in Linux may run with different priorities so that important processes get more CPU time than less important ones. This priority is called the “nice value” or ”niceness” (NI). The nice value is a number between -20 and +19 with the default value right in the center at 0. The higher this nice value, the “nicer” a process is to others, i.e., the lower its priority.

To see nice values of all processes, run top or ps axl. To examine only the nice value of a specific process, you can also execute

ps -o nice -p PID

where PID has to be replaced by the actual process ID.

When running top or ps axl you see two columns containing priority values. These two columns are PR and NI. There is much confusion about what the difference between PR and NI is. The PR column contains the current internal schedule priority of the process. The higher this number is, the less likely it is that the scheduler will give CPU time to this process if others with higher priority need to be scheduled. The PR value changes constantly based on the amount of CPU time a processes takes. But for the time being we won't concentrate on the PR value. Much more interesting is the NI column which contains our nice value. By default this is set to 0, but you might also see some processes with different nice values.

Now we want to alter the nice value. To start a program in a process with custom nice value, we can use nice. But here we have to be careful. nice is an executable at /usr/bin/nice, but it is also a shell built in in many common shell implementations like bash. Both differ in their usage. To start a program with a specific nice value using the shell built-in, run

nice -5 myprogram

This will start myprogram with a nice value of 5, which is a slightly lower priority. To start myprogram with a nice value of -5, which is a higher priority, type

nice --5 myprogram

This is quite confusing because nice -5 looks as if we were setting the nice value to negative 5, but actually that is not the case.

To use the executable instead of the shell-builtin, run

/usr/bin/nice -n 5 myprogram

to set the nice value to 5 or

/usr/bin/nice -n -5 myprogram

to set the nice value to -5. This variant is much clearer and that one I prefer.

nice is used to start a program with a custom nice value, but what if we want to change the nice value of a process that's already running? For this purpose we use renice, which takes the new nice value and the PID of the process we want to change as arguments:

renice -n 7 3456

This sets the nice value of the process with PID 3456 to 7.

One thing to note: as a normal user you can only lower the priority of processes you own. To raise the priority (set to a lower nice value) or to change the nice value of a process owned by another user, you have to be root. This also means that as a normal user you can't reset the niceness to its old low value once you have increased it.

Another way to change the nice value of running processes is to use top. When executing top, type r to renice a process. top then asks you for the PID and the new nice value. Of course, also here you have to be root to lower the nice value or to renice processes you do not own.

One last tip at the very end: if you use ps ax or ps aux to get a BSD-style list of processes, you can also see whether a process has low or high priority. Look at the STAT column: the first letter is the status, but the following characters give you some more detailed information such as < for high or N for low priority (nice to other processes). Other values not referring to process priority are also possible. For a complete list consult the man page for ps.

Learn more about process priority and scheduling:

Trackbacks

Manko10 sent a Trackback on : (permalink)

RT @reflinux: #Advent series "24 Short #Linux #Hints", day 22: #Processes with high and low #priority http://bit.ly/dLk0pg

robo47 sent a Trackback on : (permalink)

RT @reflinux: #Advent series "24 Short #Linux #Hints", day 22: #Processes with high and low #priority http://bit.ly/dLk0pg

klikunikom sent a Trackback on : (permalink)

via @reflinux #Advent series "24 Short #Linux #Hints", day 22: #Processes with high and low #priority http://bit.ly/dLk0pg

Comments

There have been 4 comments submitted yet. Add one as well!
Imre
Imre wrote on : (permalink)
Hi, is this scheduling priority or is this perhaps a way to make sure a process runs and uses less CPU or more CPU ? Many thanks in advance
Janek Bevendorff
Janek Bevendorff wrote on : (permalink)
Hi, The nice value of a process only determines its scheduling priority and does not actually limit the amount of CPU time it gets. For forcing certain processes to only use a certain amount of your CPU see this article here: "Programmatically limit CPU usage of certain processes":http://www.refining-linux.org/archives/64/Programmatically-limit-CPU-usage-of-certain-processes/
eyecon
eyecon wrote on : (permalink)
hello, just found the site, i've got a lot of reading to do

Write a comment:

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

By submitting a comment, you agree to our privacy policy.

Design and Code Copyright © 2010-2024 Janek Bevendorff Content on this site is published under the terms of the GNU Free Documentation License (GFDL). You may redistribute content only in compliance with these terms.