Lec 05
Lec 05
ECE445M/ECE380L.12
Embedded and Real-Time Systems/
Real-Time Operating Systems
Lecture 5:
Real-Time Scheduling,
Priority Scheduler
Real-Time Scheduling
• Tasks have deadlines
– Some tasks are more important than others
– In order to do something first, something else
must be second
– Priority scheduler
• Reactivity
– When to run the scheduler?
• Periodically, systick and sleep
• On OS_Wait
• On OS_Signal Reference Book,
• On OS_Sleep, OS_Kill Chapter 6
J. Valvano, A. Gerstlauer 1
ECE445M/ECE380L.12, Lecture 5 2/10/2024
Pi
Computation time Ei
• Response time ri
– Time from arrival until finish of task
• Lateness li
– ri - ti
Lecture 5 J. Valvano, A. Gerstlauer Source: M. Jacome, UT Austin 3
ECE445M/ECE380L.12
Priority Scheduling
J. Valvano, A. Gerstlauer 2
ECE445M/ECE380L.12, Lecture 5 2/10/2024
Priority Scheduler
• Assigns each thread a priority number
– Reduce latency (response time) by giving high priority
– Static (creation) or dynamic (runtime)
– Performance measures (utilization, latency/lateness)
• Strictly run the ready task with highest priority at all times
– Priority 2 is run only if no priority 1 are ready
– Priority 3 only if no priority 1 or priority 2 are ready
– If all have the same priority, use a round-robin system
• Blocking semaphores and not spinlock semaphores
• On a busy system, low priority threads may never be run
– Problem: Starvation
– Solution: Aging
J. Valvano, A. Gerstlauer 3
ECE445M/ECE380L.12, Lecture 5 2/10/2024
• Solution to starvation
• Real and temporary priorities in TCB
• Priority scheduler uses temporary priority
• Increase temporary priority periodically
– If a thread is not running
• Reset temporary back to real when runs
J. Valvano, A. Gerstlauer 4
ECE445M/ECE380L.12, Lecture 5 2/10/2024
Exponential Queue
• Exponential comes from doubling/halving
1. Round robin with variable timeslices
• Time slices 8,4,2,1 ms
2. Priority with variable priority/timeslices
• Time slices 8,4,2,1 ms
• Priorities 0,1,2,3
Scheduling Metrics
• How do we evaluate a scheduling policy?
– Ability to satisfy all deadlines
• Minimize maximum lateness
– CPU utilization Si Ei / ti
• Percentage of time devoted to useful work
– Scheduling overhead
• Time required to make scheduling decision
J. Valvano, A. Gerstlauer 5
ECE445M/ECE380L.12, Lecture 5 2/10/2024
Scheduling Algorithms
• Rate monotonic scheduling (RMS), static
– Assign priority based on how frequent task is run
– Lower period (more frequent) are higher priority
• Earliest deadline first (EDF), dynamic
– Assign priority based on closest deadline
• Least slack-time first (LST), dynamic
– Slack = (time to deadline)-(work left to do)
• …
J. Valvano, A. Gerstlauer 6
ECE445M/ECE380L.12, Lecture 5 2/10/2024
Scheduling Analysis
• Rate monotonic scheduling theorem
– All n tasks are periodic
• Priority based on period ti
• Maximum execution time Ei
– No synchronization between tasks (independent)
– Execute highest priority task first
– Guarantee deadlines if processor utilization:
i n2
ti
E
1/n 1 ln(2)
≈ 69%
Lecture 5 J. Valvano, A. Gerstlauer 13
ECE445M/ECE380L.12
RMS Example 1
Process Execution Time Period
Pi Ei τi
P1 1 4
Static priority: P1 >> P2 >> P3
P2 2 6
P3 3 12
Critical instant
all tasks arrive at same time
P3
P2 Unrolled schedule
(least common multiple of
P1 process periods)
0 2 4 6 8 10 12
J. Valvano, A. Gerstlauer 7
ECE445M/ECE380L.12, Lecture 5 2/10/2024
RMS Example 2
Earliest-Deadline-First (EDF)
• Dynamic priority scheduling scheme
– Process closest to its deadline has highest
priority
• EDF is optimal
– EDF can use 100% of CPU for worst case
• Expensive to implement
– On each OS event, recompute priorities and
resort tasks
J. Valvano, A. Gerstlauer 8
ECE445M/ECE380L.12, Lecture 5 2/10/2024
EDF Example
P1
P2
EDF Example
P1
P2
J. Valvano, A. Gerstlauer 9
ECE445M/ECE380L.12, Lecture 5 2/10/2024
EDF Example
P1
P2
EDF Example
P1
P2
J. Valvano, A. Gerstlauer 10
ECE445M/ECE380L.12, Lecture 5 2/10/2024
EDF Example
P1
P2
EDF Example
P1
P2
No process is
ready…
t
J. Valvano, A. Gerstlauer 11
ECE445M/ECE380L.12, Lecture 5 2/10/2024
EDF Example
P1
P2
EDF Example
P1
P2
J. Valvano, A. Gerstlauer 12
ECE445M/ECE380L.12, Lecture 5 2/10/2024
Scheduling Anomalies
Courtesy NASA/JPL-Caltech
Priority inversion
Priority Inversion
• Low-priority process keeps high-priority
process from running.
– Low-priority process grabs resource
(semaphore)
– High-priority device needs resource
(semaphore), but can’t get it until low-priority
process is done.
• Can trigger deadlock
J. Valvano, A. Gerstlauer 13
ECE445M/ECE380L.12, Lecture 5 2/10/2024
Priority-Based Scheduling
Priority
High
Critical
section
Low
Time
t1 t2 tn-1 tn
Deadline
Priority
High Blocked
Low
Time
t1 t2 t3 tn-1 tn
Deadline
Deadline violation
Priority Inversion
• Low-priority process blocking high-priority
– Starvation of high priority processes
Priority
Priority inversion
High
Middle
Low
Time
t1 t2 t3 tn-3 tn-2 tn-1 tn
J. Valvano, A. Gerstlauer 14
ECE445M/ECE380L.12, Lecture 5 2/10/2024
Ceiling
High
Middle
Low
Time
t1 t2 t3 t4 t5 tn-1 tn
J. Valvano, A. Gerstlauer 15
ECE445M/ECE380L.12, Lecture 5 2/10/2024
High
Middle
Low
Time
t1 t2 t3 t4 t5 tn-1 tn
Fixed Scheduling
• Time-driven scheduler
– In advance, a priori, during the design phase
• Thread sequence
• Allocated time-slices
– Like
• Creating the city bus schedule
• Routing packages through a warehouse
• Construction project
• TDMA in communication networks
J. Valvano, A. Gerstlauer 16
ECE445M/ECE380L.12, Lecture 5 2/10/2024
J. Valvano, A. Gerstlauer 17
ECE445M/ECE380L.12, Lecture 5 2/10/2024
• Design strategy
– Schedule highest priority tasks first
• 100% satisfaction guaranteed
– Then schedule all real-time tasks
• Shuffle assignments like placing pieces in a puzzle
• Maximizing objectives
– The tasks that are not real-time can be
scheduled in the remaining slots.
• Four tasks
– Finite state machine (FSM)
– Proportional-integral-derivative controller (PID)
– Data acquisition system (DAS)
– Non-real-time task (PAN)
Lecture 5 J. Valvano, A. Gerstlauer 36
ECE445M/ECE380L.12
J. Valvano, A. Gerstlauer 18
ECE445M/ECE380L.12, Lecture 5 2/10/2024
PID
FSM
DAS
real-time task
Lecture 5 J. Valvano, A. Gerstlauer 38
ECE445M/ECE380L.12
J. Valvano, A. Gerstlauer 19
ECE445M/ECE380L.12, Lecture 5 2/10/2024
J. Valvano, A. Gerstlauer 20
ECE445M/ECE380L.12, Lecture 5 2/10/2024
Example 1
• Ti={1.0ms, 1.5ms, 2.5ms, 3.0ms}, Ei= 0.1ms
– Time quanta = Δt = 0.1 ms
– LCM of 10, 15, 25 and 30 is 150
– E1/T1 + E2/T2 + E3/T3 + E4/T4 = 0.24
• ScheduleFinder(10,15,25,30)
– Schedule Task A at times n*10
– Schedule Task B at times n*15 + j
– Schedule Task C at times n*25 + k
– Schedule Task D at times n*30 + l
– About (15)*(25)*(30)=11250 possible schedules (j,k,l)
Slide factors j=1,k=2,l=3 to minimize overlap (jitter=0):
abcd a b a c ab d a b a c ab d a
012345678901234567890123456789012345678901234567890123456789012345678901234
bc a5
Lecture ab d a cJ. Valvano,
b a A. Gerstlauer
ab d c a b a 41
567890123456789012345678901234567890123456789012345678901234567890123456789
ECE445M/ECE380L.12
Example 2
• Ti={0.4ms, 0.6ms, 1.0ms, 1.5ms}, Ei= 0.1ms
– Time quanta = 0.1ms, pattern repeats every 6ms
– E1/T1 + E2/T2 + E3/T3 + E4/T4 = 0.58
• ScheduleFinder(4,6,10,15)
– Schedule Task A at times n*4
– Schedule Task B at times n*6 + 1
– Schedule Task C at times n*10 + 1
– Schedule Task D at times n*15 + 14
– Jitter = 5
abC a ba cabd a bac ab ad baC ab ac baD ab c
0123456789012345678901234567890123456789012345678901
a ba d
23456789
J. Valvano, A. Gerstlauer 21