0% found this document useful (0 votes)
40 views

01-intro-fork

The IT628: Systems Programming course focuses on operating systems and their constructs to aid application development, covering topics like concurrent and network programming. Key concepts include system calls, processes, context switching, and the fork() function, which creates new processes. The course includes lectures, exercises, and a grading system based on exams and lab work.

Uploaded by

viraguber3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

01-intro-fork

The IT628: Systems Programming course focuses on operating systems and their constructs to aid application development, covering topics like concurrent and network programming. Key concepts include system calls, processes, context switching, and the fork() function, which creates new processes. The course includes lectures, exercises, and a grading system based on exams and lab work.

Uploaded by

viraguber3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

IT628: Systems Programming

Course outline, process creation

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

the system calls associated with that concept

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

 fork() /* create a child process */


 exec() /* executing a program */
 Don’t confuse system calls with libc calls
 strcpy()

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

 Questions are ALWAYS encouraged (both

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

 Program = static file (image)


 Process = executing program = program + execution state
 Two processes are said to run concurrently
when instructions of one process are
interleaved with the instructions of the other
process

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

program as the creating (parent) process


 and starts with the same PC,
 the same CPU registers,
 the same open files, etc.

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();

system-wide unique process identifier


child’s pid (> 0) is returned in the parent

sentinel value (0) is returned in the child

20
void fork0() {
if (fork()==0)
printf(“Hello from Child!\n”);
else
printf(“Hello from Parent!\n”);
}

main(){ fork0(); }

Hello from Child!


Hello from Parent!
(or)
Hello from Parent!
Hello from Child!

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

 Important: post fork, parent and child are identical


but separate!
 OS allocates and maintains separate data/state
 control flow can diverge

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,

but each is a distinct process with its own state


 Read

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”);
}

Which are possible?


A. B. C. D. E.
L1 L0 L0 L1 L0
L0 L1 L1 Bye Bye
L1 Bye Bye Bye Bye
Bye Bye Bye L0 L1
Bye L1 Bye L1 L1
Bye Bye L1 Bye Bye
Bye Bye Bye Bye Bye

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

You might also like