Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 6: Multiprogramming and Context Switching

Similar presentations


Presentation on theme: "Lecture 6: Multiprogramming and Context Switching"— Presentation transcript:

1 Lecture 6: Multiprogramming and Context Switching

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

3 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

4 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

5 Review: Waiting Parent processes often wait for their child processes to end Parent processes do that via a wait() call pid_t wait(int * status); pid_t waitpid( pid_t pid, int * status,…);

6 Review: A stripped-down shell
while (TRUE) { /* repeat forever */ type_prompt( ); /* display prompt */ read_command (command, parameters) /* input from terminal */ if (fork() != 0) { /* fork off child process */ /* Parent code */ waitpid( -1, &status, 0); /* wait for child to exit */ } else { /* Child code */ execve (command, parameters, 0); /* execute command */ }

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 Review: exec() example:
#include <unistd.h> main() { printf(“executing ls\n”); execl(“/bin/ls”, “ls”, “l”, (char*)0); exit(1); }

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

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

11 In this lecture Multiprogramming Process context switch

12 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

13 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.

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

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

16 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

17 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

18 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

19 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

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

21 Process States Possible process states running blocked ready

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

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


Download ppt "Lecture 6: Multiprogramming and Context Switching"

Similar presentations


Ads by Google