3.6 Objective 6: Modify Process
Execution Priorities
Certain tasks on the system require more
execution time than others, and thus deserve to be allocated
more frequent execution and longer time slices by the kernel.
For this reason, each process is given an execution priority. Usually, you don't need to
worry about execution priority because the kernel handles it
automatically. Each process' 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/O --
such 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.
3.6.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.
By default, user processes are created with a
nice number of zero. With this setting, nice doesn't sway the kernel's
prioritization scheme one way or another. Positive numbers
lower the priority, and negative numbers raise the priority.
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'
nice number to raise its priority. For example, if you have a
long-running utility program but don't want to impact
interactive performance, a positive nice number will lower the
job's priority and improve interactive performance.
Syntaxnice [-n adjustment] [command ]
nice [-adjustment] [command ]
Description
The nice
command is used to alter another command's nice number at
start time. For normal users, adjustment is an integer
from 1 to 19. If you're the superuser, the adjustment
range is from -20 to 19. If an adjustment number is not
specified, the process' nice
number defaults to 10. The command consists of any
command that you might enter on the command line, including
all options, arguments, redirections, and the background
character &.
If both adjustment and command
are omitted, nice displays the current scheduling
priority, which is inherited.
Example 1
The following command starts a program in the
background with reduced priority, using the default nice
number of 10: $ nice somecmd -opt1 -opt2 arg1 arg2 &
Example 2
As superuser, you can start programs with
elevated priority. These equivalent commands start the vi editor with a higher priority,
which may be necessary for administrative purposes if the
system is exceptionally slow: # nice --10 vi /etc/hosts.deny
# nice -n -10 vi /etc/hosts.deny
Note the double dash ( -- 10) in the first form. The first
dash indicates that an option follows, while the second dash
indicates a negative number.
Be careful when using nice on interactive programs such as
editors, word processors, or browsers. Assigning a program a
positive nice number will most likely result in sluggish
performance. Remember, the higher the positive number, the
lower the resulting priority level. For that
reason, you should try not to assign positive nice numbers to
foreground jobs on your terminal. If the system gets busy,
your terminal could hang awaiting CPU time, which has been
sacrificed by nice.
3.6.1.1 Changing nice numbers on
running processes
The nice
command only works to change the nice number for new processes
at the time that they're started. To modify a running program, use
the renice command.
Syntaxrenice [+|-]nicenumber [option] targets
Description
Alter the nicenumber to set the
scheduling priority of one or more running target
processes. By default, renice
assumes that the targets are numeric PIDs. One or more
options may also be used to interpret targets as
processes owned by specific users.
Frequently used options
- -u
-
Interpret targets as usernames,
affecting all processes owned by those users.
- -p
-
Interpret targets as PIDs (the
default).
Examples
This command will lower the priority of the
process with PID 501 by increasing its nice number to the maximum:
$ renice 20 501
The following command can be used to increase
the priority of all of user jdean's processes as well
as the process with PID 501: # renice -10 -u jdean -p 501
In this command, -10 indicates a nice value of negative 10, thus
giving PID 501 a higher priority on the system. A dash isn't
used for the nice value because
the dash could be confused for an option, such as -u.
Be sure to know the range and meaning
of nice numbers and how
to change them for new and existing processes. Also note
that nice and renice specify their numbers
differently. With nice, a
leading dash can indicate a
nice number (e.g., -10),
including a negative one with a second dash (e.g., -- 10). On the other hand,
renice does not need the
hyphen. |
The renice
command is handy for managing groups of processes,
particularly to affect user processes by username. In
addition, if you're observing processes in top, you may change nice
numbers for them interactively from within the top program
(discussed in Objective 5) 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 may enter
negative values). The new nice number will be displayed by
top in the column labeled
NI for the process you specify.
|