This chapter explains the operating system processes. After completing this chapter, you will be able to:
A good way to learn about the preceding topics is to try the examples in this chapter. You should do each example so that the information on your screen is consistent with the information in this book.
A program is a set of instructions that a computer can interpret and run. You may think of most programs as belonging to one of two categories:
While a program is running, it is called a process. The operating system assigns every process a unique number known as a process identifier.
The operating system can run a number of different processes at the same time. When more than one process is running, a scheduler built into the operating system gives each process its fair share of the computer's time, based on established priorities.
When a process begins executing, the operating system opens three
files for the process:
stdin
(standard input),
stdout
(standard output),
and
stderr
(standard error). Programs use these files as follows:
stdin
from the keyboard.
stdout
to the screen.
stderr
to the screen.
In most cases, the default standard input, output, and error mechanisms will serve you well. However, there are times when it is useful to redirect the standard input, output, and error. The following sections describe these procedures.
A command usually reads its input from the keyboard (standard input) and writes its output to the display (standard output). You may want a command to read its input from a file, write its output to a file, or both. You can select input and output files for a command with the shell notation shown in Table 6-1. This notation can be used in all shells.
Notation | Action | Example |
< | Reads standard input from a file |
wc <file3
|
> | Writes standard output to a file |
ls >file3
|
>> | Appends (adds) standard output to the end of a file |
ls >>file3
|
The following sections explain how to read input from a file and how to write output to a file.
All shells allow you to redirect the standard input of a process so that input is read from a file, instead of from the keyboard.
You can use input
redirection with any command that accepts input from
stdin
(your keyboard). You cannot use input
redirection with commands, such as
who
,
that do not accept input.
To redirect input, use the
left-angle bracket (<),
as the following example shows:
$
wc <file3
3 27 129 $
The
wc
(word count) command counts the number of
lines, words, and bytes in the named file. So
file3
contains
3
lines,
27
words, and
129
bytes.
If you do not supply
an argument, the
wc
command reads its input from the keyboard.
In this example, however, input for
wc
comes from the file named
file3
.
Note that in the preceding example, you could have entered the following, and displayed the same output:
wc file3
This is because most commands allow the input file to be specified without the left-angle bracket (<).
However, there are a few commands like
mail
that require the use of the left-angle bracket (<)
for special functions.
For example, note the following command:
$
mail juan <report
This command mails to the user
juan
the file
report
.
For more information about mail, see
the
mail
(1)
reference page.
All shells allow you to redirect the standard output of a process from the screen (the default) to a file. As a result, you can store the text generated by a command into a new or existing file.
To send output to a file, use either a right-angle bracket (>) or two right-angle brackets (>>).
The right-angle bracket (>) causes the shell to:
Two right-angle brackets (>>) add (append) the output of the command to the end of a file that exists. If you use two right-angle brackets (>>) to write output to a file that does not exist, the shell creates the file containing the output of the command.
In the next example, the output of
ls
goes to the file named
file
:
$
ls >file
$
If the file already exists, the shell replaces its contents
with the output of
ls
.
If
file
does not exist,
the shell creates it.
In the following example, the shell adds the output of
ls
to the
end of the file named
file
:
$
ls >>file
$
If
file
does not exist, the shell creates it.
In addition to their standard output, processes often produce error or status messages known as diagnostic output. For information about redirecting diagnostic output, see the following section.
When a command executes successfully, it displays the results on the standard output. When a command executes unsuccessfully, it displays error messages on the default standard error file, the screen. However, the shell allows you to redirect the standard error of a process from the screen to a file.
Redirection symbols and syntax vary among shells. The following sections describe standard error redirection for the Bourne, Korn, and C shells.
The general format for Bourne and Korn shell standard error redirection is the following:
command
2>
errorfile
The
command
entry is an operating system command. The
errorfile
entry
is the name of the file to which the process writes the standard error.
The
2>
is a file
descriptor digit combined with the right-angle bracket
(>), the output redirection symbol. The
file descriptor digit tells the shell what standard file to access
so that its contents may be redirected. The file descriptor digit
2
indicates that the standard error file is being redirected.
In fact, for the Bourne and Korn shells, a file descriptor digit is associated with each of the files a command ordinarily uses:
In the following example, an error is redirected to the
error
file when the
ls
command attempts to display the nonexistent file,
reportx
.
The contents of
error
file are then displayed:
$
ls reportx 2> error
$
cat error
reportx not found $
Although only standard error is redirected to a file in the preceding example, typically you would redirect both standard error and standard output. See Section 6.2.3 for more information.
For many commands, the difference between standard output
and standard error is difficult to see. For instance, if you use the
ls
command to display a nonexistent file, an error message displays on
the screen. If you redirect the error message to a file as in the
previous example, the output is identical.
The general format for C shell standard error redirection is the following:
(
command
>
outfile
)>&
errorfile
The
command
entry is any operating system command. The
outfile
entry
is the name of the file to which the process writes the standard output.
The
right-angle bracket (>)
redirects the standard error to a file. The
errorfile
entry
is the name of the file to which the process writes the standard error.
Note that in this command format, the parentheses are mandatory.
In the preceding sections, you learned how to redirect standard output and standard error separately. Usually, however, you would redirect both standard output and standard error at the same time. Standard output and standard error can be written to different files or to the same file.
For the Bourne and Korn shells, the general format for redirecting both standard output and standard error to different files is the following:
command
>
outfile
2>
errorfile
The
command
entry is an operating system command. The
outfile
entry is
the file to which the process writes the standard output. The
2>
symbol redirects the error output. The
errorfile
entry is the file where
the process writes the standard error.
For the C shell, the general format for redirecting both standard
output and standard error to different files is the following:
(
command
>
outfile
)>&
errorfile
The
command
entry is an operating system command. The
outfile
entry is the file to which the process writes the standard output.
The right-angle bracket and ampersand (>&)
symbol redirects the error output. The
errorfile
entry is the file where the process writes the standard error.
Note that in this command format, the parentheses are mandatory.
See
Section 6.2.2.2
for more information.
For the Bourne and Korn shells, the general format for redirecting both standard output and standard error to the same file is the following:
command
1>
outfile
2>&1
The
command
entry is an operating system command. The
1>
symbol
redirects the standard output. The
outfile
entry is
the file to which the process writes the standard output. The
2>&1
symbol tells the shell to write the standard error (file
descriptor
2
)
in the file associated with the standard output
(>&1
),
outfile
.
For the C shell, the general format for redirecting both standard output and standard error to the same file is the following:
command
>&
outfile
The
command
entry is an operating system command. The
outfile
entry is
the file to which the process writes the standard output. The
right-angle bracket and ampersand (>&)
symbol tells the shell to write the standard output and standard
error to the same file specified by
outfile
.
The operating system can run a number of different processes at the same time. This capability makes it a multitasking operating system, which means that the processes of several users can run at the same time.
These different processes can be from one or multiple users. As a result, you do not have to enter commands one at a time at the shell prompt. Instead, you can run both foreground and background processes simultaneously. The following sections describe both foreground and background processes.
Normally, when you enter a command on the command line, you wait for the results to display on your screen. Commands entered singly at the shell prompt are called foreground processes.
Most commands take a short time to execute - perhaps a second or two. However, some commands require longer execution times. If a long-duration command runs as a foreground process, you cannot execute other commands until the current one finishes. As a result, you may want to run a long-duration command as a background process. The following section describes background processes.
Generally, background processes are most useful with commands that take a long time to run. Instead of tying up your screen by entering a long-duration command as a foreground process, you can execute a command as a background process. You can then continue with other work in the foreground.
To run a background process, you end the command with an ampersand (&). Once a process is running in the background, you can perform additional tasks by entering other commands at your workstation.
After you create a background process, the following takes place:
When you create a background process, note its PID number. The PID number helps you to monitor or terminate the process. See Section 6.4 for more information.
Because background processes increase the total amount of work the system is doing, they may also slow down the rest of the system. This may or may not be a problem, depending upon how much the system slows and the nature of the other work you or others do while background processes run.
Most processes direct their output to standard output, even when they run in the background. Unless redirected, standard output goes to your workstation. Because the output from a background process may interfere with your other work on the system, it is usually good practice to redirect the output of a background process to a file or to a printer. Then you can look at the output whenever you are ready. For more information about redirecting output, see the examples later in this chapter as well as Section 6.2. The examples in the remainder of this chapter use a command that takes more than a few seconds to run:
$
find / -type f -print
This command displays the pathnames for all files on your system.
You do not need to study the
find
command in order to complete this chapter - it
is used here simply to demonstrate how to work with processes.
However, if you want to learn more about the
find
command, see the
find
(1)
reference page.
In the following example, the
find
command runs in the background
(by entering an ampersand [&]) and redirects its output to a file named
dir.paths
(by using the right-angle bracket [>] operator):
$
find / -type f -print >dir.paths &
24 $
When the background process starts, the system assigns it a PID (Process Identification) number (24 in this example), displays it, and then prompts you for another command. Your process number will be different from the one shown in this and following examples.
If you use the Korn or C shell, job numbers are assigned as well. In the C shell, the preceding example looks like this:
%
find / -type f -print >dir.paths &
[1] 24 % _
Note that the job number
[1]
is displayed to the left of the PID number.
You can check the status of the process with the
ps
(process status) or the
jobs
command (Korn and C shell).
You can also terminate a process with the
kill
command. See the following section for more information about
these commands.
In the C shell, when the background process is completed, a message is displayed:
[1] 24 Done find / -type f -print >dir.paths
The completion message displays the job number and the PID, the status
Done
,
and the command executed.
Use the
ps
(process status) command to find out which processes are
running and to display information about those processes.
In the Korn and C shells, you also can use the
jobs
command to monitor background processes.
If you need to stop a process before it is finished, use the
kill
command.
The following sections describe how to monitor and terminate processes.
The
ps
command allows you to monitor the status of all active processes, both
foreground and background. In the Korn and C shell, you also can use the
jobs
command to monitor background processes only. The following sections
describe the
ps
and the
jobs
command.
The
ps
command has the following form:
ps
In the following example, the
ps
command displays the
status of all processes associated with your workstation under
the following headings:
$
ps
PID TT STAT TIME COMMAND 29670 p4 I 0:00.00 -sh (csh) 515 p5 S 0:00.00 -sh (csh) 28476 p5 R 0:00.00 ps 790 p6 I 0:00.00 -sh (csh) $
You interpret the display under these entry headings as follows:
PID
PID
number) to each process when that process
starts. There is no relationship between a process and a particular
PID
number; that is, if you start the same
process several times, it will have a different
PID
number each time.
TT
console
or the designation for one or more virtual terminals.
STAT
ps
(1)
reference page.
TIME
ps
.
COMMAND
You can also check the status of a particular process by using the
-p
flag and the PID number with the
ps
command.
The general format for checking the status of a particular process is
the following:
ps -p
PIDnumber
The
ps
command also displays the status of background processes. If
there are any
background processes running, they will be displayed along with the
foreground processes. The following example shows how to start a
find
background process and then check its status:
$
find / -type f -print >dir.paths &
25
$
ps -p25
PID TTY TIME COMMAND 25 console 0:40.00 find $
You can check background process status as often as you
like while the process
runs. In the following example, the
ps
command displays the status
of the preceding
find
process five times:
$
ps -p25
PID TTY TIME COMMAND 25 console 0:18:00 find
$
ps -p25
PID TTY TIME COMMAND 25 console 0:29:00 find
$
ps -p25
PID TTY TIME COMMAND 25 console 0:49:00 find
$
ps -p25
PID TTY TIME COMMAND 25 console 0:58:00 find
$
ps -p25
PID TTY TIME COMMAND 25 console 1:02:00 find
$
ps -p25
PID TTY TIME COMMAND $
Notice that the sixth
ps
command returns no status information
because the
find
process ended before the last
ps
command
was entered.
Generally, the simple
ps
command described here tells you
all you need to know about processes. However, you can control the
type of information that the
ps
command displays by using
more of its flags. One of the most useful
ps
flags is
-e
,
which causes
ps
to return information about
all processes,
not
just those associated with your terminal or workstation.
For an explanation of all
ps
command flags, see the
ps
(1)
reference page.
The Korn shell and the C shell display both a job and a
PID when a background process is created. The
jobs
command reports the status of all background processes only,
based upon the job number.
The
jobs
command has the following form:
jobs
Adding the
-l
flag displays both the job number and the PID.
The following example shows how to start a
find
process and
then check its status in the C shell with the
jobs -l
command:
%
find / -type f -print >dir.paths &
[2] 26
%
jobs -l
[2] +26 Running find / -type f -print >dir.paths & % _
The status message displays both the job
([2]
)
and the PID number
(26
),
the status
Running
,
and the command executed.
To cancel a foreground process (stop an executing command), press Ctrl/C. The command stops executing, and the system displays the shell prompt. Note that canceling a foreground process is the same as stopping command execution (described in Chapter 1).
Most simple operating system commands are not good examples
for demonstrating how to cancel
a process - they run so quickly that they finish before you
have time to
cancel them. However, the following
find
command runs long enough for you to cancel it (after the process
runs for a few seconds, you can cancel it
by pressing
Ctrl/C):
$
find / -type f -print
/usr/sbin/acct/acctcms
/usr/sbin/acct/acctcoN1
/usr/sbin/acct/acctcon2
/usr/sbin/acct/acctdisk
/usr/sbin/acct/acctmerg
/usr/sbin/acct/accton
/usr/sbin/acct/acctprc1
/usr/sbin/acct/acctprc2
/usr/sbin/acct/acctwtmp
/usr/sbin/acct/chargefee
/usr/sbin/acct/ckpacct
/usr/sbin/acct/dodisk
[Ctrl/C]
$
The system returns the shell prompt to the screen. Now you can enter another command.
If you decide, after starting a background process, that you do
not want the process to finish, you can cancel the process with the
kill
command.
Before you can cancel a background process, however, you must
know its PID number.
If you have forgotten the PID number of that process, you can use the
ps
command to list the PID numbers of all processes. If you
are a C or Korn Shell user, it is more efficient to use the
jobs
command
to list background processes only.
The general format for terminating a particular process is the following:
kill
PIDnumber
If you want to end all the processes you have started since login,
use the
kill 0
command. You do not have to know the PID numbers to use
kill 0
.
Because this command deletes all of your processes,
use this command carefully.
The following example shows how to start another
find
process, check its status, and then terminate it:
$
find / -type f -print >dir.paths &
38
$
ps
PID TT STAT TIME COMMAND 520 p4 I 0:11:10 sh 38 p5 I 0:10:33 find 1216 p6 S 0:01:14 qdaemon 839 p7 R 0:03:55 ps
$
kill 38
$
ps
38 Terminated PID TT STAT TIME COMMAND 520 p4 I 0:11:35 sh 1216 p6 S 0:01:11 qdaemon 839 p7 R 0:03:27 ps $
The command
kill 38
stops the background
find
process, and
the second
ps
command returns no status information about PID number
38
.
The system does not display the termination message until you enter your
next command.
Note that in this example,
kill 38
and
kill 0
have the same effect
because only one process was started from this terminal or workstation.
In the C shell, the
kill
command has the following format:
kill %
jobnumber
The following example uses the C shell to start another
find
process, to check its status with the
jobs
command, and then to terminate it:
%
find / -type f -print >dir.paths &
[3] 40
%
jobs -l
[3] +40 Running find / -type f -print >dir.paths &
%
kill %3
%
jobs -l
[3] +Terminated find / -type f -print > dir.paths % _
Stopping a foreground process and resuming it can be helpful when you have a long-duration process absorbing system resources and you need to do something quickly.
Rather than waiting for process completion, you can stop the process temporarily (suspend it), perform your more critical task, and then resume the process. Suspending a process is available for C shell users only.
To suspend a process, press Ctrl/Z. A message will display listing the job number, the status suspended, and the command executed.
Once you are ready to resume the process, enter:
%
n
To resume the process in the background, enter:
%
n
&
where the
n
entry is the number of the stopped job.
The following example starts a
find
process, suspends it, checks its status, resumes it, and then terminates it:
%
find / -type f -print >dir.paths &
[4] 41
%
jobs -l
[4] +41 Running find / -type f -print >dir.paths &
%
[Ctrl/Z]
Suspended
%
jobs -l
[4] +Stopped find / -type f -print > dir.paths
%
4 &
[4] find / -type f -print >dir.paths &
%
kill %4
[4] +Terminated find / -type f -print > dir.paths
Once a process is suspended, you may also resume it by entering the
fg
command. If a currently running process is taking too long
to run and is tying up your keyboard, you can use the
bg
command to place the process in the background and enter other commands.
The following example starts a
find
process, suspends it, puts the process in the background, copies a
file, and then resumes the process in the foreground:
%
find / -type f -print >dir.paths
[Ctrl/Z]
Suspended
%
bg
[5] find / -type f -print > dir.paths &
%
cp salary1 salary2
%
fg
find / -type f -print > dir.paths %
The operating system provides the following commands that can tell you who is using the system and what they are doing:
who
w
ps au
The
who
command allows you to determine who is logged into the system. It may
be especially useful, for example, when you want to send a message and
want to know whether the person is currently available.
In the following example, information about all currently logged in users is displayed:
$
who
juan tty01 Jan 15 08:33 chang tty05 Jan 15 08:45 larry tty07 Jan 15 08:55 tony tty09 Jan 15 07:53 lucy pts/2 Jan 15 11:24 (boston) $
Note that the
who
command lists the username of each user on the system, the workstation
being used, and when the person logged in. In addition, if a user is
logged in from a remote system, the name of the system is listed.
For example,
lucy
logged in remotely from
the system
boston
on
Jan 15
at
11:24
.
The actual display format of
who
varies according to your current locale. See
Appendix C
for more information about locales.
.md
The
who -u
command gives all the information of the
who
command and also displays
the PID of
each user and the number of hours and
minutes since there was activity at a workstation. Activity for
less than a minute is indicated by a dot (.).
In the following example, all currently logged in users are displayed:
$
who -u
juan tty01 Jan 15 08:33 01:02 50 chang tty05 Jan 15 08:45 . 52 larry tty07 Jan 15 08:55 . 58 tony tty09 Jan 15 07:53 01:20 60 lucy pts/5 Jan 15 11:24 . 65 (boston) $
Note that in the preceding example,
juan
and
tony
have been
inactive for over an hour, while
chang
,
larry
,
and
lucy
have been inactive for less than a minute.
Now that you know how to find out who is active on your system, you
may want to find out what command each person is currently
executing. The
w
command displays the command that is currently running at each user's
workstation.
In the following example, information about all users (the
User
column) and their
current commands (the
what
column) is displayed:
$
w
11:02 up 3 days, 2:40, 5 users, load average: 0.32, 0.20, 0.00 User tty login@ idle JCPU PCPU what juan tty01 8:33am 12 54 14 -csh chang tty05 8:45am 6:20 26 mail larry tty07 8:55 1:58 8 -csh tony tty09 7:53 3:10 22 4 mail lucy tty02 11:24 1:40 18 4 -csh $
In addition, the
w
command displays the following information:
tty
column: user's workstation
login@
column: user's login time
idle
column: amount of time since the user entered a command
JCPU
column: total CPU time used during the current login session
PCPU
column: CPU time used by the command that is currently executing
On certain occasions, you may want to have a detailed listing of current
processes (both foreground and background) and the users who are
running them. To get such a listing, use the
ps au
command.
In the following example, information about five users
and their active processes
is displayed:
$
ps au
USER PID %CPU %MEM SZ RSS TT STAT TIME COMMAND juan 26300 16.5 0.8 441 327 p3 R 0:02:01 ps au chang 25821 7.0 0.2 149 64 p4 R 0:12:23 mail -n larry 25121 6.1 0.2 107 83 p22 R 26:25:07 tip modem tony 11240 4.5 0.6 741 225 p19 R 1:57:46 emacs lucy 26287 0.5 0.1 61 28 p1 S 0:00:00 more $
The most important fields for the general user are the
USER
,
PID
,
TIME
,
and
COMMAND
fields. For information on the
remaining fields, see the
ps
(1)
reference page.