01-intro-fork
01-intro-fork
1
What’s this course all about?
Operating systems provide a set of constructs and
well-defined primitives to simplify application development
You will learn how to write applications that exploit
important OS features
Often we will present an OS concept first and then
2
Main topics we’ll cover
Concurrent programming
processes, signals, pipes, threads, synchronization
Network programming
sockets and servers
3
System calls
A request for the OS to do something on
behalf of the user’s program
Example
4
Main categories of system calls
File system
Low-level file I/O
E.g., creat, open, read, write, lseek, close
Multi-tasking mechanisms
Process control
E.g., fork, wait, exec, exit, signal, kill
Inter-process communication
E.g., pipe, dup, dup2
5
Classroom organization
Standard lecture format with exercises
sprinkled in for class discussion
We’ll also demo code examples in class
directions)
6
Classroom Etiquette
Come on time to both class and labs
Talking, cell phones, etc. will not be
tolerated
7
Reference books
Keith Haviland, Dina Gray and Ben Salama
“UNIX System Programming”, Addison-Wesley
Randal Bryant and David O’Hallaron
“Computer Systems: A Programmer’s Perspective”, Pearson India
Brian Kernighan and Dennis Ritchie
“The C Programming Language, Second Edition”, Prentice Hall India
8
Grade breakdown
Exams (70%): two insem exams and final
two insems weighted 20%
final weighted 50% (covers entire course)
Lab exercises (checkoffs) and homework (30%)
weighted 20%, 10%
lab attendance is mandatory
9
Processes
A process is the basic unit of execution in an
OS
A process is a running instance of a program
10
Context switching
Transferring control from the current
process to another process is called a
context switch
OS saves the state of the current process, restores the state of
the other process, and passes control to the new process
From a human observer’s point of view,
many processes appear to proceed
simultaneously
11
fork()
fork creates a new process
the process created (child) runs the same
12
Ppare
intntmain(){
fork();
foo();
}
OS
13
Ppare
intntmain(){
fork();
foo();
}
OS
14
Ppare Pchild
intntmain(){ int main(){
fork(); fork();
foo(); foo();
} }
OS creates
15
Ppare Pchild
intntmain(){ int main(){
fork(); fork();
foo(); foo();
} }
OS
16
fork(), when called, returns twice
(to each process @ the next instruction)
int main() {
fork();
printf(“Hello world!\n”);
}
Hello world!
Hello world!
17
int main() {
fork();
fork();
printf(“Hello world!\n”);
}
Hello world!
Hello world!
Hello world!
Hello world!
18
int main() {
fork();
fork();
fork();
printf(“Hello world!\n”);
}
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
19
return value of fork()
typedef int pid_t;
pid_t fork();
20
void fork0() {
if (fork()==0)
printf(“Hello from Child!\n”);
else
printf(“Hello from Parent!\n”);
}
main(){ fork0(); }
21
void fork3() {
int i;
sum = 0;
fork(); /* create a new process */
for (i=1 ; i<=5 ; i++) {
printf ("The value of i is %d\n", i);
fflush(stdout);
sum += i;
}
printf ("The sum is %d\n", sum);
exit(0)
}
main(){ fork3(); }
22
order of execution is non-deterministic
parent and child run concurrently
23
void fork1() {
int x = 1;
if (fork()==0){
printf(“Child has x = %d\n”, ++x);
} else {
printf(“Parent has x = %d\n”, --x);
}
}
Parent has x = 0
Child has x = 2
24
Summary
process != program
Process: dynamic execution context of an executing
program
Several processes may run the same program code,
http://stackoverflow.com/questions/985051/what-is-
the-purpose-of-fork
25
Humor
26
fork practice problems
27
void fork2() {
printf(“L0\n”);
fork();
printf(“L1\n”);
fork();
printf(“Bye\n”);
}
L0
L1
L1
Bye
Bye
Bye
Bye
28
void fork2() {
printf(“L0\n”);
fork();
printf(“L1\n”);
fork();
printf(“Bye\n”);
}
process graph:
Bye
L1 Bye
Bye
L0 L1 Bye
29
void fork2() {
printf(“L0\n”);
fork();
printf(“L1\n”);
fork();
printf(“Bye\n”);
}
30
void fork3() {
printf(“L0\n”);
fork();
printf(“L1\n”);
fork();
printf(“L2\n”);
fork();
printf(“Bye\n”);
}Which are possible? Bye
L2 Bye
Bye
L1 L2 Bye
Bye
L2 Bye
Bye
L0 L1 L2 Bye
31
void fork4() {
printf(“L0\n”);
if (fork()!= 0) {
printf(“L1\n”);
if (fork()!= 0) {
printf(“L2\n”);
fork();
}
}
printf(“Bye\n”);
}
A. B. C. D. E.
L0 L0 Bye L0 L0
L1 L1 L0 Bye L1
L2 Bye Bye L1 Bye
Bye Bye L1 Bye Bye
Bye L2 Bye L2 Bye
Bye Bye L2 Bye L2
Bye Bye Bye Bye Bye
32
void fork4() {
printf(“L0\n”);
if (fork()!= 0) {
printf(“L1\n”);
if (fork()!= 0) {
printf(“L2\n”);
fork();
}
}
printf(“Bye\n”);
}
Bye
Bye
Bye
L0 L1 L2 Bye
33
void fork5() {
printf(“L0\n”);
if (fork()== 0) {
printf(“L1\n”);
if (fork()== 0) {
printf(“L2\n”);
fork();
}
}
printf(“Bye\n”);
}
Bye
L2 Bye
L1 Bye
L0 Bye
34