Title: Chapter 7: Scheduling
1Chapter 7 Scheduling
Prof. Steven A. Demurjian, Sr. Computer Science
Engineering Department The University of
Connecticut 191 Auditorium Road, Box
U-155 Storrs, CT 06269-3155
steve_at_engr.uconn.edu http//www.engr.uconn.edu/st
eve (860) 486 - 4818
These slides have been modified from a set
of originals by Dr. Gary Nutt.
2Purpose of this Chapter
- What is Role of Scheduling in an OS?
- What are the Different Components of a Scheduler?
- How Does a Scheduler Work in an OS?
- How Does Scheduler Strategy Affect and Dictate
Process Behavior? - What are Different Scheduling Policies?
- Voluntary
- Preemptive
- What is the Role of Interrupts in Scheduling?
- How Does Priority of Process Impact Scheduling?
3What is Role of Scheduling in an OS?
- Assuming Multi-Programmed OS
- Multiple Processes Loaded and Active in
Executable Memory - CPU Time-Multiplexing Among Active Processes
- Scheduler is Needed to
- Allow OS and Application Processes to Share CPU
- Permit Processes Performing/Waiting for I/O to
Release CPU - Scheduler Part of the Process Manager (Chapter 6)
- Removal of Running Process from CPU
- Selection of Another Process Based on Strategy
4Recall Process Manager Overview
Process
Process
Process
Abstract Computing Environment
Synchronization
Process Description
File Manager
Protection
Deadlock
Process Manager
Memory Manager
Device Manager
Resource Manager
Resource Manager
Scheduler
Resource Manager
CPU
Other H/W
Memory
Devices
5What are the Different Components of a Scheduler?
- Scheduling Policy Determines
- When a Process is to be Removed from CPU
- and Which Ready Process Should Run Next
- Scheduling Mechanism Determines
- How Process Manager Knows When to Time-Multiplex
CPU - and How Process (De)Allocated From/To CPU
- Typical Components Include
- Ready List
- Enqueuer
- Context Switcher
- Dispatcher
6What Functions Does Each Components in a
Scheduler?
- Ready List Process Manager Data Structure
- Queue of Pointers to Process Descriptors
- Each Pointer References Ready Process
- Enqueuer Moves Process Between States
- Process is Changed to Ready State
- Process Descriptor is Updated
- Pointer to Process Descriptor in Ready List
- May Also
- Compute Priority for Allocating CPU to Process
During List Insertion or Removal
7What Functions Does Each Components in a
Scheduler?
- Context Switcher Change Running Process
- Saves Content of Registers (PC, IR, etc.) for
Process Being Removed in Descriptor - Voluntary Invoked by Running Process that
Intends to Release CPU - Preemptive Interrupt that Causes Handler that
Invokes Context Switch - Context Switch Must Also Occur Between Scheduler
Processes Application Processes - Dispatcher Select Next Process to Run
- Context Switch to Run Dispatcher
- Select Process from Ready List Based on Scheduler
Policy - Context Switch from Itself to Selected Process
8Software Architecture The Scheduler
From Other States
Process Descriptor
Ready Process
Enqueue
Ready List
Context Switch
Dispatch
CPU
9How Does a Scheduler Work in an OS?
- Scheduler is Key Component of OS
- Top Loop - Process in Ready State
- Bottom Loop - Process Blocked Until Request is
Satisfied by Resource Manager
10Scheduler as Resource Manager
Scheduler
Release
Ready to run
Dispatch
Dispatch
Release
Release
Process
Dispatch
Units of time for a time-multiplexed CPU
11 Voluntary CPU Sharing
- Initiated by Process via yield Command to Release
CPU - Pi Can Be Automatically Determined From the
Processor Status Registers - Whats Major Problem in this Strategy?
yield(pi.pc, pj.pc) memorypi.pc PC
PC memorypj.pc
yield(, pj.pc) memorypi.pc PC PC
memorypj.pc
12More on Yield
- Pi and Pj Can Resume One Anothers Execution
- Suppose Pj is the Scheduler
yield(, pj.pc) . . . yield(, pi.pc) . .
. yield(, pj.pc) . . .
// p_i yields to scheduler yield(, pj.pc) //
scheduler chooses pk yield(, pk.pc) // pk
yields to scheduler yield(, pj.pc) // scheduler
chooses ...
13Voluntary Sharing
- Every Process Periodically Yields to the
Scheduler - Relies on Correct Process Behavior
- Malicious
- Accidental
- Need a Mechanism to Override Running Process
- Overall
- Not Utilized in Practice
- Lack of Fairness
- Depends on Software Engineers
- In What Situations May Voluntary Sharing be
Utilized?
EMBEDDED OS/APPLICATIONS (AVIONICS)!!!
14Involuntary or PreemptiveCPU Sharing
- Programmable Interval Timer
- Device to Produce a Periodic Interrupt
- Re-Programmable Period
- Logically Equivalent to yield
IntervalTimer() InterruptCount--
if(InterruptCount lt 0)
InterruptRequest TRUE InterruptCount
K
SetInterval(programmableValue) K
programmableValue InterruptCount K
15Involuntary or Preemptive CPU Sharing (Continued)
- Interval Timer Device Handler
- Can Keep an In-Memory Clock Up-to-date
- Can Invoke the Scheduler
IntervalTimerHandler() Time // update
the clock TimeToSchedule--
if(TimeToSchedule lt 0) ltinvoke
schedulergt TimeToSchedule TimeSlice
16Scheduling and Performance
- Scheduler Can Control
- CPU Utilization (Always Keep Busy)
- Average Time a Process Waits for Service in Ready
List - Average Amount of Time to Complete a Job
Preemption or Voluntary Yield
New Process
Ready List
Scheduler
CPU
Done
Resource Manager
Allocate
Request
17Historical Perspective
- Schedulers Studied in Last 30 Years
- Scheduler Key to Performance of Multi-Programming
OS - Individual Performance of Processes
- Overall Performance of System (CPU and I/O
Utilization) - Long Studied Problem in OR
- Theoretical CS
- Is it Possible to
- Predict Performance?
- Guarantee Equitable Sharing of CPU?
- Optimize Based on Class of Process?
18How Does Scheduler Strategy Affect and Dictate
Process Behavior?
- Strategy Policy the Scheduler Mechanism Uses to
Choose From the Ready List - Strive for Any Of
- Equitability or Fairness
- Favor Very Short or Long Jobs
- Meet Priority and/or Deadline Requirements
- Different Policies for Different Requirements
- Mechanism Never Changes
- Scheduling Strategy Depends on
- Goals of OS, Process Priorities, Fairness,
Resource Utilization, Throughput, Turnaround
Time, Response Time, System Availability
19ContemporaryScheduling Mechanism
- Involuntary CPU Sharing -- Timer Interrupts
- Time Quantum Determined by Interval Timer
- Usually Fixed for Every Process in the System
- Sometimes Called the Time Slice Length
- Priority-Based Process (Job) Selection
- Dispatcher Selects the Highest Priority Process
- Priority Reflects Policy
- May or May Not Have Preemption
- Combination of Processes in Ready List,
Preemptive Scheduler, Specific Goal for
Scheduling Can Lead to Optimal Schedule
20Optimal Scheduling
- Suppose the Scheduler Knows Each Process pis
Service Time, t(pi) - Could Look at Every pi and t(pi) in the Ready
List - Choose the Scheduler That Optimized on Any
Desired Criteria - But,
- Other Processes May Arrive While These Processes
are Being Serviced - The t(pi) are Almost Certainly Just Estimates
- Algorithm to Choose Optimal Schedule is O(n2),
Which is Costly in Practice!
21Talking About Scheduling ...
- Let P pi 0 ? i lt n Set of Processes
- Let S(pi) ? Running, Ready, Blocked
- Let t(pi) Service Time Process Needs to Be in a
Running State - Let W(pi) Wait Time that pi is in Ready State
Before First Transition to a Running State - Let TTRnd(pi) Turnaround Time From When pi
First Enters Ready List to Last Exit Ready List - Batch Throughput Rate Inverse of Avg TTRnd
- Timesharing Response Time W(pi)
22Simple Scheduling Model
- Only Consider Running and Ready States
- Ignore Resource Manager (Time in Blocked State)
- Process Created When it Enters Ready
- Process is Destroyed When it Enters Blocked
- Just Looking at Small Phases of a Process
- Resource Management Time Increases TTRnd for
Process - But Doesnt Impact Scheduling Directly
Preemption or voluntary yield
Ready List
Scheduler
New Process
CPU
Done
23Non-Preemptive Schedulers
- Processes Run to Completion Once Scheduled and
the CPU is Allocated - Similar in Concept to OR Research
- Service Time for Bank, Airport, Supermarket
- Easy to build and analyze
- Easy to analyze performance
- No issue of voluntary/involuntary sharing
- Under What Situations is Non-Preemptive
Scheduling Still Relevant?
Ready List
Scheduler
New Process
CPU
Done
24First-Come-First-Served
i t(pi) 0 350 1 125 2 475 3 250 4
75
0
350
p0
TTRnd(p0) t(p0) 350
W(p0) 0
25First-Come-First-Served
i t(pi) 0 350 1 125 2 475 3 250 4
75
475
350
p0
p1
TTRnd(p0) t(p0) 350 TTRnd(p1) (t(p1)
TTRnd(p0)) 125350 475
W(p0) 0 W(p1) TTRnd(p0) 350
26First-Come-First-Served
i t(pi) 0 350 1 125 2 475 3 250 4
75
475
950
350
p0
p1
p2
TTRnd(p0) t(p0) 350 TTRnd(p1) (t(p1)
TTRnd(p0)) 125350 475 TTRnd(p2) (t(p2)
TTRnd(p1)) 475475 950
W(p0) 0 W(p1) TTRnd(p0) 350 W(p2)
TTRnd(p1) 475
27First-Come-First-Served
i t(pi) 0 350 1 125 2 475 3 250 4
75
1200
950
475
350
p1
p2
p3
p0
p1
TTRnd(p0) t(p0) 350 TTRnd(p1) (t(p1)
TTRnd(p0)) 125350 475 TTRnd(p2) (t(p2)
TTRnd(p1)) 475475 950 TTRnd(p3) (t(p3)
TTRnd(p2)) 250950 1200
W(p0) 0 W(p1) TTRnd(p0) 350 W(p2)
TTRnd(p1) 475 W(p3) TTRnd(p2) 950
28First-Come-First-Served
i t(pi) 0 350 1 125 2 475 3 250 4
75
1275
1200
950
475
350
p4
p2
p3
p0
p1
TTRnd(p0) t(p0) 350 TTRnd(p1) (t(p1)
TTRnd(p0)) 125350 475 TTRnd(p2) (t(p2)
TTRnd(p1)) 475475 950 TTRnd(p3) (t(p3)
TTRnd(p2)) 250950 1200 TTRnd(p4) (t(p4)
TTRnd(p3)) 751200 1275
W(p0) 0 W(p1) TTRnd(p0) 350 W(p2)
TTRnd(p1) 475 W(p3) TTRnd(p2) 950 W(p4)
TTRnd(p3) 1200
29FCFS Average Wait Time
- Easy to Implement
- Ignores Service Time, Etc.
- Not a Great Performer
i t(pi) 0 350 1 125 2 475 3 250 4
75
1275
1200
950
475
350
p4
p2
p3
p0
p1
TTRnd(p0) t(p0) 350 TTRnd(p1) (t(p1)
TTRnd(p0)) 125350 475 TTRnd(p2) (t(p2)
TTRnd(p1)) 475475 950 TTRnd(p3) (t(p3)
TTRnd(p2)) 250950 1200 TTRnd(p4) (t(p4)
TTRnd(p3)) 751200 1275
W(p0) 0 W(p1) TTRnd(p0) 350 W(p2)
TTRnd(p1) 475 W(p3) TTRnd(p2) 950 W(p4)
TTRnd(p3) 1200
Wavg (03504759501200)/5 2975/5
595
TTRnd (35047595012001275)/5
4250/5 850
30Shortest Job Next
i t(pi) 0 350 1 125 2 475 3 250 4
75
75
0
p4
TTRnd(p4) t(p4) 75
W(p4) 0
31Shortest Job Next
i t(pi) 0 350 1 125 2 475 3 250 4
75
200
75
0
p1
p4
TTRnd(p1) t(p1)t(p4) 12575
200 TTRnd(p4) t(p4) 75
W(p1) 75 W(p4) 0
32Shortest Job Next
i t(pi) 0 350 1 125 2 475 3 250 4
75
450
200
75
0
p1
p3
p4
TTRnd(p1) t(p1)t(p4) 12575
200 TTRnd(p3) t(p3)t(p1)t(p4) 25012575
450 TTRnd(p4) t(p4) 75
W(p1) 75 W(p3) 200 W(p4) 0
33Shortest Job Next
i t(pi) 0 350 1 125 2 475 3 250 4
75
800
450
200
75
0
p0
p1
p3
p4
TTRnd(p0) t(p0)t(p3)t(p1)t(p4)
35025012575 800 TTRnd(p1) t(p1)t(p4)
12575 200 TTRnd(p3) t(p3)t(p1)t(p4)
25012575 450 TTRnd(p4) t(p4) 75
W(p0) 450 W(p1) 75 W(p3) 200 W(p4) 0
34Shortest Job Next
i t(pi) 0 350 1 125 2 475 3 250 4
75
1275
800
450
200
75
0
p0
p1
p2
p3
p4
TTRnd(p0) t(p0)t(p3)t(p1)t(p4)
35025012575 800 TTRnd(p1) t(p1)t(p4)
12575 200 TTRnd(p2) t(p2)t(p0)t(p3)t(p1)t
(p4) 47535025012575 1275 TTRnd(p3)
t(p3)t(p1)t(p4) 25012575 450 TTRnd(p4)
t(p4) 75
W(p0) 450 W(p1) 75 W(p2) 800 W(p3)
200 W(p4) 0
35Shortest Job Next
- Minimizes Wait Time
- May Starve Large Jobs
- Must Know Service Times in Advance!
i t(pi) 0 350 1 125 2 475 3 250 4
75
1275
800
450
200
75
0
p0
p1
p2
p3
p4
Wavg (450758002000)/5 1525/5
305
TTRnd (800200127545075)/5
2800/5 560
36Priority Scheduling
- Reflects Importance of External Use
- May Cause Starvation
- Can Address Starvation With Aging
i t(pi) Pri 0 350 5 1 125 2 2
475 3 3 250 1 4 75 4
1275
925
850
375
250
0
p0
p1
p2
p3
p4
TTRnd(p0) t(p0)t(p4)t(p2)t(p1) )t(p3)
35075475125250 1275 TTRnd(p1)
t(p1)t(p3) 125250 375 TTRnd(p2)
t(p2)t(p1)t(p3) 475125250 850 TTRnd(p3)
t(p3) 250 TTRnd(p4) t(p4) t(p2) t(p1)t(p3)
75475125250 925
W(p0) 925 W(p1) 250 W(p2) 375 W(p3)
0 W(p4) 850
Wavg (9252503750850)/5 2400/5
480
TTRnd (1275375850250925)/5
3675/5 735
37Comparing Non-Preemptive Scheduling
- First-Come-First-Serve
- Shortest Job Next
- Priority Scheduling
- Observations re. Behavior and Tradeoffs?
Wavg (03504759501200)/5 2975/5
595
TTRnd (35047595012001275)/5
4250/5 850
Wavg (450758002000)/5 1525/5
305
TTRnd (800200127545075)/5
2800/5 560
Wavg (9252503750850)/5 2400/5
480
TTRnd (1275375850250925)/5
3675/5 735
38Deadline Scheduling
i t(pi) Deadline 0 350 575 1 125
550 2 475 1050 3 250
(none) 4 75 200
- Must Receive Service by Deadline
- May Not Be Attainable in Practice!
- What are TTRnd(pi) and Average TTRnd?
- What are W(pi) and Average W?
39Preemptive Schedulers
- Highest Priority Process is Guaranteed to Be
Running at All Times - Or at Least at the Beginning of a Time Slice
- Lower Priority Processes Made to Yield
- Tradeoff with Equitability for All Processes
- Dominant Form of Contemporary Scheduling
- But Complex to Build Analyze
- Whats Not Shown in Figure Below?
CONTEXT SWITCHING AND ASSOC. COST!!!
Preemption or voluntary yield
Ready List
Scheduler
New Process
CPU
Done
40Round Robin Scheduling
- Employs Combination of Following
- Time Slicing for All Processes
- Context Switch Between Processes
- Timer-Interrupt Driven
- Timer Functions as Follows
- Allow Process to Run for its Time Quantum
- Generate Interrupt
- Results in Interrupt Handler that Calls Scheduler
- Scheduler Removes Current Process
- Dispatcher/Context Switcher Starts Next Ready
Process
41Round Robin (TQ50)
i t(pi) 0 350 1 125 2 475 3 250 4
75
0
50
p0
W(p0) 0
42Round Robin (TQ50)
i t(pi) 0 350 1 125 2 475 3 250 4
75
100
0
p0
p1
W(p0) 0 W(p1) 50
43Round Robin (TQ50)
i t(pi) 0 350 1 125 2 475 3 250 4
75
100
0
p0
p2
p1
W(p0) 0 W(p1) 50 W(p2) 100
44Round Robin (TQ50)
i t(pi) 0 350 1 125 2 475 3 250 4
75
200
100
0
p3
p0
p2
p1
W(p0) 0 W(p1) 50 W(p2) 100 W(p3) 150
45Round Robin (TQ50)
i t(pi) 0 350 1 125 2 475 3 250 4
75
200
100
0
p4
p3
p0
p2
p1
W(p0) 0 W(p1) 50 W(p2) 100 W(p3)
150 W(p4) 200
46Round Robin (TQ50)
i t(pi) 0 350 1 125 2 475 3 250 4
75
300
200
100
0
p0
p4
p3
p0
p2
p1
W(p0) 0 W(p1) 50 W(p2) 100 W(p3)
150 W(p4) 200
47Round Robin (TQ50)
i t(pi) 0 350 1 125 2 475 3 250 4
75
475
400
300
200
100
0
550
p4
p0
p4
p3
p0
p2
p1
p3
p2
p1
p0
p1
TTRnd(p4) 475
W(p0) 0 W(p1) 50 W(p2) 100 W(p3)
150 W(p4) 200
48Round Robin (TQ50)
i t(pi) 0 350 1 125 2 475 3 250 4
75
475
400
300
200
100
0
550
p4
p0
p4
p3
p0
p2
p1
p3
p2
p1
p0
p1
TTRnd(p1) 550 TTRnd(p4) 475
W(p0) 0 W(p1) 50 W(p2) 100 W(p3)
150 W(p4) 200
49Round Robin (TQ50)
i t(pi) 0 350 1 125 2 475 3 250 4
75
550
650
475
400
300
200
100
0
p4
p0
p4
p3
p0
p2
p1
p3
p2
p1
p0
p1
p3
p2
650
750
850
950
p0
p3
p2
p0
p3
p2
TTRnd(p1) 550 TTRnd(p3) 950 TTRnd(p4) 475
W(p0) 0 W(p1) 50 W(p2) 100 W(p3)
150 W(p4) 200
50Round Robin (TQ50)
i t(pi) 0 350 1 125 2 475 3 250 4
75
550
650
475
400
300
200
100
0
p4
p0
p4
p3
p0
p2
p1
p3
p2
p1
p0
p1
p3
p2
650
750
850
950
1050
p0
p2
p0
p0
p3
p2
p0
p3
p2
TTRnd(p0) 1100 TTRnd(p1) 550 TTRnd(p3)
950 TTRnd(p4) 475
W(p0) 0 W(p1) 50 W(p2) 100 W(p3)
150 W(p4) 200
51Round Robin (TQ50)
i t(pi) 0 350 1 125 2 475 3 250 4
75
550
650
475
400
300
200
100
0
p4
p0
p4
p3
p0
p2
p1
p3
p2
p1
p0
p1
p3
p2
1150
1250
1275
650
750
850
950
1050
p2
p2
p2
p2
p0
p2
p0
p0
p3
p2
p0
p3
p2
TTRnd(p0) 1100 TTRnd(p1) 550 TTRnd(p2)
1275 TTRnd(p3) 950 TTRnd(p4) 475
W(p0) 0 W(p1) 50 W(p2) 100 W(p3)
150 W(p4) 200
52Round Robin (TQ50)
- Equitable
- Most Widely-used
- Fits Naturally With Interval Timer
i t(pi) 0 350 1 125 2 475 3 250 4
75
550
650
475
400
300
200
100
0
p4
p0
p4
p3
p0
p2
p1
p3
p2
p1
p0
p1
p3
p2
1150
1250
1275
650
750
850
950
1050
p2
p2
p2
p2
p0
p2
p0
p0
p3
p2
p0
p3
p2
W(p0) 0 W(p1) 50 W(p2) 100 W(p3)
150 W(p4) 200
TTRnd(p0) 1100 TTRnd(p1) 550 TTRnd(p2)
1275 TTRnd(p3) 950 TTRnd(p4) 475
TTRnd_avg (11005501275950475)/5 4350/5
870
Wavg (050100150200)/5 500/5 100
53RR with Overhead10 (TQ50)
- Overhead must be Considered
- Each Context Switch Takes CPU Cycles
i t(pi) 0 350 1 125
i t(pi) 2 475 3 250 4 75
540
480
360
240
120
0
575
790
635
670
p0
p4
p1
p0
p4
p3
p2
p1
p1
p2
p3
p0
p3
p2
910
1030
1150
1270
1390
1510
1535
790
p0
p3
p2
p0
p3
p2
p0
p2
p0
p2
p2
p2
p2
TTRnd(p0) 1320 TTRnd(p1) 660 TTRnd(p2)
1535 TTRnd(p3) 1140 TTRnd(p4) 565
W(p0) 0 W(p1) 60 W(p2) 120 W(p3)
180 W(p4) 240
TTRnd_avg (132066015351140565)/5 5220/5
1044
Wavg (060120180240)/5 600/5 120
54Multi-Level Queues
Preemption or Voluntary Yield
Ready List0
New Process
Scheduler
Ready List1
CPU
Done
Ready List2
- All Processes at Level I Run Before
- Any Process at Level J
- At a Level, Use RR
- BSD UNIX Has 32 Run Queues
- Queues 0-7 Are for System
- Queues 8-31are for User
- nice Influences Run Queue
Ready List3
55Approximating Load
- Let l Mean Arrival Rate of New Processes into
Ready List - So 1/l Mean Time Between Arrivals
- And m Mean Service Rate
- So 1/m Mean Service Time (Avg t(pi))
- Measure of CPU Busy r l 1/m l/m
- Notice must have l lt m (i.e., r lt 1)
- What if r Exceeds 1?
- What if r Approaches 1?
- CPU Nearing Saturation
- Needs Arbitrarily Large Ready List
56Predicting Wait Time in FCFS
- In FCFS, When a Process Arrives, All in Ready
List Will Be Processed Before This Job - Let m be the Service Rate
- Let L be the Ready List Length
- Wavg(p) L1/m 0.5 1/m L/m1/(2m)
- Each Job in Queue uses Average of 1/m Time Units
for Service - L1/m is the Amount of Time for Processing
- 0.5 1/m is Average Time to Finish Processes in
Ready List - Compare Predicted Wait with Actual in Earlier
Examples
57Concluding Remarks/Looking Ahead
- Review of Scheduling
- Non-Preemptive
- FCFS, SJN, Priority, Deadline
- Voluntary vs. Run-Until Done
- Preemptive
- RR and RR with Context Time
- How Would SJN and Priority Scheduling work with
RR? - Looking Ahead to
- Synchronization (Chapters 8 9)
- Mid-Term Exam
- Deadlock (Chapter 10)
58Interesting Exercises - Section 7.6
- Problems 4, 5, 6, 9
- Construct Gnatt Chart
- Calculate Turnaround Time/Average Wait Time
- FCFS, SJN, and Priority Scheduling
- Problems 7 and 8
- Round Robin - Similar to 4, 5, 6
- Also Consider Context Switch
- Problem 10
- What is the Effect of Increasing Time Quantum to
Arbitrary Large Number for RR Scheduling? - What is Impact if Problem 7 is Redone with
Quantum of 100?