142
◾
Linux with Operating System Concepts
first display before it self terminates. If you do not specify –n, then you must stop top on
your own. This can be done by either typing a q or pressing
control
+
c
.
The
ps
command is to some extent an inverse of top; it shows a detailed examination of
the running processes as a snapshot (an instance in time). Unlike top, it is not interactive.
It displays the information and exits.
The ps command by itself (with no options) displays only the running processes of the
current user in the current terminal window. If you are typing from the command line,
this means that any other processes are suspended
or running in the background, aside
from Bash itself.
Below is an example of what you might find when running ps. In this case, we see only
two processes, the ps command itself and bash. By the time the output is displayed, the
ps command will have terminated and the only active process is bash. The information
displayed when using ps without options is minimal: the PID, the terminal window, the
amount of CPU time being used by the process, and the command name itself.
PID
TTY
TIME CMD
16922 pts/0
00:00:00 bash
24042 pts/0
00:00:00 ps
There are a multitude of options available for ps although
their proper usage can be
confusing because of ps’ history. The ps command originated from early Unix and has
been modified for BSD Unix and the GNU’s project. The result is that there are three cat-
egories of options available. The original version of ps required that options be preceded
by hyphens, the BSD version required that options
not
be preceded by hyphens, and the
GNUs version required that options be preceded by double hyphens (––). Therefore, it is
important to remember which options require which notations.
In order to
display all processes, you can use any of
ps –A
,
ps –e
or
ps ax
. The ‘a’
and ‘x’ options list all user processes and processes outside of the given terminal window
respectively so their combination is all processes started by all users in all windows as well
as processes run from the GUI. You can restrict those processes to a given list of users by
using
–u
user,user,user
where each
user
is either the user’s ID or username (you can
also use –U or –– User). Notice that each user is separated by a comma but no space. There
are several ways to display threads along with processes using H, –L, m, or –m. In the lat-
ter two cases, the threads appear after the processes. The option r outputs only running
processes.
You can also request the status of specific processes by listing the command names.
This version
of ps uses the option
–C commandlist
. As with the user list, the
com-
mandlist
is a list of command names separated by commas (but no spaces). For instance,
if you have several terminal windows open and are running processes like bash, emacs,
man and find, you might issue the command
ps –C bash,emacs,man,find
to obtain
information on just those processes. Notice that –C will find processes of all windows and
all users.
Managing Processes
◾
143
Aside from specifying the processes that ps will examine, you can also specify the for-
mat of the output. The ps command with no options provides minimal information. You
might use this to obtain a process’ ID number or to see what is still running as you may
have processes in the background that you have forgotten about. The l (lower case L) option
is the long format. The s option provides signal format which provides information for the
number of signals sent to each process. The option u provides user-oriented format and
the option v provides virtual memory information. The option –f is the
full
format. The
u option provides more detail than –f. The j (or –j)
option provides the
jobs
format which
includes the PID, the PPID (parent’s PID), PGID (process group ID), SID (session ID), and
TPGID (your terminal window’s foreground process group).
You can specify your own formatting options using any of o, –o, or
––format
. In
specifying your own formatting options, you can select the statistics that ps will output.
For instance, you might issue
ps -o pid,start,ppid
to obtain the PID, start time,
and PPID of the processes owned by you in the current terminal window. Note that with o
(-o, ––format) you only receive the specific fields requested.
You can also view the parent-child relationship between processes
by displaying the pro-
cesses in a process tree, or hierarchically. There are two options for this. The older option,
–H, displays children commands (names) indented underneath the parent processes. The
newer option, f, uses an approach called “ascii art,” which uses \_ to indicate the parent–
child relationship between related processes.
Another command,
pstree
, captures the parent–child relationship between processes.
The hierarchical relationship is easier to view with the command pstree although this com-
mand is far less powerful in terms of available options and output statistics than ps.
Before we look at specific instances of ps commands, we need to understand the infor-
mation that ps can provide. Each of the outputs has column headers describing the data of
that column. Table 4.2 summarizes the more useful column headers, which are listed as
abbreviations.
Let us focus on the STAT value. As shown in Table 4.2, the status will appear as one or
more characters describing the status of the process.
The most common values are S, R, or
T. Running (R) and Stopped (T) mean that the process is running or has been stopped by
the user via a control
+
z. Interruptible sleep (S) means that the process has entered a wait
state, waiting for an event. The event might be input from the user or disk file or data to be
provided to it from another process through what is known as interprocess communication.
The process may also be in a wait mode for a prespecified amount of time. Uninterruptible
sleep (D) is often a process invoked via a system call to the kernel. The process is waiting
(asleep) as with (S) but in this case, it cannot be resumed until a specific event occurs.
Figure 4.5 illustrates several different views of the ps command. Each listing is truncated
to just a few processes as the important point is to see the headers (and
in the case of the
hierarchical listing, how processes are arranged). The first portion demonstrates the options
aux
, showing several users (root, Student, postfix) from different terminal windows. The
second portion demonstrates the options
aef
to display Ascii art illustrating parent–child
relationships. The third demonstrates the “long” listing format, through
ps al
.
144
◾
Linux with Operating System Concepts
FIGURE 4.5 Output from various combinations of ps options.
TABLE 4.2
Headers for Various ps Options
Do'stlaringiz bilan baham: