4 Scheduling
4 Scheduling
Scheduling Algorithms
Process Scheduling
Process execution consists of a CPU burst followed by I/O
burst
CPU burst distribution is of main concern
Preemptive
Can be taken out from the processor after the time
slice gets expire
Non-preemptive
Cannot be taken out unless a process completes its
execution
Scheduling Criteria
CPU utilization – keep the CPU as busy as possible
Waiting time – amount of time a process has been waiting in the ready queue
Response time – amount of time it takes from when a request was submitted
until the first response is produced, not output (for time-sharing environment)
Scheduling Algorithm Optimization Criteria
Maximize CPU utilization
Maximize throughput
Minimize turnaround time
Minimize waiting time
Minimize response time
Process Scheduling
Multiprogramming needs CPU scheduling
Without any hardware support, what can the OS do to a running
process?
Timer interrupt
Generated by the hardware
Setting requires privilege
Delivered to the OS
Preemptive Scheduler
Using interrupts for scheduling
Basic Idea
Before moving process to running, OS sets timer
If process yields/blocks, clear timer, go to scheduler
If timer expires, go to scheduler
Recall Context Switching!
Scheduling
How long can they keep it? When more resources are requested than
can be granted, in what order can they be serviced?
[Lecture 1] Separating Policy from
Mechanism
Issues?
Short jobs shouldn’t suffer
Users shouldn’t be annoyed
Challenges in Policy (cont.)
Fairness
All users should get access to CPU
Amount of CPU should be roughly even?
Issues?
Short-term vs. long-term fairness
Goals and Assumptions
Goals (Performance metric)
Minimize turnaround time: average time to complete a job
Maximize throughput: operations (jobs) per second
Minimize overhead of context switches: large quanta
Efficient utilization (CPU, memory, disk etc.)
Short response time: type on a keyboard
Small quanta
Fairness (fair, no starvation, no deadlock)
Goals often conflict
Fairness vs. average turnaround time?
Assumptions
One process/program per user
Programs are independent
Scheduling Policies
Is there an optimal scheduling policy?
Even if we narrow down to one goal?
P1 P2 P3
0 24 27 30
P2 P3 P1
0 3 6 30
Disadvantages
Non-Preemptive scheduling algorithm so after the process has been
allocated to the CPU, it will never release the CPU until it finishes
executing or blocked.
The Average Waiting Time is high.
Short processes at the back wait for the long process at the front.
Not an ideal technique for time-sharing systems.
Shortest-Job-First (SJF) Scheduling Policy
Associate with each process the length of its next CPU burst
Use these lengths to schedule the process with the shortest time
Non-preemptive
Advantage
SJF is optimal – gives minimum average waiting time and
turnaround time for a given set of processes
Disadvantage
Can cause starvation if there is a steady supply of shorter processes
Difficult to know the future (length of the next CPU request)
Example: SJF Scheduling Policy
Process Burst Time
P1 6
P2 8
P3 7
P4 3
P4 P1 P3 P2
0 3 9 16 24
P4 P3 P1 P2
0 7 14 22
3
P1 P3 P4 P2
0 13 16 22
6
SJF with Arrival time
Process Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 7.0 3
P1 P3 P4 P2
0 13 16 22
6
Advantage
Minimal average waiting and turnaround time for a given set of
processes
Disadvantage
Like SJF, can cause starvation for longer processes
Example: SRTF Scheduling Policy
Now we add the concepts of varying arrival times and preemption to the
analysis
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1 P2 P4 P1 P3
8 14 22
0 1 4
P1 = 8 – 1 = 7
P2 = 1 – 1 = 0
P3 = 14 – 2 = 12
P4 = 4 – 3 = 1
Average waiting time = (7 + 0 + 12 + 1) / 4 = 5
Priority Scheduling
Assign each process a priority (integer) number (smallest integer highest
priority)
Run the process with highest priority in the ready queue first
Preemptive
Non-preemptive
Use FIFO for processes with equal priority
SJF is priority scheduling where priority is the inverse of predicted next
CPU burst time
Advantage
Flexibility: Not all processes are born equal
P2 P5 P1 P3 P4
0 1 6 16 18 19
Arrival Burst
Process Id Priority
time time
P1 0 4 4
P2 1 3 3
P3 2 1 2
P4 3 5 1
P5 4 2 1
Exercise
Arrival Burst
Process Id Priority
time time
P1 0 4 4
P2 1 3 3
P3 2 1 2
P4 3 5 1
P5 4 2 1
Exercise
Arrival Burst
Process Id Priority
time time
P1 0 4 4
P2 1 3 3
P3 2 1 2
P4 3 5 1
P5 4 2 1
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Typically, higher average turnaround time than SJF, but better response
time
q should be large compared to context switch time
q usually 10ms to 100ms, context switch < 10 usec
RR
Advantages
best performance in terms of average response time.
best suited for
time sharing system,
client server architecture and
interactive system.
Disadvantages
leads to starvation for processes with larger burst time
as they have to repeat the cycle many times.
Its performance heavily depends on time quantum.
Important Notes
With decreasing value of time quantum,
Number of context switch increases
Response time decreases
Chances of starvation decreases
When time quantum tends to infinity, Round Robin Scheduling becomes
FCFS Scheduling.
The value of time quantum should be
neither too big nor too small.
Turnaround Time Varies With The Time Quantum
P1 0 5
P2 1 3
P3 2 1
P4 3 2
P5 4 3
Time slice = 2
sec
Exercise
Process Id Arrival time Burst time
P1 0 5
P2 1 3
P3 2 1
P4 3 2
Time slice = 2 P5 4 3
sec
Most flexible
because it can be tuned for any situation.
RR is 23ms: