lecture_slides_08_083-processes-exec
lecture_slides_08_083-processes-exec
of Washington
Fork-‐Exec
University
of
Washington
Fork-‐Exec
¢ fork-‐exec
model:
§ fork()
creates
a
copy
of
the
current
process
§ execve()
replaces
the
current
process’
code
&
address
space
with
the
code
for
a
different
program
§ There
is
a
whole
family
of
exec
calls
–
see
exec(3)
and
execve(2)
exec():
Heap
Heap
Data
Data
Data
Code:
/usr/bin/bash Code:
/usr/bin/bash Code:
/usr/bin/ls
Fork-‐Exec
University
of
Washington
void cleanup(void) {
printf("cleaning up\n");
}
void fork6() {
atexit(cleanup);
fork();
exit(0);
}
Fork-‐Exec
University
of
Washington
Zombies
¢ Idea
§ When
process
terminates,
it
sXll
consumes
system
resources
Various
tables
maintained
by
OS
§
§ Called
a
“zombie”
§ A
living
corpse,
half
alive
and
half
dead
¢ Reaping
§ Performed
by
parent
on
terminated
child
§ Parent
is
given
exit
status
informaXon
§ Kernel
discards
process
¢ What
if
parent
doesn’t
reap?
§ If
any
parent
terminates
without
reaping
a
child,
then
child
will
be
reaped
by
init
process
(pid
==
1)
§ But
in
long-‐running
processes
we
need
explicit
reaping
§ e.g.,
shells
and
servers
Fork-‐Exec
University
of
Washington
Fork-‐Exec
University
of
Washington
wait
Example
void fork_wait() {
int child_status;
pid_t child_pid;
if (fork() == 0) {
HC Bye
printf("HC: hello from child\n");
} else {
child_pid = wait(&child_status); CT Bye
printf("CT: child %d has terminated\n”,
child_pid);
}
printf("Bye\n");
exit(0);
}
Fork-‐Exec
University
of
Washington
Summary
¢ Processes
§ At
any
given
Xme,
system
has
mulXple
acXve
processes
§ Only
one
can
execute
at
a
Xme,
but
each
process
appears
to
have
total
control
of
the
processor
§ OS
periodically
“context
switches”
between
acXve
processes
§ Implemented
using
excep,onal
control
flow
¢ Process
management
§ fork-‐exec
model
Fork-‐Exec