CSCE 313 Introduction to Computer Systems Instructor: Dr. Guofei Gu http://courses.cse.tamu.edu/guofei/csce313/ 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 1
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. 2
Scheduling Decisions Who is going to use the CPU next?! ready 4 running 2 3 1 waiting non-preemptive 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. preemptive Structure of a Scheduler ready queue PCB scheduler dispatcher CPU select process? restart executing process 3
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 4
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: P 1 P 2 P 3 0 24 27 30 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 5
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 3 6 30 Waiting time for P 1 = 6; P 2 = 0 ; P 3 = 3 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. 6
Example of SJF Process Burst Time P 1 6 P 2 8 P 3 7 P 4 3 SJF scheduling chart P 4 P 1 P 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 7
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. t n = actual length of n 2. tn+ 1 3. a, 0 a 1 4. Define : th = predicted value for CPU burst the next CPU ( 1 a ). t = a t + - t n = 1 n 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 8
Conceptually Priority Queues low priority priority queue priority low priority q=f(p) Selector (compare priorities) Selector (compare priorities) high priority CPU high priority CPU 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 FIFO queue end of time quantum CPU terminate 9
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 10
Turnaround Time Varies With The Time Quantum Multilevel Queue Scheduling 11
Multilevel Feedback Queue Scheduling (conceptually) low priority FCFS (quantum = infinity) Selector (compare priorities) quantum = 16 ms quantum = 4ms aging quantum = 2 ms demotion high priority 12