CSCE 313 Introduction to Computer Systems Instructor: Dezhen Song
Schedulers in the OS CPU Scheduling Structure of a CPU Scheduler Scheduling = Selection + Dispatching Criteria for scheduling Scheduling Algorithms FIFO/FCFS SPF / SRTF Priority - Based
Schedulers start long-term (admission) scheduler short-term (CPU) scheduler suspended ready ready running suspended blocked blocked medium-term (memory) scheduler
Focus: Short-Term Scheduling Recall: Motivation for multiprogramming -- have multiple processes in memory to keep CPU busy. Typical execution profile of a process/thread: start terminate wait for I/O wait for I/O wait for I/O CPU burst CPU burst CPU burst CPU burst CPU scheduler is managing the execution of CPU bursts, represented by processes in ready or running state.
non-preemptive preemptive Scheduling Decisions Who is going to use the CPU next?! ready 4 running 2 3 1 waiting Scheduling decision points: 1. The running process changes from running to waiting (current CPU burst of that process is over). 2. The running process terminates. 3. A waiting process becomes ready (new CPU burst of that process begins). 4. The current process switches from running to ready.
Structure of a Scheduler ready queue PCB scheduler dispatcher CPU select process restart executing process?
What Is a Good Scheduler? Criteria User oriented: Turnaround time : time interval from submission of job until its completion Waiting time : sum of periods spent waiting in ready queue Response time : time interval from submission of job to first response Normalized turnaround time: ratio of turnaround time to service time System oriented: CPU utilization : percentage of time CPU is busy Throughput : number of jobs completed per time unit Any good scheduler should: maximize CPU utilization and throughput minimize turnaround time, waiting time, response time Huh? maximum/minimum values vs. average values vs. variance
Scheduling Algorithms FCFS : First-come-first-served SPN/SJF: Shortest Process Next/Shortest Job First SRT: Shortest Remaining Time priority scheduling RR : Round-robin MLFQ: Multilevel feedback queue scheduling Multiprocessor scheduling
First-Come-First-Served (FCFS/FIFO) append at the end of queue head tail PCB CPU Advantages: very simple Disadvantages: long average and worst-case waiting times poor dynamic behavior (convoy effect): many other processes may wait for one big process to get off the CPU
FCFS Scheduling (cont) Process Burst Time P 1 24 P 2 3 P 3 3 Suppose that the processes arrive in the order: P 1, P 2, P 3 The Gantt Chart for the schedule is: 0 P 1 P 2 P 3 Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17 Average response time: (0+24+27)/3=17 Average turnaround time: (24+27+30)/3=27 24 27 30
FCFS Scheduling (cont) Suppose that the processes arrive in the order P 2, P 3, P 1 The Gantt chart for the schedule is: P 2 P 3 P 1 0 Waiting time for P 1 = 6; P 2 = 0 ; P 3 = 3 3 6 30 Average waiting time: (6 + 0 + 3)/3 = 3 Average response time? Turnaround time? Much better than previous case Convoy effect short process behind long process
Shortest Process Next/Shortest Job First determine location in queue (compare next CPU burst lengths) CPU long jobs short jobs Whenever CPU is idle, picks process with shortest next CPU burst. Advantages: minimizes average waiting times. (frequently used in long-term scheduling) Problem: How to determine length of next CPU burst?! Problem: Starvation of jobs with long CPU bursts.
Example of SJF Process Burst Time P 1 6 P 2 8 P 3 7 P 4 3 SJF scheduling chart P 4 P P 1 3 P 2 0 3 9 16 Average waiting time = (0 + 3 + 9+ 16) / 4 = 7 Average turnaround time? Average response time? 24
SJF Minimizes Average Waiting Time Provably optimal: Proof: swapping of jobs P long P short P short P long dw = t short - t long < 0 Example: 6 12 8 4 W = 6+18+26 = 50 6 8 12 4 W = 6+14+26 = 46 6 8 4 12 W = 6+14+18 = 38 6 4 8 12 W = 6+10+18 = 34 4 6 8 12 W = 4+10+18 = 32
Determining Length of Next CPU Burst Can only estimate the length Can be done by using the length of previous CPU bursts, using exponential averaging 1. 2. 3. 4. t n actual length of n n 1, 0 1 Define : CPU burst predicted value for the next CPU th 1. n 1 tn n burst
(Fixed) Priority Scheduling Selector (compare priorities) CPU low priority high priority Whenever CPU is idle, picks process with highest priority. Priority: process class, urgency, pocket depth. Unbounded blocking: Starvation Increase priority over time: aging
Selector (compare priorities) Selector (compare priorities) Conceptually Priority Queues low priority priority queue priority low priority q=f(p) high priority CPU high priority CPU
FIFO queue Round-Robin FIFO with preemption after time quantum Method for time sharing Choice of time quantum: large: FCFS small: Processor sharing Time quantum also defines context-switching overhead end of time quantum CPU terminate
Example of RR with Time Quantum = 4 The Gantt chart is: Process Burst Time P 1 24 P 2 3 P 3 3 P 1 P 2 P 3 P 1 P 1 P 1 P 1 P 1 0 4 7 10 14 18 22 26 30 Average response time: (0+4+7)/3 = 11/3 Average turnaround time: (30+7+10)/3 = 47/3 Average waiting time: (6 + 4 + 7)/3 = 17/3 Typically, higher average turnaround than SJF, but better response
Time Quantum and Context Switch Time
Turnaround Time Varies With The Time Quantum
Multilevel Queue Scheduling
Selector (compare priorities) Multilevel Feedback Queue Scheduling (conceptually) low priority FCFS (quantum = infinity) quantum = 16 ms aging quantum = 4ms quantum = 2 ms demotion high priority