Every process in the system is running with defined priorities (also called “nice value”). This value may vary from -20 to +20. The maximum priority value for processes is -20. If it is not defined, every process will run with a default priority of 0 (the “base” scheduling priority). Processes with maximum priority (any negative value up to -20) use more system resources than others. Processes with minimal priority (+20) will work when the system is not used by other tasks. Users other than the super-user may only lower the priority of processes they own within a range of 0 to 20. The super-user (root) may set the priority of any process to any value.
If one or more processes use too many system resources, you can change their priorities instead of killing them. For such tasks the renice command can be used. 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 (introduced by -g if various) 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 run a process with PID 785, which makes a long scientific operation, and while it is working you want to play a game. 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 use too many system resources, you can change that user's process 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 user's processes.
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. By default nice sets a priority of 10. The “niceness” range goes from -20 (highest priority) to 19 (lowest). Option -n is used to set priority value.
For example, you want to create an ISO image of a Mandrake Linux installation CD-ROM:
$ dd if=/dev/cdrom of=~/mdk1.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 all other processes, you can start the process with a lowered priority by using this command:
$ nice -n 19 dd if=/dev/cdrom of=~/mdk1.iso |
and continue with what you were doing.
To change a process' priority you also can use the above described top utility. Use the r command within top's interface to change the priority of selected process.