Notes:
Administrivia:
What topics do we want to cover for the rest of the semester?
Course Notes:
Process Control
Process is unix-speak for "program." You can see what processes you have
running by using the ps command:
% ps
PID TTY TIME CMD
18061 pts/3 0:00 csh
18264 pts/3 0:00 tcsh
or
% ps -f
UID PID PPID C STIME TTY TIME CMD
jones 18061 18058 0 Apr 06 pts/3 0:00 -csh
jones 18264 18061 0 Apr 06 pts/3 0:00 tcsh
The "-f" flag gives more information (a "full" listing).
So what do these columns mean?
UID is the user that owns the process.
PID is the process ID of the process.
PPID is the PID of the process that spawned this process (the Parent PID).
TTY is the terminal on which the process is running.
CMD is the name of the process.
From the listing above, you can see that I have two processes, and
that the tcsh process was spawned from the csh process (the tcsh PPID
is the same as the csh PID).
This relationship is better illustrated by the pstree command:
% pstree jones
-csh---tcsh---vim
-csh---tcsh---perl---ps
Here you see that I have one window open editing this file with vim,
and another window running pstree, which is a perl script that
calls ps.
The basics - process control from the command prompt
Let's create a dummy process to manipulate. We'll create an empty
file, and create a process to watch for any lines added to the
end of that file:
% touch jib
% tail -f jib &
[1] + Suspended (tty output) tail -f jib
[1] 4739
What happened? Remember that the & character added to the end of
a line puts the command into the background.
The [1] above tells you that "tail -f jib" is job #1, while the [1] 4739
tells you that the PID of that process is 4739. We can confirm this with
ps:
% ps
PID TTY TIME CMD
18061 pts/3 0:00 csh
18264 pts/3 0:00 tcsh
4739 pts/3 0:00 tail
We can get the same result by starting the command without the "&",
and then sending the process the suspend signal by hitting ctrl-z:
(stuff the user types below is in bold)
% tail -f jib
^Z
Suspended
At this point, the process is suspended, and you are at a command prompt.
You can now do other stuff until you are ready to resume the process,
using fg to bring the process back to the foreground:
If this is a command that can run in the background (is not interactive
with the user) you can let the command run in the background using bg.
You can bring the command to the foreground at any time using fg.
At some point, you will want to end the command. You can send the interrupt
signal to the command while the command is in the foreground with ctrl-C:
At this point, the "tail -f jib" process is over.
more advanced: kill
The kill command can be used to control processes without bringing
them to the foreground. If you are the superuser, the kill command can
be used to terminate other user's commands, or to send signals to system
daemons, which the daemons interpret as input. A common example of this
is causing the web-server to reload it's configuration, after it has been
changed, by sending it the HANGUP signal: kill -HUP httpdPID (where
httpdPID is the PID of your webserver)
From a user's perspective, you can
% ps
PID TTY TIME CMD
5149 pts/25 0:00 csh
% ps -u jones
PID TTY TIME CMD
18061 pts/3 0:00 csh
4946 pts/36 0:01 vim
5149 pts/25 0:00 csh
4844 pts/3 0:00 tail
10955 pts/36 0:00 csh
18264 pts/3 0:01 tcsh
11081 pts/36 0:01 tcsh
If we call ps with no arguements, it reports on the processes
associated with the local terminal. To see all of my processes on the
machine, I call ps -u jones.
Say I went home or my computer froze and I wanted to stop that tail job.
I couldn't use job control as described above because I don't have access
to terminal 3. I would have to use the kill command:
% kill 4844
[2] Terminated tail -f jib
Some commands don't repond to the terminate signal (aka SIGTERM, aka
signal -15). You can send other signals to the program, for a full list,
read the kill manpage. The most popular signals are:
Signal Name Kill# nemonic catchable?
----------- ----- ------- ----------
hangup -1 HUP catchable
interrupt -2 INT catchable
quit -3 QUIT catchable
kill -9 KILL not catchable
terminate -15 TERM catchable
Catchable signals are signals that a program can "catch" and interpret
as input. It's up to the program's designer to cause the program to
behave in a predictable manner for a given signal (i.e. to cause the
program to exit when it gets a QUIT signal).
If a program doesn't respond to plain-vanila kill, or kill -3, you
can use kill -9 (or kill -KILL):
% ps
PID TTY TIME CMD
5493 pts/3 0:00 tail
18061 pts/3 0:00 csh
18264 pts/3 0:01 tcsh
% kill -3 5493
% ps
PID TTY TIME CMD
5493 pts/3 0:00 tail
18061 pts/3 0:00 csh
18264 pts/3 0:01 tcsh
% kill -9 5493
[1] Killed tail -f blah
% ps
PID TTY TIME CMD
18061 pts/3 0:00 csh
18264 pts/3 0:01 tcsh
kill -9 is viewed as a last resort, because it terminates the
program without allowing itself to clean up it's memory close any files
or do whatever else it is supposed to before exiting. Some programs
use the signals other than -9 as alerts that they are about to be killed,
and clean up, but don't terminate when those signals are recieved, so
those signals should be tried first.
More advanced stuff: process execution priority with nice
nice [-19 -> 19] cmd args (default is 10)
renice #
"be nice, run slower (less frequently)"
what's eating up my cpu time?
top
c.2002, Devin Jones - jones@csua.berkeley.edu
last modified:
|