Presentation is loading. Please wait.

Presentation is loading. Please wait.

More examples How many processes does this piece of code create?

Similar presentations


Presentation on theme: "More examples How many processes does this piece of code create?"— Presentation transcript:

1 More examples How many processes does this piece of code create?
int main() { fork(); /*(Line 1)*/ fork(); /*(Line 2)*/ }

2 What is the output of this?
int main() { int i; for (i=0; i<2; i++) { fork(); printf(“%d\n”,i); } return (0);

3 Lecture 6: Multiprogramming and Context Switching

4 Review: UNIX process memory layout
Stack Heap Static data Text (program) Address space or core image

5 Review: Stack Frame arguments return address stack frame pointer
Heap Static data Text (program) arguments return address stack frame pointer local variables To previous stack frame pointer To the point at which this function was called

6 Review: Creating a process via fork()
Stack Heap Static data Text (program) fork() //return p Stack Heap Static data Text (program) fork() //return 0 The only way to create a new process is to use the fork system call. PC PC parent process child process process id = p

7 Review: Loading a new image via exec()
Stack Heap Static data Text (program) exec(prog, args) prog’s stack prog’s heap prog’s static data prog’s Text before after

8 In this lecture Multiprogramming Process context switch

9 Why Multiprogramming? Imagine not having it Multiprogramming:
Type in command in a shell Wait for result Can’t browse in the meanwhile! Multiprogramming: Many processes executing in parallel

10 Multiple Processes = Multiple Address Spaces
User Kernel In this slide we view things from the point of view of the operating system, which has to deal with multiple address spaces, each belonging to a separate process. We normally think of a user process as executing user code. But when a process invokes kernel functions (by executing system calls), it switches into privileged mode and executes kernel code. Besides having its own stack in user mode, each process has a stack in the kernel for use when executing kernel code. There is also a common heap shared by all processes in the kernel, as well as the equivalents of data, BSS, and text. Copyright © 2002 Thomas W. Doeppner. All rights reserved.

11 Pseudoparallelism Multi-processor CPU Single processor CPU
Real parallelism Single processor CPU Pseudoparallelism

12 The Process Model Multiprogramming 4 processes
Conceptual model of 4 independent, sequential processes Only one program active at any instant

13 Implementation of Multiprogramming
Context Switch Resources (CPU) taken away from one process and given to another Save the context of previous process and restore context of new process

14 Process Table Where is process table Process context
Registers File management Others OS stores the context of a process in process table Each process has an entry in the process table Where is process table

15 When to Switch Context? When a process has reached its quota of time
When a process waits for I/O When a previously started I/O has completed

16 Interrupt Driven Context Switch
Interrupt occurs (timer or I/O) Each interrupt has its own service procedure (address given by interrupt vector) Save some context and then jump to interrupt service procedure Scheduler might context switch after the interrupt is serviced

17 Interrupt Implementation
Skeleton of what lowest level of OS does when an interrupt occurs

18 Process States Possible process states running blocked ready

19 Summary Multiprogramming Context switching Process table
Each entry stores context of a process

20 Will the following get speeded up with multiprogramming?
Four compilations running in parallel A compilation and a text editor


Download ppt "More examples How many processes does this piece of code create?"

Similar presentations


Ads by Google