Fork
Fork
of Washington
int fork(void)
creates
a
new
process
(child
process)
that
is
iden2cal
University of Washington
Understanding
fork
Process
n
pid_t pid = fork();
if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}
University of Washington
Understanding
fork
Process
n
pid_t pid = fork();
if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}
Child
Process
m
pid_t pid = fork();
if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}
pid = m
University of Washington
Understanding
fork
Process
n
pid_t pid = fork();
if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}
Child
Process
m
pid_t pid = fork();
if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}
University of Washington
Understanding
fork
Process
n
Child Process m
University of Washington
Understanding
fork
Process
n
Child Process m
University of Washington
Fork Example #1
void fork1()
{
int x = 1;
pid_t pid = fork();
if (pid == 0) {
printf("Child has x = %d\n", ++x);
} else {
printf("Parent has x = %d\n", --x);
}
printf("Bye from process %d with x = %d\n", getpid(), x);
}
7
University of Washington
Fork Example #2
University of Washington
Fork Example #2
L0
L1
Bye
Bye
L1
Bye
Bye
University of Washington
Fork Example #3
10
University of Washington
Fork Example #3
L2
L1
L0
L1
Bye
Bye
L2
Bye
Bye
Bye
Bye
L2
Bye
Bye
L2
11
University of Washington
Fork Example #4
12
University of Washington
Fork Example #4
Bye
Bye
L0
L1
L2
Bye
Bye
13
University of Washington
Fork Example #4
14
University of Washington
Fork Example #4
Bye
L2
L1
L0
Bye
Bye
Bye
15
University of Washington
void exit(
status)
exits
a
process
Normally
16
University of Washington
Zombies
Idea
When
process
terminates,
s2ll
consumes
system
resources
Various
tables
maintained
by
OS
Called
a
zombie
That
is,
a
living
corpse,
half
alive
and
half
dead
Reaping
Performed
by
parent
on
terminated
child
(horror
movie!)
Parent
is
given
exit
status
informa2on
Kernel
discards
process
17
University of Washington
Zombie
Example
void fork7()
{
if (fork() == 0) {
/* Child */
printf("Terminating Child, PID = %d\n",
getpid());
exit(0);
} else {
printf("Running Parent, PID = %d\n",
linux> ./forks 7 &
getpid());
while (1)
[1] 6639
; /* Infinite loop */
Running Parent, PID = 6639
}
Terminating Child, PID = 6640
}
linux> ps
PID TTY
TIME
6585 ttyp9
00:00:00
6639 ttyp9
00:00:03
6640 ttyp9
00:00:00
6641 ttyp9
00:00:00
linux> kill 6639
[1]
Terminated
linux> ps
PID TTY
TIME
6585 ttyp9
00:00:00
6642 ttyp9
00:00:00
CMD
tcsh
forks
forks <defunct>
ps
CMD
tcsh
ps
18
University of Washington
Non-termina3ng
Child
Example
void fork8()
{
if (fork() == 0) {
/* Child */
printf("Running Child, PID = %d\n",
getpid());
while (1)
; /* Infinite loop */
} else {
printf("Terminating Parent, PID = %d\n",
getpid());
exit(0);
}
}
linux> ./forks 8
Terminating Parent, PID = 6675
Running Child, PID = 6676
linux> ps
PID TTY
TIME CMD
6585 ttyp9
00:00:00 tcsh
6676 ttyp9
00:00:06 forks
6677 ttyp9
00:00:00 ps
linux> kill 6676
linux> ps
PID TTY
TIME CMD
6585 ttyp9
00:00:00 tcsh
6678 ttyp9
00:00:00 ps
19
University of Washington
Synchroniza3on!
20
University of Washington
21
University of Washington
HC Bye
HP
CT Bye
22
University of Washington
wait() Example
void fork10()
{
pid_t pid[N];
int i;
int child_status;
for (i = 0; i < N; i++)
if ((pid[i] = fork()) == 0)
exit(100+i); /* Child */
for (i = 0; i < N; i++) {
pid_t wpid = wait(&child_status);
if (WIFEXITED(child_status))
printf("Child %d terminated with exit status %d\n",
wpid, WEXITSTATUS(child_status));
else
printf("Child %d terminated abnormally\n", wpid);
}
}
23
University of Washington
void fork11()
{
pid_t pid[N];
int i;
int child_status;
for (i = 0; i < N; i++)
if ((pid[i] = fork()) == 0)
exit(100+i); /* Child */
for (i = 0; i < N; i++) {
pid_t wpid = waitpid(pid[i], &child_status, 0);
if (WIFEXITED(child_status))
printf("Child %d terminated with exit status %d\n",
wpid, WEXITSTATUS(child_status));
else
printf("Child %d terminated abnormally\n", wpid);
}
24
University of Washington
int execve(
char *filename,
char *argv[],
char *envp
)
0xbfffffff
Null-terminated
environment
variable
strings
Null-terminated
commandline
arg
strings
unused
envp[n]
=
NULL
envp[n-1]
envp[0]
argv[argc]
=
NULL
argv[argc-1]
argv[0]
Linker
vars
envp
argv
argc
25
University of Washington
execve: Example
envp[n]
=
NULL
envp[n-1]
envp[0]
PWD=/homes/iws/luisceze
PRINTER=ps581
USER=luisceze
argv[argc]
=
NULL
argv[argc-1]
argv[0]
/usr/include
-l
ls
26
University of Washington
Summary
Excep3ons
Events
that
require
non-standard
control
ow
Generated
externally
(interrupts)
or
internally
(traps
and
faults)
Processes
At
any
given
2me,
system
has
mul2ple
ac2ve
processes
Only
one
can
execute
at
a
2me,
however,
Each
process
appears
to
have
total
control
of
the
processor
+
has
a
private
memory
space
27
University of Washington
Summary (contd)
Spawning
processes
Call
to
fork
One
call,
two
returns
Process
comple3on
Call
exit
One
call,
no
return
28