Preemptive Scheduling Vivek Pai / Kai Li Princeton University Overview for Today Wrap up regular scheduling Move on to pre-emptive scheduling Discuss scheduling project Reading assignments missing Will be added to web page soon 2 Rethinking Your Server Server: Accept connection User: Send request Server: Send (possibly long) response User: Receive response Both: Disconnect 3 Drawbacks… Server: Accept connection User: Send request Server: Send (possibly long) response No control over time User: Receive response Both: Disconnect 4 What’s the Simplest Option? While (1) accept new connection fork new process let process handle connection Drawback: lots of process creation/deletion 5 Can We Reduce Forks? At program launch, fork some number While (1) accept wait for hello wait for goodbye close Note: wait = implicit yield 6 But What If We Do More While (1) accept wait for hello perform possibly unbounded calculation wait for goodbye close Problem: when do we yield? 7 Signals and Interrupts Both are asynchronous Used to notify system of event occurrence Signal – delivered by OS to process Interrupt – delivered by hardware to OS Some overlap/interaction? Definitely Examples? Code tries executing divide-by-zero User disconnects (hangs up) 8 I/O and Timer Interrupts Why Timer interrupt to do CPU management Asynchronous I/O to Memory overlap with computation CPU Interrupt Interrupt Between instructions Within an instruction Enable and disable 9 Using Interrupts For Scheduling Timer interrupt Generated by hardware Assume changing requires privilege Delivered to the OS Main idea Before moving process to running, set timer If process yields/blocks, clear timer Timer expires? Go to scheduler 10 Scheduling Considerations Timer granularity Finer timers = more responsive Coarse timers = more efficient Accounting Cheap Accurate Fair – consider I/O versus CPU applications 11 Preemptive Scheduling Terminate (call scheduler) Scheduler dispatch Running Block for resource (call scheduler) Yield, Timer Interrupt (call scheduler) Create Ready Blocked I/O completion interrupt (move to ready queue) 12 No Control Over Yielding Reasons for yielding Timer goes off Higher-priority interrupt occurs Higher-priority process becomes ready Some unintentional block (e.g. page fault) 13 “Atomic” Pieces of Code Example: bank transaction Read account balance Add/subtract money Write account balance Problem: what happens when two transactions are being posted to the same account? 14 Next Time Atomic pieces known as critical sections Very common in concurrent/parallel programming Must share memory Possible via forked processes Default via threads Cover some scheduling policies 15