niceness
value. Niceness refers to how nice
a process is with respect to other processes. A higher nice value means that the process is
willing to offer some of its CPU time to other processes. Thus, the higher the nice value,
the lower the priority. This might seem counter-intuitive in that we would expect a higher
value to mean a higher priority, but in Linux, it is just the opposite.
146
◾
Linux with Operating System Concepts
There are several ways to set or change a process’ nice value. First, through the GUI
System Monitor program, you can right click on a process (under the Process tab) and
select Change Priority, which pops up the window in Figure 4.6. From this window, chang-
ing the niceness value is handled by adjusting the slider. As you can see in the figure, you are
adjusting the process’ nice value and so a lower nice value means a higher priority.
Alternatively, you can establish the nice value of a process when you launch it from the
command line. This can be accomplished using the
nice
command. The nice command
requires that you specify the actual command as one of its parameters, so you issue nice
rather than the command itself, as shown here
nice –n
# command
The value
#
is the niceness value, an integer which ranges between
−
20 and
+
19. The
command
is the command you want to issue. By default, the nice value is
+
10.
The following two examples illustrate how to launch instructions using nice. The find
command operates with high priority while the script, myscript, runs with low priority. As
both processes are launched in the background, we are able to run them both concurrently
from the same terminal window.
nice –n
−
15 find ~ –name *.txt
>
found_files &
nice –n 15 ./myscript < inputfile > outputfile &
Once a process is running, you can change its nice value using the
renice
command.
The renice command, at a minimum, requires the new priority and some specification of
which process should be altered. You may either provide the process ID, multiple PIDs, or
a list of users. If you select the latter, then all processes by those users are altered. What fol-
lows are several examples. The –n option is used to specify the new nice value. If multiple
process IDs are provided, use –p. The –u option is used to denote users.
renice –n 5 18311
renice –n 19 –u foxr zappaf
renice –n
−
10 –p 23813 24113 26729
FIGURE 4.6
Changing process priority through GUI.
Managing Processes
◾
147
In the first and third of these examples, you are increasing the priority (lowering the
niceness) of the processes. Unfortunately, Linux will not allow this unless you are root.
While a user can give away CPU time from a process, the user is not allowed to take CPU
time away from other processes even if that user owns those processes!
Notice in the first and third examples above that we are using the PID of the
process(es) that we want to adjust. How do you obtain a PID of a process? As you have
seen throughout this section, you can obtain it by searching through the list of entries
in the Processes tab of the GUI System Monitor, or by looking at the entries listed by the
top or ps command. But rather than searching through these lists, you can simplify the
task for yourself.
You can use
ps aux
and pipe the result to grep. The grep program, which we will
explore in Chapter 6, searches lines of text to match against a regular expression or a
literal string. In our case, we want to match the response from
ps aux
to a string that
describes the process(es) you are searching for. This might be your user name or it might
be the name of the process, or even both. Below we see three examples. In the first, we are
looking for all processes owned by user zappaf. In the second, we are looking for all bash
processes. In the third, we are looking for all bash processes owned by zappaf. Note that
the order of the two grep statements in the third example is immaterial. The result will
be the same.
ps aux | grep zappaf
ps aux | grep bash
ps aux | grep bash | grep zappaf
Alternatively, we can use the command
pidof
. This command returns the PIDs of pro-
cesses matching the command name given. For instance,
pidof bash
, will return the PID
of every bash process running. Unlike ps which returns one process per line, this returns
all of the PIDs as a single list. The parameter –s causes pidof to exit after it outputs the first
found PID. You can supply pidof with a list of as many programs as desired. However, pidof
does not differentiate between the programs found in its output.
Aside from renice, there are many other reasons for acquiring a process’ ID. One such
example is the lsof command. This command provides a list of open files attributed to the
process. The command’s structure is
lsof –p
PID
. We visit another use of the PID in
the next section.
4.6 KILLING PROCESSES
4.6.1 Process Termination
Typically, a Linux program runs until it reaches a normal termination point. In a program
like ls, this occurs once the program has output all of the content that matches its param-
eters. In a program like cd, this occurs when the PWD and OLDPWD variables have been
modified. In interactive programs, normal termination occurs when the user has specified
that the program should exit. In top, this happens when the user enters a q, or for the vi
148
◾
Linux with Operating System Concepts
editor, when the user enters
:q
. In a GUI, it happens when the user closes the window or
selects Exit.
Some processes do not terminate normally. Some processes reach an error and stop
functioning. In many Linux programs, an abnormal error causes a core dump to be
saved. A
Do'stlaringiz bilan baham: |