Ece 367 09 13
Ece 367 09 13
Operating Systems
(ECE 367)
Dr. Rajiv Ranjan Singh
Professor
Department of Electronics and Communication
(ECE) Engineering
Lecture – 09-13
Real-Time Scheduling
2
Outline (Lecture-09-13)
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 3
Real-Time Scheduling
Real-time tasks => Will have associated constraints
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 4
Important Concepts and Terminologies
Task Instance
Each time a task recurs => task instance
First occurrence of a task => first instance
Second occurrence of a task => second instance………and so on
The jth instance of a task Ti is denoted as => Ti (j)
Each instance of a task is associated with a deadline
Task instances Processes
Relative Deadline => time interval between start (arrival) of the task and the
corresponding deadline.
11 May 2021 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh
Important Concepts and Terminologies
Relative Deadline vs Absolute Deadline
11 May 2021 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh
Important Concepts and Terminologies
Response Time
Time duration from the occurrence of the event generating the task to the time
task produces its results
For Hard Real-Time Tasks need not be completed early as long as deadlines
are met
For Soft Real-Time Tasks average response time is an important metric to
measure the performance of a scheduler.
Task Precedence
A Task is said to precede another task if, the first task completes before the
second task starts.
When a task Ti precedes another task Tj, then each instance of Ti precedes the
corresponding instance of Tj.
If T1 precedes T2, then T1(1) precedes T2(1) and T1(2) precedes T2(2), and so on
11 May 2021 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh
Important Concepts and Terminologies
Task Precedence
In the fig shown, T1 precedes T2, but we can not relate T1 with either T3 or T4.
Data Sharing
Tasks often share data between each other => requires precedence
A Task may precede another task even if, no data sharing happens.
Data sharing may occur between concurrent tasks as well as overlapping tasks
to => data sharing can not be represented using precedence relation.
In the fig shown, T2 uses results of T3 (shown using dashed line) but T2 and T3
may execute concurrently.
T2 may even start executing first, after sometime it may receive some data from
T3 , and continue its execution, and so on.
11 May 2021 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh
Important Concepts and Terminologies
Periodic Task
A task that repeats after a certain fixed time interval (demarcated by clock
interrupts) => also referred as clock-driven tasks
Period of a task = the fixed time interval after which the task repeats
11 May 2021 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh
Important Concepts and Terminologies
Periodic Task
Consider the track correction task found in a rocket control software
track correction task starts at 2000ms after the launch of the rocket and recurs
every 50 ms
Each instance of a task requires a processing time of 8 ms and its relative
deadline is 50 ms
Ti (1) = (𝚽𝒊 , 𝒑𝒊 , 𝒆𝒊 , 𝒅𝒊 )
11 May 2021 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh
Important Concepts and Terminologies
Periodic Task
Ti = (𝚽𝒊 , 𝒑𝒊 , 𝒆𝒊 , 𝒅𝒊 )
𝚽𝒊 = phase of the task 2000 ms
Ti can be formally represented as Ti = (2000 ms, 50ms, 8ms, 50ms)
when 𝒑𝒊 = 𝒅𝒊 , the fourth tuple can be omitted, hence we can write
Ti = (2000 ms, 50ms, 8ms) => 𝒑𝒊 = 𝒅𝒊 = 50 ms
11 May 2021 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh
Important Concepts and Terminologies
Periodic Task
A vast majority of tasks in a real-time system are periodic in nature.
Monitoring certain conditions
Polling information from sensors at regular intervals => like in chemical
plants
11 May 2021 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh
Important Concepts and Terminologies
Sporadic Task
Recurs at random instants
Represented by Ti = (𝒆𝒊 , 𝒈𝒊 ,𝒅𝒊 )
𝒆𝒊 => worst case execution time of an instance of a task
𝒈𝒊 => minimum separation between two consecutive instances of the task
𝒅𝒊 => relative deadline of the task
𝒈𝒊 => once an instance of a sporadic task occurs, the second can not occur
before 𝒈𝒊 time units have elapsed.
𝒈𝒊 restricts the rate at which the sporadic task can arise.
First instance of a sporadic task Ti is denoted by Ti (1)
Successive instances by Ti (2), Ti (3) etc.
Many sporadic tasks are highly critical in nature => task handling fire in factory
Highly critical => task handling fire in factory
Moderately critical => I/O device interrupt or DMA interrupt etc.
11 May 2021 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh
Important Concepts and Terminologies
Aperiodic Task
Similar to sporadic tasks in many ways
Can arise at random instants
𝒈𝒊 => minimum separation between two consecutive instances could be
‘0’
Two or more instances can occur at the same time instant
Deadline could be expressed either as average value or statistically
Generally soft real-time tasks, as they can recur in quick succession =>
few deadline misses may be noticed as difficult to meet the deadlines of
all instances
E.g. logging task in a distributed system (can be started different tasks
running at different nodes). Logging requests from different tasks may
arrive at the same time at the logger, or they may be spaced out in time
Other examples – operator requests, keyboard presses, mouse
movements etc.
11 May 2021 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh
Task Scheduling
Real-Time Task Scheduling
Refers to the order in which the various tasks are to be taken up for
execution by the operating system (OS).
11 May 2021 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh
Task Scheduling: Terminologies
Valid Schedule
At most one task is assigned to a processor at time
No task is scheduled before its arrival time
Precedence and resources constraints of all tasks are satisfied
Feasible Schedule
A valid schedule is called feasible, only if all tasks meet their respective time
constraints in the schedule
Proficient Scheduler
A task scheduler sch1 is said to be more proficient than another scheduler sch2,
if sch1 can feasibly schedule all task sets that sch2 can feasibly schedule, but
not vice versa.
A sch1 can feasibly schedule all task sets that sch2 can feasibly schedule and
vice versa, then sch1 and sch2 are called equally proficient schedulers.
11 May 2021 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh
Task Scheduling: Terminologies
Optimal Scheduler
A task scheduler which can feasibly schedule any task set that can be feasibly
scheduled, by any other scheduler.
An optimal scheduler can not schedule some task set, then no other scheduler
should be able to produce a feasible schedule for that task set.
Scheduling Points
Scheduling Points of a scheduler are the points on time line at which the
scheduler makes decisions regarding which task is to be run next.
A task scheduler need not run continuously, as it is activated by the OS only at
scheduling points to make the scheduling decisions as to which task is to be run
next.
Clock-driven scheduler => defined at time instants marked by interrupts
generated by a periodic timer.
Event-driven scheduler => determined by occurrence of certain events.
11 May 2021 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh
Task Scheduling: Terminologies
Preemptive Scheduler
On arrival of a higher priority task, the scheduler suspends the lower priority task
that may be executing.
A preempted lower priority task can resume its operation only when no higher
priority task is ready.
Utilization
Processor utilization (or simply utilization) of a task is the average time for which
it executes per unit time interval.
𝒆
For a periodic task Ti, the utilization is denoted as 𝒖𝒊 = 𝒑𝒊
𝒊
𝒆𝒊 = execution time
𝒑𝒊 = period of Ti,
For a set 𝒆of periodic task {Ti}, the total utilization due to all tasks is given as
𝑼 = 𝒏𝒊=𝟏 𝒑𝒊
𝒊
Objective of a good scheduling algorithm => feasibly schedule those task that have
task utilization = 1 (high utilization)
On Uniprocessors tasks having utilization more than 1 can not be scheduled properly.
11 May 2021 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh
Task Scheduling: Terminologies
Jitter
It is the deviation of a periodic task from its strict periodic behavior.
Arrival Time Jitter => deviation of a the task from arriving at the precise
periodic time of arrival. Caused may be due to imprecise clocks or
factors like network congestions etc.
11 May 2021 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh
Scheduling Algorithms
Scheduling Algorithms
Above Real-Time Task Scheduling Algorithms => based on how scheduling points are defined
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 20
Scheduling Algorithms
Scheduling Algorithms
(based on type of task acceptance test )
Planning Best
based Effort
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 21
Scheduling Algorithms
Scheduling Algorithms
(based on target platform on which tasks are to be run )
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 22
Clock Driven Scheduling
Also known as offline schedulers =>
a schedule is computed off-line and stored for use at runtime (i.e. fix the
schedule before the system starts to run)
determines which job to execute when
all the parameters of hard RT jobs are fixed and known.
Scheduling decisions taken based on clock interrupt points
Scheduling points determined by timer interrupts
Advantage: Incur very little run time overhead
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 23
Clock Driven Scheduling
Make scheduling decisions at regularly spaced time => frequently adopted
choice
Use a hardware timer => makes scheduling decisions periodically
(scheduler is invoked by a timer)
The timer is set to expire periodically without the intervention of the
scheduler.
When the system is initialized, the scheduler selects and schedules the
job(s) that will execute until the next scheduling decision time and then
blocks itself waiting for the expiration of the timer.
When the timer expires, the scheduler awakes and repeats these actions.
static or cyclic
predictable and deterministic
multiple tables for different operation modes
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 24
Table-Driven Scheduling
Usually precomputed which task would run when and store this
schedule in a table at the time the system is designed or
configured.
Application programmer selects his own schedule for the set of
tasks in the application and store the schedule in a schedule
table, used by the scheduler at run time.
Consider the table shown =>
How to decide size of the schedule table? => Important
Task Start Time in msec
If a set ST = {Ti} of n tasks is to be T1 0
scheduled, then the entries will be
given as LCM(p1, p2, p3,…pn) duration T2 3
in the schedule table T3 10
T4 12
T5 17
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 25
Table-Driven Scheduling
Example 1: Consider the following example
T1 = (e1 = 5 ms, p1 = 20 ms)
T2 = (e2 = 20 ms , p2 = 100 ms)
T3 = (e3 = 30 ms, p3 = 250 ms)
Then, the schedule will repeat after every 500 ms. That is, LCM
of (20, 100, 250), with an assumption that Φi = 0.
LCM(p1, p2, p3,…pn) is called the major cycle of the set of tasks
ST.
A major cycle of a set of tasks is an interval of time
on the timeline such that in each major cycle, the
different tasks recur identically.
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 26
Table-Driven Scheduling
Theorem 1: The major cycle of a set of tasks ST = LCM(T1, T2,
T3,…Tn) is LCM(p1, p2, p3,…pn), even when the tasks have
arbitrary phasings.
Consider the figure shown below for a task Ti in the major cycle
First occurrence of Ti starts Φ time units from the start of major
cycle
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 27
Table-Driven Scheduling
Theorem 1: Cont…
The major cycle ends x time units after the last (i.e. (k-1)th)
occurrence of Ti in the major cycle => must be same for each
major cycle
If M is size of each major cycle then for the task to repeat in
each major cycle => 𝑀 = 𝑘 − 1 𝑝𝑖 + Φ + 𝑥…..( I )
For Ti to have identical occurrence time in each major cycle, we
should have Φ + 𝑥 = 𝑝𝑖 , substituting in Eqn. ( I ), we get
𝑀 = 𝑘 − 1 𝑝𝑖 + 𝑝𝑖 = k*𝑝𝑖 => major cycle M contains an integral
multiple of 𝑝𝑖 , holds for each task in the major cycle.
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 28
Table-Driven Scheduling
Example 2: Consider the following example
We have to schedule the activities of a car control system. Let
C = e = worst case execution time
T = p (sampling) period
D = d = deadline
Speed measurement: C=4ms, T=20ms, D=20ms
ABS control: C=10ms,T=40ms, D=40ms
Fuel injection: C=40ms,T=80ms, D=80ms
Other software with soft deadlines e.g. audio, air condition etc.
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 29
Table-Driven Scheduling
Example 2: Cont…
(ms)
Speed
ABS
Fuel Injection
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 30
Cyclic Schedulers
Very popular in industry
Larger majority of all small embedded applications
manufactured presently => cyclic schedulers
Simple, efficient and easy to program
E.g. => Temperature Controller (used in computer-controlled air
conditioners) which periodically samples the temperature of a
room and maintains it at a preset value.
Repeats a precomputed schedule => to be stored only for one
major cycle
Each task in the task set (to be scheduled) repeats identically
in every major cycle
Major cycle is divided into one or more minor cycles
Each minor cycle is called a frame
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 31
Cyclic Schedulers
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 32
Cyclic Schedulers
Task Number Frame Number
T3 F1
Schedule table => T1 F2
T3 F3
T4 F2
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 33
Cyclic Schedulers
1. Minimum Context Switching (Cont…):
Select a frame size larger than the execution time of each task
to avoid unnecessary task switches
When a task starts at a frame boundary, it should be able to complete
within the same frame
The constraint is expressed as: max 𝑒𝑖 ≤𝐹
Where, 𝑒𝑖 = execution time of task 𝑇𝑖 and F = frame size
This constraint imposes a lower bound on frame size i.e. F should not be
smaller than max 𝑒𝑖 .
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 34
Cyclic Schedulers
3. Satisfaction of Task Deadline:
Constraint: Between the arrival of a task and its deadline, there exist at
least one full frame (as shown in the figure below)
(a) (b)
Let a task arrives after ∆𝑡 time units have passed since the last frame, Fig (b)
The task can complete before its deadline iff 2𝐹 − ∆𝑡 ≤ 𝑑𝑖 or 2𝐹 ≤ 𝑑𝑖 + ∆𝑡
min(∆𝑡) = minimum separation from the start of frame => worst case scenario
to miss a deadline by the task = gcd 𝐹, 𝑝𝑖 .
The constraint is written as for every 𝑇𝑖 : 2𝐹 − gcd 𝐹, 𝑝𝑖 ≤ 𝑑𝑖 ; for one task
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 35
Cyclic Schedulers
3. Satisfaction of Task Deadline (Cont…):
The constraint defines an upper bound on frame size for a task .
A task may miss its deadline if the frame size is any larger than the defined
upper-bound.
max gcd 𝐹,𝑝𝑖 +𝑑𝑖
Hence for all tasks, the frame size must be smaller than 2
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 36
Cyclic Schedulers
Example 3: A cyclic scheduler is to be used to run the following
set of periodic tasks on a uniprocessor where
T1 = (e1 = 1, p1 = 4) 1. max{ei} <= F
T2 = (e2 = 1, p2 = 5) F=2
T3 = (e3 = 1, p3 = 20)
2. Find Major Cycle = LCM (4, 5, 20, 20) =
T4 = (e4 = 2, p4 = 20) 20
Select an appropriate frame size
F can take values of
For F = 2 for all tasks T1 – T4 = Satisfied 2, 4, 5, 10, 20
For F = 4; Constraint is not satisfied
For F= 5; Constraint is not satisfied 2F –gcd(F, pi) <= di
For F = 10; Constraint is not satisfied
For F = 20; Constraint is not satisfied For F = 2 and Task T1
Hence only frame size of 2 is suitable for scheduling 2x2 – gcd(2,4) <= 4
4 – 2 =2 <= 4 => For p1 the selection is
satisfied….
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 37
Cyclic Schedulers
Example 4: A cyclic scheduler is to be used to run the following
set of periodic tasks on a uniprocessor where
T1 = (e1 = 1, p1 = 4)
T2 = (e2 = 2, p2 = 5)
T3 = (e3 = 5, p3 = 20)
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 38
Cyclic Schedulers
Example 4: A cyclic scheduler is to be used to run the following
set of periodic tasks on a uniprocessor where
T1 = (e1 = 1, p1 = 4)
T2 = (e2 = 2, p2 = 5)
T3 = (e3 = 5, p3 = 20)
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 39
A Generalized Task Scheduler
Many practical applications consists of a mixture of several
periodic, aperiodic and sporadic tasks.
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 40
A Generalized Task Scheduler
Sporadic task is scheduled only if enough slack time available
for the arriving sporadic task before its deadline
Sporadic tasks are subjected to acceptance test on their
arrival => to check whether the task will be completed within
deadline (in the available slack times)
If not possible => rejected and corresponding recovery routines are run
Aperiodic tasks can be taken up for scheduling without
acceptance test
since they do not have strict deadlines
Best effort is made to complete them within the available slack times,
but no guarantee is assured for the completion time
Store the slack times in a table during acceptance test for
schedulability
Accept both sporadic and aperiodic tasks without acceptance
test => make best effort to meet respective deadlines
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 41
Generalized Scheduler Pseudo Code
cyclic-scheduler () {
current-task T = Schedule-Table [k];
k = k + 1;
k = k mod N; // N = total no of tasks in the schedule table
dispatch-current-task(T);
schedule-sporadic-tasks( ); // Current task T completed early, sporadic task
; // can be taken up
schedule-aperiodic-tasks( ); // At the end of the frame, the running task is
; // preempted, if not complete
Idle ( ); // No task to run, idle
} Assumptions:
Precomputed schedule for a periodic task is stored in a schedule table.
Sporadic task those passed the acceptance test has been kept for
scheduling, if possible
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 42
Cyclic vs Table-driven Schedulers
Cyclic Table-driven
Set a periodic timer only once at the A timer has to be set every time a task starts
application initialization time, which give an to run. A call to a timer is made every few
interrupt exactly at every frame boundary milliseconds which represents a overhead.
Since size of the frame that needs to be If overhead of setting a timer is ignored
chosen should be at least as long as the size then, table-driven is more efficient.
of the largest execution time of a task in the
task set. This is a source of inefficiency since
this results in processor time being wasted
in case of those tasks whose execution times
are smaller than the chosen frame size.
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 43
Clock-driven Schedulers
Example 5: Consider a real-time system which consists of three
tasks as shown in the table. If the tasks are to be scheduled
using a table driven scheduler, what is the length of time for
which the schedules have to be stored in the precomputed
schedule table of the scheduler?
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 44
Cyclic Schedulers
Example 6: A cyclic real-time scheduler is to be used to
schedule three periodic tasks as shown in the table. Suggest a
suitable frame size that can be used. Show all intermediate
steps in your calculations.
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 45
Cyclic Schedulers
Example 7: Consider the following three set of independent
real-time periodic tasks shown in the table below. Suppose a
cyclic scheduler is to be used to schedule the task set. What is
the major cycle of task set? Suggest a suitable frame size and
provide a feasible schedule (task to frame assignment for a
major cycle) for the task set.
Task Phase Processing Period Deadline
(msec) Time (msec) (msec)
(msec)
T1 20 25 150 100
T2 40 10 50 30
T3 60 50 200 150
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 46
Hybrid Schedulers
Clock-driven Schedulers => scheduling points defined through
clock interrupts.
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 47
Time-Sliced Round Robin Schedulers
This is a preemptive scheduling methods.
The ready tasks are held in circular queue and are taken up
one after another in a sequence from the queue.
Once a task is taken up, it runs for a certain fixed interval of
time called its time slice.
Each process is given a time slice to run in
When the time slice expires, the next process preempts the current
process, and runs for its time slice, and so on
The preempted process is placed at the end of the queue
If a task does not complete within its allocated time slice, it is inserted
back into the circular queue.
Implemented with
A ready queue
A regular timer interrupt
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 48
Time-Sliced Round Robin Schedulers
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 49
Time-Sliced Round Robin Schedulers
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 50
Time-Sliced Round Robin Schedulers
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 51
Time-Sliced Round Robin Schedulers
Pros
Fair, easy to implement
Con
Assumes everybody is equal
Issue: What should the time slice be?
Too short
Waste a lot of time switching between processes
Example: time slice of 4 ms with 1 ms context switch = 20% round robin
overhead
Too long
System is not responsive
Example: time slice of 100ms
If 10 people hit “enter” key simultaneously, the last guy to run will only see
progress after 1 second.
Degenerates into First Come First Serve (FCFS) if time slice longer than
burst length
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 52
Event-Driven Schedulers
Event Driven
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 53
Event-Driven Schedulers
Event-driven schedulers are less suitable for embedded
applications, as they should be of
Small size
Low cost
Consume less power
Can handle aperiodic and sporadic tasks proficiently
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 54
Foreground-Background Schedulers
It is the simplest priority-driven preemptive scheduler
Real-Time tasks run as foreground tasks
Foreground tasks => highest priority tasks run at every
scheduling points
Background tasks run at lowest priority => when no
foreground task running
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 55
Foreground-Background Schedulers
When any foreground task is executing, the background task
waits.
The average CPU utilization due to the foreground task Ti is
𝑒
equal to 𝑖 , since ei amount of processing is required over
𝑝𝑖
every pi period.
All foreground tasks together would result in CPU utilization
𝑒
of 𝑛𝑖=1 𝑖
𝑝𝑖
Therefore, the average time available for execution of the
𝑒
background tasks in every unit of time is 1 - 𝑛𝑖=1 𝑖
𝑝𝑖
Hence, the completion time for the background task is given
𝑒𝐵
by 𝑐𝑡𝐵 =
1 − 𝑛𝑖=1𝑝𝑒𝑖
𝑖
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 56
Foreground-Background Schedulers
Example 8: Consider a real-time system in which tasks are
scheduled using foreground-background scheduling. There is
only one foreground task
𝑻𝒇 = (𝚽𝒇 = 𝟎, 𝒑𝒇 = 𝟏𝟎𝟎 𝒎𝒔𝒆𝒄, 𝒆𝒇 = 𝟓𝟎 𝒎𝒔𝒆𝒄, 𝒅𝒇 = 𝟏𝟎𝟎 𝒎𝒔𝒆𝒄),
and the background task be 𝑻𝑩 = (𝒆𝑩 = 𝟏𝟎𝟎𝟎 𝒎𝒔𝒆𝒄). Compute
the completion time for the background task.
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 57
Foreground-Background Schedulers
Example 8: Consider a real-time system in which tasks are
scheduled using foreground-background scheduling. There is
only one foreground task
𝑻𝒇 = (𝚽𝒇 = 𝟎, 𝒑𝒇 = 𝟏𝟎𝟎 𝒎𝒔𝒆𝒄, 𝒆𝒇 = 𝟓𝟎 𝒎𝒔𝒆𝒄, 𝒅𝒇 = 𝟏𝟎𝟎 𝒎𝒔𝒆𝒄),
and the background task be 𝑻𝑩 = (𝒆𝑩 = 𝟏𝟎𝟎𝟎 𝒎𝒔𝒆𝒄). Compute
the completion time for the background task.
Solution:
𝑒𝐵 1000
𝑐𝑡𝐵 = 𝑛 𝑒𝑖
= 50 𝑚𝑠𝑒𝑐 = 2000 𝑚𝑠𝑒𝑐
1 − 𝑖=1𝑝 1−100
𝑖
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 58
Foreground-Background Schedulers
Example 9: In a simple priority-driven preemptive scheduler, two
periodic tasks T1 and T2 and a background tasks are scheduled.
The periodic tasks T1 has the highest priority and executes once
every 20 msec and requires 10 msec of execution time each
time. T2 requires 20 msec of processing every 50 msec. T3 is a
background task and requires 100 msec to complete. Assuming
that all the tasks start at time 0, determine the time at which T3
will complete.
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 59
Foreground-Background Schedulers
Example 9: Given
T1 = (e1 = 10 ms, p1 = 20 ms)
T2 = (e2 = 20 ms, p2 = 50 ms)
T3 = (e3 = 100 ms) => background task
Solution:
2 𝑒𝑖 10 20 90
The total utilization due to foreground task 𝑖=1 𝑝 = + =
𝑖 20 50 100
That is, fraction of time remaining for the background task to
𝑒 90 10 1
execute is given by 1 − 𝑛𝑖=1 𝑖 = 1 - = =
𝑝𝑖 100 100 10
=> background task gets 1 ms every 10 ms .
10
=> So background task 𝑻𝟑 would take 100 ∗ = 1000 ms to
1
complete.
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 60
Foreground-Background Schedulers
Example 10: Assume that in example 8 an overhead of 1 ms on
account of every context switch is to be taken into account.
Compute the completion time of TB.
The very first time the foreground task runs (at t = 0), it incurs a
context switch overhead of 1 ms. In every preempt cycle it incurs
one context switch.
The background task runs after every instance of the foreground
tasks and incurs one context switch too => the execution time of
every foreground task is increased by two context switch times
i.e. from 50 ms to 52 ms.
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 61
Foreground-Background Schedulers
Example 10:
Therefore the completion time of TB.
𝑒𝐵 1000
𝑐𝑡𝐵 = 𝑛 𝑒𝑖
= 𝑚𝑠𝑒𝑐 = 2083.4 𝑚𝑠𝑒𝑐
1− 𝑖=1𝑝 1−
52
100
𝑖
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 62
Priorities
Each Process (or thread) is associated with a priority
Provides basic mechanism to influence a scheduler
decision:
Scheduler will always chooses a thread of higher priority over lower
priority
Priorities can be defined internally or externally
Internal: e.g. I/O bound or CPU bound
External: e.g. based on importance to the user
CPU-Bound process I/O-Bound process
Spends most of its computing Spend most of its time waiting for I/O to complete
Small bursts of CPU to process I/O and request
next I/O
Time to completion largely determined Time to completion largely determined by I/O request
by received CPU time time
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 63
Priorities: Example
Consider 5 Jobs as shown
Job number equals priority
Priority 1 > priority 5
Release and execution
times as shown
Priority-driven preemptively
scheduled
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 64
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 65
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 66
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 67
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 68
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 69
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 70
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 71
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 72
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 73
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 74
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 75
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 76
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 77
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 78
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 79
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 80
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 81
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 82
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 83
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 84
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 85
Priorities: Example
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 86
Online Scheduling
11 May 2021 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh
Schedulability Test
Test to determine whether a feasible schedule exists
Sufficient Test
If test is passed, then tasks are definitely schedulable
If test is not passed, tasks may be schedulable, but not necessarily
Necessary Test
If test is passed, tasks may be schedulable, but not necessarily
If test is not passed, tasks are definitely not schedulable
Exact Test (= Necessary + Sufficient)
The task set is schedulable if and only if it passes the test.
11 May 2021 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh
Earliest Deadline First (EDF) Scheduling
In Earliest Deadline First (EDF) Scheduling, at every
scheduling point the task with shortest deadline is taken up
for scheduling.
A task is schedulable under EDF if and only if it satisfies the
condition that the total processor utilization due to the task
set is less than 1.
For a set of periodic tasks {T1, T2, T3, …., Tn}, the EDF
schedulability criterion is given as:
𝑛 𝑒𝑖 𝑛
𝑖=1 =
𝑝𝑖 𝑖=1 𝑢𝑖 ≤ 1 => pi of each task same as di (assumption)
11 May 2021 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh
Earliest Deadline First (EDF) Scheduling
EDF is optimal scheduling algorithm => if a set of tasks is
unschedulable under EDF, then no other scheduling
algorithm can feasibly schedule this task set.
In practical problems the period pi of a task may be different
than its deadline di => schedulability test needs to be
changed.
If pi > di => each task needs ei amount of computing time
every min(pi, di) duration of
𝑒
time, therefore the expression
could be written as 𝑛𝑖=1 𝑖
≤1
min(𝑝𝑖 , 𝑑𝑖 )
If pi < di => it is possible that a set of task is EDF
schedulable, even when the task set fails to meet the
expression as above. For this case, the above condition is
not a necessary condition but only a sufficient condition for a
task to be EDF schedulable.
11 May 2021 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh
EDF Scheduling
Example 11: Given three periodic real-time tasks to be
scheduled using a EDF on a uniprocessor.
T1 = (e1 = 10 ms, p1 = 20 ms)
T2 = (e2 = 5 ms, p2 = 50 ms)
T3 = (e3 = 10 ms, p3 = 35 ms)
Determine whether the task set is schedulable
Solution:
The total utilization due to foreground task
3 𝑒𝑖 10 5 10
𝑖=1 𝑝 = + + =0.89 < 1 => the task set is EDF schedulable
𝑖 20 50 35
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 91
Minimum Laxity First (MLF) Scheduling
Variant of EDF
At every scheduling point, a laxity value is computed for every
task in the system, and the task having minimum laxity is
executed first.
Laxity of a task measures the amount of time that would
remain if the task is taken up for execution next.
Unlike EDF, MLF considers the execution time of a task =>
measure of flexibility available for scheduling
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 92
Implementation of EDF
Implementation 1:
Maintain all tasks in a queue that are ready for execution
Insert any freshly arriving task at the end of queue
Every node in the queue would contain the absolute deadline
of the task.
Scan the entire queue from beginning at the every preemption
point to determine the task having the shortest deadline
However, this implementation is inefficient because:
Each task insertion would be achieved in O(1) or constant time
But the task selection (to run next) and its deletion would require O(n)
time, where ‘n’ is the no of tasks in the queue.
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 93
Implementation of EDF
Implementation 2:
Maintain all ready tasks in a sorted priority queue (can be
efficiently implemented using a heap data structure)
In priority queue, tasks are always kept sorted according to the
proximity of their deadline.
Insert a record of any freshly arriving task into the heap in
O(log2n) time, where ‘n’ is the no of tasks in the priority queue.
At every scheduling point, the next task to be run can be found
at the top of the heap.
When a task is taken up for scheduling, it needs to be
removed from priority queue which could be achieved in O(1)
time.
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 94
Implementation of EDF
Implementation 3:
Assume that the no of distinct deadlines that the tasks can
have in an application are restricted.
Whenever task arrives, its absolute deadline is computed from
its release time and its relative deadline.
A separate FIFO queue is maintained for each distinct relative
deadline that tasks can have.
The scheduler inserts a newly arrived task at the end of the
corresponding relative deadline queue => tasks are ordered
according to their absolute deadlines.
To find a task with the earliest absolute deadlines => scheduler
only searches the threads of all FIFO queues.
If Q = no of priority queues maintained, then the order of
searching as well as time to insert a task would be O(1).
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 95
Shortcomings of EDF
Transient Overload Problems:
Transient overload occurs when some tasks take more time to
complete than what was planned originally at the design time.
Enter an infinite loop
Encounter an unusual condition
Enter a rarely used branch due to some abnormal input values
A task overshooting its completion time can cause some
task(s) to miss their deadlines.
Usually very difficult to predict which task might miss
deadlines.
Only prediction possible => the task(s) that would run
immediately after the tasks causing the transient overload
would get delayed and might miss its (their) deadlines.
EDF can’t guarantee about deadline misses => big drawback
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 96
Shortcomings of EDF
Resource Sharing Problems:
While EDF is used for scheduling real-time tasks => high
overheads might have to be incurred to support resource
sharing among the tasks without making tasks to miss their
deadlines
Efficient Implementation Problems:
It is difficult to restrict the no of tasks with distinct deadlines to a
reasonable number.
The efficient implementation that achieves O(1) overhead assumes that
the no of relative deadlines is restricted => sometimes unacceptable.
The priority queue implementation [which uses O(log2n)] also creates
high runtime overhead, since most real-time tasks are periodic with
small periods and strict deadlines.
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 97
Next Lecture
RMA Scheduling
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 98
Bibliography
K.C. Wang, “Embedded and Real-Time Operating Systems,” Springer
International Publishing, 2017.
Rajib Mall, “Real Times Systems: Theory and Practice,” Pearson Education.
Jonathan W. Valvano, “Embedded Systems: Real-Time Operating Systems for
Arm® Cortex™-M Microcontrollers,” CreateSpace Independent Publishing
Platform.
Wolf, Wayne, Computers as Components – Principles of Embedded Computing
System Design, Second Edition, Morgan-Kaufmann, 2008.
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh 99
.
Thank You!
©These lecture slides have been prepared for the benefits of the registered students of the course
Embedded Real Time Operating Systems (ECE 367) of Presidency University, Bengaluru. Anybody
who may wish to use these slides for educational purpose could use it without any explicit permission. Any
kind of redistribution, alteration, and commercial use of this material is strictly prohibited.
08 February 2020 Embedded Real Time Operating Systems (ECE 367) @Dr. Rajiv Ranjan Singh