4. Setting Priority to Processes: nice, renice

Every process in the system is running with defined priorities, also called “nice value”, which may vary from -20 (highest priority) to 19 (lowest priority). If not defined, every process will run with a default priority of 0 (the “base” scheduling priority). Processes with greater priority (lower nice value, down to -20) will be scheduled to run more often than others which have less priority (up to 19), thus granting them more processor cycles. Regular users may only lower the priority of processes they own within a range of 0 to 19. The super-user (root) may set the priority of any process to any value.

4.1. renice

If one or more processes use too many system resources, you can change their priorities instead of killing them. To do so, use the renice command. Its syntax is as follows:

renice priority [[-p] pid ...] [[-g] pgrp ...] [[-u] user ...]

where priority is the value of the priority, pid (use option -p for multiple processes) is the process ID, pgrp (use the -g option if there is more than one) is the process group ID, and user (-u for more than one) is the user name of the process' owner.

Let's suppose you have a process running with PID 785, which is performing a long and complex scientific calculation, and while it is working you want to play a game for which you need to free system resources. Then you would type:

$ renice +15 785

In this case your process could potentially take longer to complete but will not take CPU time from other processes.

If you are the system administrator and you see that some user is running too many processes and they are using too many system resources, you can change that user's processes priority with a single command:

# renice +20 -u peter

After this, all of peter's processes will have the lowest priority and will not obstruct any other processes launched by other users.

4.2. nice

Now that you know that you can change the priority of processes, you may wish to run a command with a defined priority. For this, use the nice command.

In this case you need to specify your command as an option to nice. Option -n is used to set priority value. By default nice sets a priority of 10.

For example, you want to create an ISO image of a Mandriva Linux installation CD-ROM:

$ dd if=/dev/cdrom of=~/mandriva1.iso

On some systems with a standard IDE CD-ROM, the process of copying large volumes of information can use too many system resources. To prevent the copying from blocking other processes, you can start the process with a lower priority by using this command:

$ nice -n 19 dd if=/dev/cdrom of=~/mandriva1.iso