An overview of operating system scheduling, focusing on first-come, first-serve (fcfs), round robin (rr), shortest job first (sjf), and shortest remaining time first (srtf) scheduling algorithms. It discusses their pros and cons, as well as multi-level feedback scheduling and lottery scheduling as alternatives.
Download Understanding OS Scheduling: FCFS, Round Robin, Shortest Job First, and Lottery Scheduling and more Study notes Operating Systems in PDF only on Docsity!
Scheduling Algorithms
Jay Kothari ([email protected]) CS 370: Operating Systems July 9, 2008
CPU Scheduling
Earlier, we talked about the life-cycle of a thread
Active threads work their way from Ready queue to Running to various waiting queues.
Question: How is the OS to decide which of several tasks to take off a queue? - Obvious queue to worry about is ready queue - Others can be scheduled as well, however
Scheduling: deciding which threads are given access to resources from moment to moment
Program behavior and scheduling...
Assumption: CPU Bursts
What is important in a scheduling algorithm?
Scheduling Policy Goals/Criteria
Minimize Response Time
Minimize elapsed time to do an operation (or job)
Response time is what the user sees:
Time to echo a keystroke in editor
Time to compile a program
Real-time Tasks: Must meet deadlines imposed by World
Maximize Throughput
Maximize operations (or jobs) per second
Throughput related to response time, but not identical:
Minimizing response time will lead to more context switching than if you only maximized throughput
Two parts to maximizing throughput
Minimize overhead (for example, context-switching)
Efficient use of resources (CPU, disk, memory, etc)
Fairness
Share CPU among users in some equitable way
Fairness is not minimizing average response time:
Better average response time by making system less fair
First-Come, First-Serve Scheduling (1)
First-Come, First-Served (FCFS)
Also “First In, First Out” (FIFO) or “Run until done”
In early systems, FCFS meant one program scheduled until done (including I/O)
Now, means keep CPU until thread blocks
Example:
Suppose processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is:
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 17
Average Completion time: (24 + 27 + 30)/3 = 27 P 1 P 2 P 3 0 24 27 30
FCFS (2)
Example continued:
Suppose that processes arrive in order: P2 , P3 , P Now, the Gantt chart for the schedule is:
Waiting time for P1 = 6; P2 = 0; P3 = 3
Average waiting time: (6 + 0 + 3)/3 = 3
Average Completion time: (3 + 6 + 30)/3 = 13
In second case:
Average waiting time is much better (before it was 17)
Average completion time is better (before it was 27)
FIFO Pros and Cons:
Simple (+)
Short jobs get stuck behind long ones (-)
Supermarket: Getting milk, always stuck behind cart full of small items. Upside: get to read about Britney Spears! P 2 P 3 P 1 0 3 6 30
Round Robin (RR) Scheduling
FCFS Scheme: Potentially bad for short jobs!
Depends on submit order
If you are first in line at supermarket with milk, you don’t care who is behind you, on the other hand…
Round Robin Scheme
Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds
After quantum expires, the process is preempted and added to the end of the ready queue.
n processes in ready queue and time quantum is q
Each process gets 1/n of the CPU time
In chunks of at most q time units
No process waits more than (n-1)q time units
Performance
q large FCFS
q small Interleaved (really small hyperthreading?)
q must be large with respect to context switch, otherwise overhead is too high (all overhead)
RR with Time Quantum = 20 milliseconds
Example:
Waiting time for P1=(68-20)+(112-88)= P2=(20-0)= P3=(28-0)+(88-48)+(125-108)= P4=(48-0)+(108-68)=
Average waiting time = (72+20+85+88)/4=66¼
Average completion time = (125+28+153+112)/4 = 104½
Thus, Round-Robin Pros and Cons:
Better for short jobs, Fair (+)
Context-switching time adds up for long jobs (-) P 1 P 2 P 3 P 4 P 1 P 3 P 4 P 1 P 3 P 3 0 20 28 48 68 88 108 112 125 145 153
Comparing FCFS and RR
Assuming zero-cost context-switching time, is RR always better than FCFS?
Simple example: 10 jobs, each take 100s of CPU time RR scheduler quantum of 1s All jobs start at the same time
Completion Times:
Both RR and FCFS finish at the same time
Average response time is much worse under RR!
Bad when all jobs same length
Also: Cache state must be shared between all jobs with RR but can be devoted to each job with FIFO - Total time for RR longer even for zero-cost switch!
FCFS with Different Time Quantum
Best FCFS:
Discussion
SJF/SRTF are the best you can do at minimizing average response time
Provably optimal (SJF among non-preemptive, SRTF among
preemptive)
Since SRTF is always at least as good as SJF, focus on SRTF
Comparison of SRTF with FCFS and RR
What if all jobs the same length?
SRTF becomes the same as FCFS (i.e. FCFS is best can do
if all jobs the same length)
What if jobs have varying length?
SRTF (and RR): short jobs not stuck behind long ones
Example: SRTF (1)
Three jobs:
A,B: both CPU bound, run for week C: I/O bound, loop 1ms CPU, 9ms disk I/O
If only one at a time, C uses 90% of the disk, A or B could use 100% of the CPU