LPI Linux Certification in a Nutshell (In a Nutshell (OReilly))
5.7. Objective 6: Modify Process Execution Priorities
One of the features of Linux is that the administrator has the ability to prioritize process execution. This feature is handy when you have a high load machine and want to make sure special processes (like yours!) get more rights to use system resources than others. It also is useful if you have a process that's gone haywire and you want to debug the problem prior to killing it. On the flip side, you can bury non-essential processes giving them the lowest priority so they don't ever conflict with other processes. (Particularly useful when participating in experiments to search for extraterrestrial intelligence with SETI@home.) Generally, on a day-to-day basis, you don't need to worry about execution priority because the kernel handles it automatically. Each process's priority level is constantly and dynamically raised and lowered by the kernel according to a number of parameters, such as how much system time it has already consumed and its status (perhaps waiting for I/Osuch processes are favored by the kernel). Linux gives you the ability to bias the kernel's priority algorithm, favoring certain processes over others. The priority of a process can be determined by examining the PRI column in the results produced from issuing either the top or ps -l commands. The values displayed are relative; the higher the priority number, the more CPU time the kernel offers to the process. The kernel does this by managing a queue of processes. Those with high priority are given more time, and those with low priority are given less time. On a heavily loaded system, a process with a very low priority may appear stalled. 5.7.1. nice
One of the parameters used by the kernel to assign process priority is supplied by the user and is called a nice number. The nice command [*] is used to assign a priority number to the process. It is so named because it normally causes programs to execute with lower priority levels than with their default. Thus, the process is being "nice" to other processes on the system by yielding CPU time. With this scheme, more "niceness" implies a lower priority, and less niceness implies a higher priority. [*] Some shells, not including bash, have a built-in nice command. By default, user processes are created with a nice number of zero. Positive numbers lower the priority relative to other processes, and negative numbers raise it. For example, if you have a long-running utility and don't want to impact interactive performance, a positive nice number will lower the job's priority and improve interactive performance. Nice numbers range from -20 to +19. Any user can start a process with a positive nice number, but only the superuser (root) can lower a process's nice number and thus raise its priority.
You can renice processes interactively using top's text interface by using the single-keystroke r command. You will be prompted for the PID of the process whose nice number you wish to change and for the new nice number. If you are the superuser, you can enter negative values. The new nice number will be displayed by top in the column labeled NI for the process you specify. |