0% found this document useful (0 votes)
28 views30 pages

AOS3PPT Lec4

Uploaded by

NarasimY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views30 pages

AOS3PPT Lec4

Uploaded by

NarasimY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Advanced Operating Systems

Advanced Scheduling Algorithms


Session 3

Yayati Gupta
[email protected]

Mahindra University

December 17, 2024

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 1 / 30
Advanced Operating Systems

Outline
1 Basic Scheduling algorithms: Recap
2 Multilevel Queue Scheduling
Basic Multilevel Queue Scheduling
Multilevel Feedback Queue Scheduling
3 Linux Fair Share Scheduling Algorithms
Lottery Scheduling
Completely Fair Scheduling
4 Multiprocessor Scheduling (MS)
Single Queue MS
Multi Queue MS
5 Real Time Scheduling Algorithms
Rate Monotonic Scheduling
Earliest Deadline First
Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 2 / 30
Basic Scheduling algorithms: Recap
Advanced Operating Systems
Basic Scheduling algorithms: Recap

Basic Scheduling Algorithms

In the following process state transition diagram for a uniprocessor system, assume that there
are always some processes in the ready state.
1 If a process makes a transition D, it would

result in another process making transition


A immediately.
2 A process P2 in blocked state can make
transition E while another process P1 is in
running state.
3 The OS uses preemptive scheduling.
4 The OS uses non-preemptive scheduling.

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 4 / 30
Advanced Operating Systems
Basic Scheduling algorithms: Recap

Non Preemptive Scheduling Algorithms

1 First Come First Served (FCFS): Ready Queue Addition: O(1), Scheduling: O(1)
2 Shortest Job First (SJF):
Unsorted: RQ Addition: O(1), Scheduling: O(n)
Sorted list: RQ Addition: O(n), Scheduling: O(1)
Min heap: RQ Addition: O(log n), Scheduling: O(log n)
Additional overhead of predicting burst time: O(1) in case of exponential averaging
3 NP Priority Scheduling: Similar to SJF.
User defined
System processes can be given higher priorities
Priority of I/O bound jobs > Priority of CPU bound jobs
Can be based on deadlines as in Real time scheduling

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 5 / 30
Advanced Operating Systems
Basic Scheduling algorithms: Recap

Preemptive Scheduling Algorithms

1 Round Robin Scheduling (RR)


2 Shortest Remaining Time First (SRTF)
3 P Priority Scheduling

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 6 / 30
Advanced Operating Systems
Basic Scheduling algorithms: Recap

Convoy Effect

P# AT CB IOB CB IOB CB IOB


P0 0 100 6 100 6 100 6
P1 1 1 6 1 6 1 6
P2 1 1 6 1 6 1 6
P3 1 1 6 1 6 1 6
P4 1 1 6 1 6 1 6
P5 1 1 6 1 6 1 6

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 7 / 30
Advanced Operating Systems
Basic Scheduling algorithms: Recap

Basic Scheduling Algorithms

Algorithm Pros Cons


FCFS Simple implementation, Fair Convoy effect
SJF/SRTF Optimal Needed prediction, Starvation
Priority Scheduling Flexible Starvation
Round Robin Fair Frequent context switches, Op-
timal time quantum
1 SJF/SRTF: Compute intensive tasks (Background processes)
2 Round Robin: I/O bound tasks (Foreground processes)
3 Priority: System processes
No one size fits all!

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 8 / 30
Multilevel Queue Scheduling
Advanced Operating Systems
Multilevel Queue Scheduling
Basic Multilevel Queue Scheduling

Multilevel Queue Scheduling (One size doesn’t fit all)


1 CPU bound
processes: SRTF
2 I/O bound
processes: RR
3 Partition the ready
queue into
multiple queues
with varying
priorities
4 Among queues:
Fixed Priority/
Timeslice

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 10 / 30
Advanced Operating Systems
Multilevel Queue Scheduling
Basic Multilevel Queue Scheduling

Multilevel Queue Scheduling Challenges

1 Starvation in low
priority queues
2 How to differenti-
ate/approximate?
CPU bound
processes
I/O bound
processes

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 11 / 30
Advanced Operating Systems
Multilevel Queue Scheduling
Multilevel Feedback Queue Scheduling

Most general and Turing Award winning Algorithm


1 Smartly infers the type of the processes (no history needed)
Qi : RR with time quantum 2i
Qn−1 : FCFS
2 Timeslice expired at Qj : Move to Qj+1
3 Windows: 32 priority levels/queues with top 50% for real-time tasks (e.g. multimedia,
gaming etc.)

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 12 / 30
Advanced Operating Systems
Multilevel Queue Scheduling
Multilevel Feedback Queue Scheduling

Question

Given:
Queues: Q1 (Time Quantum: 3), Q2 (Time Quantum: 6), Q3: FCFS
Processes:
Process Arrival Burst I/O
P1 0 6 4
P2 3 10 3
P3 6 4 2

Draw the Gantt chart and find waiting time and turnaround time.

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 13 / 30
Linux Fair Share Scheduling Algorithms
Advanced Operating Systems
Linux Fair Share Scheduling Algorithms
Lottery Scheduling

