Process Scheduling Basic Question: Which process goes next? • Personal Computers – Few processes, interactive, low response time • Batch Systems (Compute Servers) – Many processes, not interactive, throughput is important • Real Time Systems – Guaranteed response times, must meet deadlines Cpr E 308 Spring 2005 Context Switch • One process stops and another one starts • Process Contexts stored in the Kernel Process Table • Significant Overhead for each Context Switch Cpr E 308 Spring 2005 Non Preemptive Scheduling • Context Switch happens only when the process gives up the CPU (wait for I/O, process completes, etc) – Example: DOS • Bad: A user process can cause the system to hang • Good: Fewer context switches Cpr E 308 Spring 2005 Preemptive Scheduling • CPU forcibly taken away from process and given to a new process • Preemptive Scheduling used on current PC operating systems (Windows, Linux) • Enabled by interrupts Cpr E 308 Spring 2005 Goals of Batch Scheduling • • • • Maximize Throughput (jobs per minute) Fast Response Times Fairness to Processes Often the goals conflict with each other… Cpr E 308 Spring 2005 First Come First Serve (FCFS) (non preemptive) • Rule: Service processes (jobs) according to order of arrival • Implementation 1. Maintain queue of current processes 2. Schedule first in queue; Scheduled job executes until it finishes or blocks for I/O 3. A new job is added to the end of the queue • Advantages: Simple, easy to implement • Problems – – – Example: a short job arriving soon after a long one What about simultaneous, or near simultaneous arrivals? Doesn’t optimize throughput or the response time Cpr E 308 Spring 2005 Shortest Job First (non-preemptive) • Suppose 3 jobs arrive 8 2 1 • Shortest Job First: 3 11 Sum of response times = 15 1 • Scheduler X: 8 Cpr E 308 Spring 2005 10 11 Sum of response times = 29 Shortest Remaining Time Next (preemptive) • Shortest job First is fine if all jobs arrive simultaneously • If not, use shortest remaining time next – Strategy: Next schedule the job with the shortest time to completion • Starvation: A long job might be continually pre-empted by many short jobs Cpr E 308 Spring 2005 Realistically . . . • Don’t know the length of jobs • Jobs don’t run to completion – run till they block for user input • How long is that? – predict using past behavior – Guess next job’s running time = average running time of all jobs so far from the user Cpr E 308 Spring 2005 Copyright © 2002 Thomas W.Doeppner. All rights reserved Mean Guessing Ti guess(i) i Cpr E 308 Spring 2005 Copyright © 2002 Thomas W.Doeppner. All rights reserved Exponential Averaging • guessi = a(new_data) + (1-a)(guessi-1) – a: weight given to new data – (1-a): weight given to previous guess Cpr E 308 Spring 2005 Copyright © 2002 Thomas W.Doeppner. All rights reserved Better Guessing Ti guess(i) i Cpr E 308 Spring 2005 Copyright © 2002 Thomas W.Doeppner. All rights reserved Round Robin (Preemptive) • • • • Give CPU time to each process by turn Circular queue of ready processes Quantum = CPU time allotted every turn How to choose the value of the quantum? o Too small might mean high context switch overhead o Too large might mean bad response times o Typically a few 10s of milliseconds o If quantum is very high, then similar to FCFS Cpr E 308 Spring 2005 Round Robin Example • Three processes – P1 = 24ms – P2 = 6ms – P3 = 3ms • Quantum = 4ms • What is the schedule? Cpr E 308 Spring 2005 Round Robin • Better than shortest remaining time next – Long job will not be overwhelmed by short jobs • Better than first come first serve – Long job arriving early will not overwhelm future short jobs Cpr E 308 Spring 2005 Priority Scheduling • Different Priorities – Background process sending mail versus – Shell process accepting input • “nice” command in UNIX – allows user to reduce the priority of a process Cpr E 308 Spring 2005 Priority Scheduling Goals • Schedule jobs with highest priority first • High priority jobs should not overwhelm the others – Might decrease priority with time Cpr E 308 Spring 2005 Multiple Queues – Static Version System level processes High Interactive processes Batch processes Low • Lower priority queues don’t run if higher priority queues non-empty • Could time slice between queues Cpr E 308 Spring 2005 Multiple Queues – Dynamic Version Quantum = 1 High Quantum = 2 Quantum = 4 Low • Process enters high priority level • If takes more than 1 quantum, moved to second highest priority, ….. Cpr E 308 Spring 2005 Multiple Queues – Dynamic Version • Higher priority = faster service, but short service times • Reduce number of context switches for long processes • What about a process which became interactive after a long bout of computation? – Increase priority? Cpr E 308 Spring 2005 Real-Time Scheduling • Provide time guarantees • Upper bound on response times – Programmer’s job! – Every level of the system • Soft versus hard real-time – Streaming mp3 player versus air-traffic controller Cpr E 308 Spring 2005 Real-Time Scheduling • Is this set of processes schedulable? – A: once every 100ms, 20ms CPU time – B: once every 200ms, 80ms CPU time – C: once every 100ms, 50ms CPU time • If schedulable, still have to draw up a schedule for the processes Cpr E 308 Spring 2005 CPU Scheduling: Summary • FCFS • Shortest job first, shortest remaining job next • Round robin – context switch overhead • Priority scheduling – Multi-level queues • Real-time Scheduling – Schedulability Cpr E 308 Spring 2005