Chapter 9
Process management
Chapter 9 Outline
Process concepts
Process concepts
Processes
Typical process life cycle
Processes
Provide system sevices e.g. file servers, print services, web servers
Known as daemons
At any one time, a linux system is likely to have between 50 and 500
processes
Linux performs pre-emptive multitasking between processes allowing
them to share resources (including CPU time) on the computer
Each process has a numeric identifier, called its process ID (PID)
fork() creates a
new process
Child process
PID = 4188
Parent process
PID = 4103
process to terminate
Examining processes
Examining processes
Listing processes with ps
Command line options for ps
Long process listings
Listing system processes
Listing processes with pstree
Showing process activity with top
KDE System Guard
$$ ps
ps
PID
PID TTY
TTY
3540
tty1
3540 tty1
3632
3632 tty1
tty1
$$
The process ID.
These are allocated
sequentially in the
range 0-32767,
then wrap round
to re-use available
values
00:00:00
00:00:00 bash
bash
00:00:00
00:00:00 ps
ps
HH:MM:SS of CPU
time used by
process
The device name
of the associated
terminal
A confusing set of option flags control which processes are shown and
how much detail is shown about each process
x
a
U user
Meaning
List processes that have no controlling terminal
Show processes belonging to other users
Show processes owned by user
l
u
e
f
Meaning
Show long format (more detail)
Show detailed user oriented format
Show the process environment
Show processes as a hierarchical tree
$$ ps
ps ll
FF UID
UID PID
PID
00 500
500 3540
3540
00 500
3553
500 3553
00 500
500 4107
4107
$$
PPID
PPID PRI
PRI NI
NI VSZ
VSZ RSS
RSS WCHAN
WCHAN STAT
STAT TTY
TTY
3539
pts/1
3539 15
15 00 2884
2884 1700
1700 wait4
wait4 SS
pts/1
2186
15
0
2868
1648
schedu
S
tty1
2186 15
0 2868 1648 schedu S
tty1
3540
19
0
3680
1708
R
pts/1
3540 19
0 3680 1708 R
pts/1
Memory
usage
ID of the
user running
the process
NICE value
Priority (lower values
mean higher priority)
ID of the parent
process
ID of the process
TIME
TIME COMMAND
COMMAND
0:00
0:00 /bin/bash
/bin/bash
0:00
-bash
0:00 -bash
0:00
0:00 ps
ps ll
Process status:
R Runnable
S Sleeping
T Suspended
Z Zombie
$$ ps
ps uu
USER
PID
USER
PID %CPU
%CPU %MEM
%MEM VSZ
VSZ RSS
RSS TTY
TTY
chris
3540
0.0
0.3
2884
1700
pts/1
chris 3540 0.0 0.3 2884 1700 pts/1
chris
chris 3553
3553 0.0
0.0 0.3
0.3 2868
2868 1648
1648 tty1
tty1
chris
4172
0.0
0.1
2668
720
pts/1
chris 4172 0.0 0.1 2668 720 pts/1
$$
Percentage of
CPU time this
process is using
Percentage of
memory this
process is using
STAT
STAT START
START
SS
16:07
16:07
SS
16:07
16:07
RR
20:39
20:39
Time (HH:MM)
when process
was started
TIME
TIME COMMAND
COMMAND
0:00
/bin/bash
0:00 /bin/bash
0:00
0:00 -bash
-bash
0:00
ps
0:00 ps uu
$$ ps
ps ax
ax
PID
TTY
PID TTY
11 ??
22 ??
33 ??
44 ??
....
....
724
724 ??
727
727 ??
1081
1081 ??
1115
1115 ??
1278
1278 ??
....
....
8432
8432 ??
8433
8433 tty1
tty1
8453
tty1
8453 tty1
Kernel processes
SS
SS
SS
SS
SS
0:00
0:00 /sbin/syslogd
/sbin/syslogd
0:00
/sbin/klogd
0:00 /sbin/klogd -c
-c 11 -2
-2
0:00
/sbin/portmap
0:00 /sbin/portmap
0:00
0:00 /sbin/rpc.statd
/sbin/rpc.statd
0:00
/usr/sbin/acpid
0:00 /usr/sbin/acpid
Daemons
SS
SS
RR
0:00
0:00 login
login --- tux
tux
0:00
-bash
0:00 -bash
0:00
0:00 ps
ps ax
ax
User processes
killing of processes
An example of top
Discussed in Chapter 10
Hint: use grep to search the output of ps for the process you want
Using the help screen as a guide, sort the top listing on (a) the process
ID and (b) the %CPU utilisation of the processes
+1
-u
$ renice
+2
1712
tux
The 'r' command in top can also be used to adjust nice levels
jobs
^Z
bg
fg
kill
Terminate a job
$$ xclock
Job started in background
xclock -update
-update 11 &&
[1]
Shell reports job number and PID
[1] 3197
3197
$$ xeyes
xeyes &&
[2]
[2] 3198
3198
$$ jobs
jobs
[1]xclock
[1]- Running
Running
xclock -update
-update 11 &&
[2]+
xeyes
[2]+ Running
Running
xeyes &&
'+' indicates the current
$$ fg
fg
job
Bring current job
xeyes
xeyes
into foreground
^Z stops (pauses) the current job
[2]+
[2]+ Stopped
Stopped
$$ kill
kill %1
%1
$$ fg
fg
xeyes
xeyes
[1]
[1]
Terminated
Terminated
xeyes
xeyes
Shell reports
termination of job 1
The program soak (written especially for this exercise), simply soaks
up CPU time in an infinite loop. If started with a command line
argument, it will print that argument, the process ID, and a loop
counter every pass through the loop
$ ./soak A
soak A 4487 0
soak A 4487 1
^C
$
10 seconds later
3. Wait 20 seconds
4. Re-run the jobs command.
When does the shell notify you that a background job is done? ________
6. Kill both background jobs using kill and the job numbers
9. Adjust the nice level of the first soak to match that of the second
(+10)
10.Re-examine the %CPU usage of both processes. Can you see a
change? (It may take a few seconds to see a significant change)
11.Try to adjust the nice level of the first soak back down to zero.
Sending signals
Linux defines over 30 signal types, kill -l will give the full list
^C
^\
-SIGHUP
xinetd
Signal types
Number
1
Signal
Name
Description
Default
Handling
SIGHUP
Kill
process
SIGINT
Kill
process
SIGQUIT
Kill
process
and write
core dump
SIGKILL
Kill
process
15
SIGTERM
Kill
process
18
SIGCONT
Signal handlers
Orphan processes
fork()
exit()
orphan
wait()
exit()
Zombie processes
Occasionally, a parent process will not wait for its children but will
block on some other event
Processes that exit when no process is waiting for them become
'zombies'
Cannot be laid to rest as they want to pass back their exit status
In the worst case, zombies accumulate and fill the process table so that
no more processes can be started
PID
2359
3705
3766
3876
3877
3878
3879
3882
3883
PPID
2358
3704
2358
2359
3876
3876
3876
3876
2359
STAT
S
S
S
S
Z
Z
Z
Z
R
TTY
pts/1
pts/2
pts/3
pts/1
pts/1
pts/1
pts/1
pts/1
pts/1
TIME
0:00
0:00
0:00
0:00
0:00
0:00
0:00
0:00
0:00
COMMAND
/bin/bash
/bin/bash
/bin/bash
./myprog
[myprog] <defunct>
[myprog] <defunct>
[myprog] <defunct>
[myprog] <defunct>
ps l
This program is written specially for this exercise. It creates four child
processes then pauses indefinitely. The child processes immediately exit
and so become zombies
Verify that the parent and the zombies are now all gone
Quiz
In the output from ps l, what does an 'R' in the STAT column mean?
True or false?
A program's priority can be set to a fixed level using the bash shell