back-
ground
, as opposed to a process being in the
foreground
which is the default in Linux.
What is a background process? A background process has two different meanings
depending on the context. One meaning is that the process runs when the CPU has time
for it. This is the case when you want to run a time-consuming process that takes up a lot
of system resources, but you do not want it to interfere with other tasks. So for instance, the
process may run when you have taken a break and then move back to the background when
you start working again. In our case, background means that the process will work along-
side of other processes, but will do so without interaction with the user. In both Windows
and Linux, GUI processes can be minimized or moved to the back of the desktop. In such
cases, you are no longer interacting with them, so they are in the background.
The use of & tells the operating system that the process, launched from the command
line, should run in the background so that the user can interact with other processes. The
interpreter responds with a message to indicate that the process has been launched. This
message has the form
[1] 3811
The value in the [] is the job number (discussed shortly) and the second value is the
process ID. The process then executes until completion. Upon completion of the process,
the terminal window displays a message to indicate that the process has completed, which
looks like this
[1]
+
Done
command
where
command
is the full command entered by the user.
If you launch a program in the background and do not redirect its output, upon ter-
mination, the program’s output will be displayed to the terminal window. This might be
Managing Processes
◾
137
confusing to you the user as, while you are performing command line input, you might
find suddenly a bunch of output appearing.
You may run as many processes as you wish in the background. A terminal window,
however, can only have one process running in the foreground. The foreground process is
the one that provides interaction with the user, whether that is to receive the user input data
or commands from the user, or to output information to the window, or both. However,
this does not mean that while a process is running in the foreground, you are not able to
launch other processes or move background processes to the foreground.
Before discussing how to move processes between foreground and background, we
need a mechanism to identify the foreground and background processes in the termi-
nal window. We obtain this information with the command
jobs
. The jobs command
responds with all of the active jobs in the terminal window. These are processes that have
been started but not yet terminated. A process will have one of three different statuses. It
may be in the foreground, it may be in the background, or it may be stopped (suspended).
However, a process will not be in the foreground if you are executing jobs because jobs
require that you have access to the command line. So what you will find as processes listed
by jobs is that they are either stopped or in the background.
To stop a process that is in the foreground, type
control
+
z
in the terminal window.
This presents you with information like this:
[2]
+
Stopped
find ~ -name *.txt
This line tells us that job #2, find, was stopped. The plus sign is described below.
To resume this process, type
fg
. The command fg moves this process into the fore-
ground. Alternatively, to resume this job in the background, type
bg
. Thus, we have two
ways to get a job into the background. We can launch it in the background using & or we
can launch the process, stop it, and then move it to the background using bg. We can simi-
larly switch running processes between fg and bg. Let us take a closer look.
Imagine that we have started top. We suspend it with control
+
z. We then start vi and
suspend it followed by a man jobs command and then a grep process. We resume man, stop
it, then resume top, and stop it. Typing jobs gives us the following output:
[1]
+
Stopped
top
[2] Stopped
vi file1.txt
[3]- Stopped
man jobs
[4] Stopped
grep files *.*
This tells us that there are four active processes in the current terminal window. All four
are current stopped. The numbers in the listing indicate the order that the processes were
started. The
+
and
−
indicate the most recent two processes that were in the foreground. In
this case, top was the most recent foreground process and man jobs the second most recent.
Now, we want to move the grep instruction into the background. We do so typing
bg
4
. If we were to type
bg
without a number, it would move top into the background
138
◾
Linux with Operating System Concepts
because it was the last process to be referenced (it has the
+
). Moving any of top, vi, or
man into the background makes little sense because each of these processes require user
interaction (grep does not). If we were to type
fg
2
, it would move vi into the foreground.
Typing control
+
z followed by jobs would now yield the following output. Notice how
the
+
and
−
have changed.
[1] Stopped
top
[2]
+
Stopped
vi file1.txt
[3] Stopped
man jobs
[4]
−
Stopped
grep files *.*
Note that the job number is not in any way related to the process ID. If there is only a
single job, you can move it to the foreground or background using
fg
or
bg
respectively
without a job number. Otherwise, fg and bg will move the job with the
+
to the foreground
or background. A stopped process resumes executing if it is moved to the foreground or
the background. However, the resumed process, if moved to the background, may not exe-
cute until the CPU has time for it.
4.4 MONITORING PROCESSES
You might find that, as a user, you seldom have to keep tabs on running processes. The
operating system can run multiple processes efficiently with little or no user intervention.
Yet there are times when you might want to see what a process is doing. For instance, a
process may be using an inordinate amount of system resources or a process might have
died on you.
4.4.1 GUI Monitoring Tools
There are several different tools available to monitor processes and system resources. The
primary GUI tool is called the System Monitor. There are two different versions, one for
Gnome and one for KDE. Figure 4.2 shows the Gnome version. There are four tabs along
the top of the GUI, System, Processes, Resources, and File Systems.
In Figure 4.2, Processes is selected and the active processes are listed in the window.
They are ordered, in this case, by CPU utilization. You might notice that the process requir-
ing the most CPU attention is the system monitor itself! It is typical that GUI programs
will require far greater CPU utilization than text-based programs. The next program in
CPU utilization is a find command, running inside a gnome-terminal window. You might
notice that the gnome-terminal process is “sleeping.” In fact, it is running the find com-
mand, so while find is executing, the terminal window is not accessible (unless we suspend
find as we discussed in Section 4.3). We will examine the meaning behind the values under
Status in the next subsection when we review the ps command.
Other information in this window describes for each process the process ID, nice-
ness, memory utilization, waiting channel, and session (not shown in the figure). We have
already discussed the process ID (PID) and we will continue to reference it in this chapter.
The niceness of a process describes its priority. We will talk about that in more detail in
Managing Processes
◾
139
the next section. Memory utilization is the amount of memory that the process is currently
holding on to. We will ignore the waiting channel and session information.
Also notice that the window provides a summary of load averages. These averages
describe the amount of CPU utilization, where 0.12 means 12%. We see the load averages
for the previous 1, 5, and 15 min.
From the Processes tab, aside from viewing the processes, you are able to select any
single process and stop it, resume it, kill it, end it, or change its priority. Ending a process
terminates it as if you select exit from the process’ GUI (e.g., “exit” from the “file” menu).
Killing a process ends it immediately which may damage open files or cause processes
spawned by this process to be abandoned. It is better to end a process than kill it unless the
process has stopped responded.
The Resources tab provides a summary of CPU, memory and swap space usage, and net-
work utilization over time. An example is shown in Figure 4.3. This tab is strictly output.
You might notice in this case almost no swapping has been done. This is because the user
has not run many large processes. Network utilization has increased, which was caused by
the user starting Firefox.
The File Systems tab displays statistics about the file system. This information is similar
to that provided by the program df. Finally, the System tab summarizes operating system
specifications.
The KDE system monitor has two tabs, Process Table and System Load. While both
are similar to the Processes and Resources tab from the Gnome system monitor, they
both contain a useful summary along the bottom of the window. The summary provides
the total number of processes, current CPU utilization, current memory use, and swap
space use.
FIGURE 4.2
Process listing shown in system monitor.
140
◾
Linux with Operating System Concepts
4.4.2 Command-Line Monitoring Tools
Anything that is available via the GUI is also available by the command line. The
top
command is an interactive program that outputs current process resource utilization. The
top program refreshes itself every 3 s. You can alter the update period by adding
–d s.t
where
Do'stlaringiz bilan baham: |