More examples How many processes does this piece of code create? int main() { fork(); /*(Line 1)*/ fork(); /*(Line 2)*/ }
What is the output of this? int main() { int i; for (i=0; i<2; i++) { fork(); printf(“%d\n”,i); } return (0);
Lecture 6: Multiprogramming and Context Switching
Review: UNIX process memory layout Stack Heap Static data Text (program) Address space or core image
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
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
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
In this lecture Multiprogramming Process context switch
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
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.
Pseudoparallelism Multi-processor CPU Single processor CPU Real parallelism Single processor CPU Pseudoparallelism
The Process Model Multiprogramming 4 processes Conceptual model of 4 independent, sequential processes Only one program active at any instant
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
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
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
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
Interrupt Implementation Skeleton of what lowest level of OS does when an interrupt occurs
Process States Possible process states running blocked ready
Summary Multiprogramming Context switching Process table Each entry stores context of a process
Will the following get speeded up with multiprogramming? Four compilations running in parallel A compilation and a text editor