Академический Документы
Профессиональный Документы
Культура Документы
Processes
Topics
cs105
Processes
Def: A process is an instance of a running program.
CS 105
Process B
Process C
Time
CS 105
Concurrent Processes
Two processes run concurrently (are concurrent) if their
flows overlap in time.
Otherwise, they are sequential.
Examples:
Sequential: B & C
Process A
Process B
Process C
Time
CS 105
Process B
Process C
Time
CS 105
Context Switching
Processes are managed by a shared chunk of OS code
called the kernel
Process B
code
user code
Time
kernel code
context switch
user code
kernel code
context switch
user code
6
CS 105
0xc0000000
0x40000000
read/write segment
(.data, .bss)
run-time heap
(managed by malloc)
0x08048000
memory
invisible to
user code
read-only segment
(.init, .text, .rodata)
unused
brk
CS 105
Fork is interesting
(and often confusing)
because it is called
once but returns twice
Huh? Run that
by me again!
CS 105
Fork Example #1
Key Points
void fork1()
{
int x = 1; // Parent and child will get private copies!
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);
}
9
CS 105
Fork Example #2
Key Points
void fork2()
{
printf("L0\n");
fork();
printf("L1\n");
fork();
printf("Bye\n");
}
10
L0
L1
Bye
Bye
L1
Bye
Bye
CS 105
Fork Example #3
Key Points
void fork3()
{
printf("L0\n");
fork();
printf("L1\n");
fork();
printf("L2\n");
fork();
printf("Bye\n");
}
L1
L0
11
L1
L2
Bye
Bye
L2
Bye
Bye
L2
Bye
Bye
L2
Bye
Bye
CS 105
Fork Example #4
Key Points
void fork4()
{
printf("L0\n");
if (fork() != 0) {
printf("L1\n");
if (fork() != 0) {
printf("L2\n");
fork();
}
}
printf("Bye\n");
}
12
Bye
Bye
L0
L1
L2
Bye
Bye
CS 105
Fork Example #5
Key Points
void fork5()
{
printf("L0\n");
if (fork() == 0) {
printf("L1\n");
if (fork() == 0) {
printf("L2\n");
fork();
}
}
printf("Bye\n");
}
13
Bye
L2
L1
L0
Bye
Bye
Bye
CS 105
Exits a process
14
CS 105
Zombies
Idea
Called a zombie
Reaping
15
CS 105
Zombie Example
void fork7()
{
if (fork() == 0) {
/* Child */
printf("Terminating Child, PID = %d\n",
getpid());
exit(0);
} else {
printf("Running Parent, PID = %d\n",
getpid());
while (1)
; /* Infinite loop */
}
}
ps shows child
process as defunct
Nonterminating
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());
linux> ./forks 8
exit(0);
Terminating Parent, PID = 6675
}
}
Running Child, PID = 6676
linux> ps
PID TTY
TIME
6585 ttyp9
00:00:00
6676 ttyp9
00:00:06
6677 ttyp9
00:00:00
linux> kill 6676
linux> ps
PID TTY
TIME
6585 ttyp9
00:00:00
6678 ttyp9
00:00:00
17
CMD
tcsh
forks
ps
CMD
tcsh
ps
18
CS 105
if (fork() == 0) {
printf("HC: hello from child\n");
}
else {
printf("HP: hello from parent\n");
wait(&child_status);
printf("CT: child has terminated\n");
}
printf("Bye\n");
exit(0);
}
19
HC Bye
HP
CT Bye
CS 105
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);
20 }
CS 105
}
Waitpid
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);
}
21
CS 105
3565
3564
3563
3562
3566
terminated
terminated
terminated
terminated
terminated
with
with
with
with
with
exit
exit
exit
exit
exit
status
status
status
status
status
103
102
101
100
104
22
3568
3569
3570
3571
3572
terminated
terminated
terminated
terminated
terminated
with
with
with
with
with
exit
exit
exit
exit
exit
status
status
status
status
status
100
101
102
103
104
CS 105
main() {
if (fork() == 0) {
execl("/usr/bin/cp", "cp", "foo", "bar", NULL);
}
wait(NULL);
printf("copy completed\n");
exit(0);
}
23
CS 105
Summarizing
Processes
24
CS 105
Summarizing (cont.)
Spawning Processes
Call to fork
Terminating Processes
Call exit
Reaping Processes
25
CS 105