Process Management
65
Changing Process Priority with nice
You don’t often hear the word
nice used in the context of hackers, but here
you will. The
nice
command is used to influence the
priority of a process to
the kernel. As you saw when we ran the
ps
command, numerous processes
run on the system at once, and all of them are contending for the available
resources. The kernel will have final say
over the priority of a process, but
you can use
nice
to
suggest that a process should be elevated in priority.
The idea behind the use of the term
nice is that, when you use it, you’re
determining how “nice” you’ll be to
other users: if your process is using most
of
the system resources, you aren’t being very nice.
The values for
nice
range from −20 to +19, with zero being the default
value (see Figure 61). A high
nice
value translates to a low priority, and a
low
nice
value translates to a high priority (when you’re not being so nice
to other users and processes).
When a process is started, it inherits the
nice
value of its parent process. The owner of the process can lower the priority
of the process but cannot increase its priority. Of course, the superuser or
root user
can arbitrarily set the
nice
value to whatever they please.
0
−20
+19
Default
nice value
Least likely to
receive priority
Most likely to
receive priority
Figure 6-1: Niceness priority values
When you start a process, you can set the priority level with the
nice
command and then alter the priority after the process has started run
ning with the
renice
command. The syntax for these two commands is
slightly different and can be confusing. The
nice
command requires that
you
increment the
nice
value, whereas the
renice
command wants an
absolute
value for niceness. Let’s look at an example to demonstrate this.