Linux Scheduling Algorithms

1 Designed for responsiveness fairness (avoid starvation)


Server oriented OS
HPC
Distributed OS
Cloud computing environments
2 Lottery Scheduling: Probabilistic Fairness
3 RR + Priority
4 A simple Python implementation
5 Variants: Tickets transfer, ticket inflation, hierarchical lottery

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 15 / 30
Advanced Operating Systems
Linux Fair Share Scheduling Algorithms
Lottery Scheduling

Question

Given:
Process P1: Priority = 5, Burst Time = 8 units.
Process P2: Priority = 3, Burst Time = 6 units.
Process P3: Priority = 4, Burst Time = 4 units.
Time Quantum (TQ) = 1 unit.
1 Calculate the probability of each process being selected for execution.
2 What is the expected CPU time that Process P2 will get in the first 4 units of
time?
3 What happens to the expected CPU time for P2 if the Time Quantum (TQ) is
changed to 2 units instead of 1 unit? Explain the difference in your answer.

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 16 / 30
Advanced Operating Systems
Linux Fair Share Scheduling Algorithms
Completely Fair Scheduling

Completely Fair Share Scheduling

1 Which process to run?: one with least Virtual Runtime (vruntime): proc/pid/sched
sched latency
2 How long to run?: n : /sys/kernel/debug/sched/latency ns
If n is very large, use min granularity: /sys/kernel/debug/sched/min granularity ns
3 Other parameters
Nice value: Determines priority
Each nice value maps to a weight: Kernel scheduler code(line 10,114)
How long to run: Adjust the formula based on wi .
A similar adjustment is made for vruntime.
4 Implemented with Red-Black trees for high performance.

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 17 / 30
Advanced Operating Systems
Linux Fair Share Scheduling Algorithms
Completely Fair Scheduling

Question

Consider the following processes with their corresponding weights and burst times:
Process 1: Weight = 1024, Burst Time = 12
Process 2: Weight = 512, Burst Time = 18
Process 3: Weight = 2048, Burst Time = 30
Given the following parameters for Completely Fair Share Scheduling (CFSS):
Latency = 24
Minimum Granularity = 3
Task: Find the CFSS schedule for the given processes.
Note: You can assume all units to be in milliseconds.

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 18 / 30
Multiprocessor Scheduling (MS)
Advanced Operating Systems
Multiprocessor Scheduling (MS)

Basics

Figure: Two CPUs with caches sharing memory

1 Multithreading with parallelism: Faster applications


2 Cache Coherence (Resolved by the hardware and semaphores)
3 Cache Affinity
Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 20 / 30
Advanced Operating Systems
Multiprocessor Scheduling (MS)
Single Queue MS

Single Queue MS

Simple to implement
The queue needs to be locked: Increases contention, reduces scalability
Cache affinity disregarded

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 21 / 30
Advanced Operating Systems
Multiprocessor Scheduling (MS)
Multi Queue MS

Multi Queue MS

No locking required
Cache affinity is preserved (Recall cpu core.c): CFSS uses MQMS.
Load imbalance problem
Solution: Use job migration

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 22 / 30
Real Time Scheduling Algorithms
Advanced Operating Systems
Real Time Scheduling Algorithms

Real Time Scheduling Algorithms


1 The goal is to maximise throughput, maximise response time, meet deadlines
2 Fairness doesn’t help you meet deadlines.

Figure: Cowboy Scheduling Algorithm

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 24 / 30
Advanced Operating Systems
Real Time Scheduling Algorithms

Cowboy Scheduling Algorithm

Priority scheduling (as used in Windows/Linux) can work for soft real time systems
Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 25 / 30
Advanced Operating Systems
Real Time Scheduling Algorithms

Timing Parameters for the process

1 Period, p (Rate = p1 )
2 Execution time, t
3 Deadline, d
4 0≤t≤d ≤p

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 26 / 30
Advanced Operating Systems
Real Time Scheduling Algorithms
Rate Monotonic Scheduling

Rate Monotonic Scheduling


1 Static priority scheduling
2 Priority ∝ Rate

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 27 / 30
Advanced Operating Systems
Real Time Scheduling Algorithms
Rate Monotonic Scheduling

Rate Monotonic Shceduling: Working Example

We have two processes P1 and P2. The periods for P1 and P2 are 50 and 100,
respectively-that is, Pl =50 and P2 = 100. The processing times are t1 = 20 for P1 and t2 =
35 for P2 . The deadline for each process requires that it complete its CPU burst by the start
of its next period.

Is it schedulable?
CPU utilisation ≤ 100%
CPU utilisation ≤ n(2n − 1)
= 82.8% for n = 2
= 69.3% for n → ∞

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 28 / 30
Advanced Operating Systems
Real Time Scheduling Algorithms
Earliest Deadline First

Earliest Deadline First

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 29 / 30
Advanced Operating Systems
Real Time Scheduling Algorithms
Earliest Deadline First

Thank You

Questions?

Yayati Gupta (Mahindra University) Advanced Operating Systems December 17, 2024 30 / 30

You might also like