Controlling Processes
This contains links to information used in the lectures.
The entity which executes programs is called a process. Resources such
as memory, CPU, and i/o devices are allocated to processes.
In Unix, the SysAdm can monitor the status of processes, control how much
CPU time they get, send signals to them, and suspend or halt them.
Components of a Process
- address space
- memory pages (usually 1Kbytes to 4Kbytes)
- usually virtual memory now - some pages may be on disk, some in memory
- address space contains program code, variables, process stack,
other infomation
- data structures in kernel
These contain information such as:
- process id (PID) and parent process id (PPID)
Unique number assigned by kernel. Assigns the next available PID.
Treats this as a circular system, that is when it reaches maximum number,
starts again at 0.
Unix creates new process by spawing a copy of an existing process using
fork
and then substituting the text of another program using
exec.
- owner and group, real and effective - UID, EUID, GID, EGID
UID and EUID are the same except for setuid programs.
When a process is spawned its GID is set to that of parent.
- priority and nice value
When the kernel schedules processes,
the one with highest internal priority is chosen.
The is calculated from nice value, the CPU time consumed, and
time the process has been ready (waiting to run). The nice value
can be set with nice.
-
controlling terminal
-
process group
- current process state
- process address space map
- resources used
- signal mask
Life Cycle of a Process
Unix creates new process by spawing a copy of an existing process using
fork
and then substituting the text of another program using
exec.
In UNIX, all processes other than some the kernel creates, are children
spawned by init, PID 1.
Init plays another important role. Exiting processes call _exit
and return an exit status. This is stored by the kernel
until requested by the process's parent (using the wait system call).
The address space of the process is released and it uses no further CPU time.
However it still retains its identity (PID). This uses a slot
in the process table but no other resources.
Problems arise if
- parent fails to call wait
- parent dies first - in this case init becomes the parent
of the process and waits on it. (init waits on all orphans.)
Sometimes this does not happen and the processes remain in the
system as zombies. These appear with status Z or
exiting. You cannot kill a zombie - it is already dead !!
Signals
Process States
In Unix systems there are at least 5 execution states, although more
may exist on some systems:
- Runnable - process can be executed (running or ready)
- Sleeping - process is blocked waiting for an event
(terminal input, network connection)
- Zombie - process is trying to finish exiting
- Stopped - process is suspended
- Swapped - process is not in memory (technically not a state)
When runnable processes make system calls (such as requesting i/o),
which cannot be completed immediately, they are put to sleep.
Interactive processes and daemons spend most time sleeping waiting
for input or connections. They are not elegible to be
selected by the CPU scheduler, until unblocked by a signal.
Most sleeps last for tens of milliseconds. A process sleeping longer
is presumed interactive, and get to run immediately it is unblocked.
This biases the scheduling in favor of interactive programs and makes
them more responsive.
Processes enter the stopped state by receiving a STOP or TSTP signal and
can only be restarted by receiving a CONT signal from another
process. Most processes are stopped by one of:
- When the suspend character (usually ^Z) is typed to interactive
processes, shell send a kill -TSTP to the foreground process
- At the request of another program or user (using kill -STOP or
kill -TSTP). STOP cannot be caught.
- When a background process tries to access its controlling terminal,
it is sent a TTIN or TTOU signal which (by default) puts it to sleep.
The state of the process on SunOS is:
R Runnable processes.
T Stopped processes.
P Processes in page wait.
D Processes in non-interruptible waits;
typically short-term waits for disk
or NFS I/O.
S Processes sleeping for less than
about 20 seconds.
I Processes that are idle (sleeping
longer than about 20 seconds).
Z Processes that have terminated and
that are waiting for their parent
process to do a wait(2V) ("zombie"
processes).
and on HP-UX is:
0 Nonexistent
S Sleeping
W Waiting
R Running
I Intermediate
Z Terminated
T Stopped
X Growing
Nice and Renice
The nice command executes command at a nondefault CPU scheduling
priority. On BSD (SunOS) the value can be -19 to 19, and on ATT (HP-UX)
0 to 39. Lower numbers are higher priority.
All processes have an associated system nice value which is used to
compute the instantaneous-priority of the process when it is scheduled
to run. Normally, all processes inherit the system nice value of
their parent process when they are spawned. The shell (sh, csh, ksh,
etc.) can create a child process with a different priority from the
current shell process by spawning the child process via the nice
command.
If the priority_change value is unsigned (positive), the
child process is nicer (lower in priority) relative to the parent for example
nice -12 command &
runs the child process at a priority 12 worse than the default for
background processes.
If the priority_change value is negative, the child process runs at a
higher priority with a greater share of available system resources.
nice --14 ./prog prog_args
To spawn a higher priority child process, the parent process must be
owned by a user who has the appropriate privileges.
Some versions on Unix
nice up background jobs by default. On HP-UX ksh
executes all background processes via nice -4 (the csh does not !!).
BSD systems allow resetting the nice values during execution using
renice.
If a process goes wild and drives the load to 65, you may need to
use nice to run a high priority shell to investigate the problem.
On HP-UX the superuser or other authorized users
can run processes with real-time priority using the
rtprio executes command with a real-time priority, or changes the
real-time priority of currently executing process pid. Real-time
priorities range from zero (highest) to 127 (lowest). Real-time
processes are not subject to priority degradation, and are all of
greater (scheduling) importance than non-real-time processes.
Monitoring Processes
ps
The main tool for
monitoring processes is
ps.
On BSD good overwviews can be had with ps -aux
and ps -axl, which is also faster to run. ps
is generally expensive to run. On ATT systems ps -ef and
ps -elf are the equivalents.
top
top displays the top processes on the system and periodically updates
the information (by default every 10 seconds). Raw CPU percentage
is used to rank the processes.
It comes with HP-UX 10.XX and is also freely available.
Examples:
NOHUP: Protect background processes
Primitive shells such as sh send signals to all descendants,
including background processes, when terminated. To prevent the
background process from exiting use nohup:
nohup command &
This also increases niceness by 5. Output is put in nohup.out
if stdout and stderr are not redirected.
Errant Processes
- Hung processes, zombies
- Runaway processes
- User processes using excessive CPU time or disk space.
- Systems processes that have gone wild
- Handling runaway processes
- Contact owner
- Track down source code of runaway process
- Determine if legitimate
- Stop the process - normally do not kill it, in addition to lost work,
if the process is destructive one wants to determine what it has done.
- Runaway processes can fill disk causing console messages
- Finding it not so easy - no tool - when do stop
- If in doubt suspend all dubious processes
- Be aware of prank programs which try to fill up system
- May not be able to use rm -r to clean up !!
Paul A. Farrell
Thu Feb 1 04:34:17 EST 1996