0% found this document useful (0 votes)
16 views51 pages

RT2021 Chap4

Chapter 4 of the document discusses the basic concepts of scheduling in real-time systems, including the need for scheduling, types of scheduling algorithms, and task constraints. It covers preemption, task parameters, and various classifications of scheduling algorithms such as static vs dynamic and preemptive vs non-preemptive. The chapter also addresses the complexity of scheduling problems, guarantee-based algorithms, and metrics for performance evaluation.
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)
16 views51 pages

RT2021 Chap4

Chapter 4 of the document discusses the basic concepts of scheduling in real-time systems, including the need for scheduling, types of scheduling algorithms, and task constraints. It covers preemption, task parameters, and various classifications of scheduling algorithms such as static vs dynamic and preemptive vs non-preemptive. The chapter also addresses the complexity of scheduling problems, guarantee-based algorithms, and metrics for performance evaluation.
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/ 51

Real-time Systems

Chapter 4:
Basic concepts of scheduling

Ngo Lam Trung


Dept. of Computer Engineering

NLT, SoICT, 2021


Contents
❑ Introduction of scheduling
❑ Constraints of real-time tasks
❑ Classification of scheduling algorithms
❑ Scheduling anomalies

NLT, SoICT, 2021


Introduction
❑ Why do we need scheduling?
There are always more tasks than processors.
Multiple tasks run concurrently on uniprocessor
system.

❑ Scheduling policy: the criterion to assign the


CPU time to concurrent tasks
❑ Scheduling algorithm: the set of rules that
determines the order in which tasks are
executed
→ What is the main difference between scheduling in
RTOS and GPOS?

NLT, SoICT, 2021


An illustration of scheduling
❑ All activated tasks enters “ready queue” at first.
❑ The scheduler selects one task in the Ready queue according to the
tasks’ priorities allocated based on the scheduling algorithm.
❑ The selected task is dispatched and becomes in “running” state.
❑ After the selected task is completed, it is removed from the Ready
queue.
Scheduler

Start/
dispatched terminate
activate
task Ready queue running

preempted
released Wait/blocked
Wait queues
NLT, SoICT, 2021
Preemption
❑ The running task can be interrupted at any point, so that a
more important task that arrives can immediately gain the
processor.
❑ The to-be-preempted task is interrupted and inserted to
the ready queue, while CPU is assigned to the most
important ready task which just arrived.
❑ Why preemption is needed in real-time systems?
Exception handling of a task
Treating with different criticalities of tasks, permits to anticipate
the execution of the most critical activities
Efficient scheduling to improve system responsiveness

NLT, SoICT, 2021


Notation of scheduling (1)
❑J = {J1,…,Jn} A set of tasks
❑ :R+→N A schedule
A function mapping from time to task to assign task to
CPU
If (t)=i for t[t1,t2), task Ji is executed during time
duration [t1,t2).
If (t)=0, the CPU is idle.

❑ Simple translation
CPU time is divided into time slices [t1,t2)
During a time slice (t)=const, representing the task
that is executed
NLT, SoICT, 2021
Notation
Notation of scheduling (2)
of scheduling(2)

Idle J1 J2 J3 idle

3
(t)
2
1 A time slice

t1 t2 t3 t4

Context switching are performed at these times


NLT, SoICT, 2021
Notation of scheduling (3)
❑ Preemptive schedule
A schedule in which the running task can be arbitrarily
suspended at any time, to assign the CPU to another
task

❑ Feasible schedule
A schedule that all tasks can be completed according to
a set of specified constraints

❑ Schedulable set of tasks


A set of tasks that has at least one feasible schedule by
some scheduling algorithm

NLT, SoICT, 2021


Example of preemptive schedule

J1
t
J2 t

J3 t

(t)

NLT, SoICT, 2021


Types of task constraints
❑ Timing constraints
❑ Precedence constraints
❑ Resource constraints

NLT, SoICT, 2021


Timing constraints
❑ Timing constraints:
Constraints on execution time, is the time that must
be meet in order to achieve the desired behavior.
Typical constraint: deadline
- Relative deadline: deadline is specified with respect to the
arrival time
- Absolute deadline: deadline is specified with respect to time
zero.

❑ Classification of real-time tasks


Hard : completion after deadline can cause
catastrophic consequence
Soft : missing deadline decreases the performance of
the system but does not jeopardize its correct
behavior
NLT, SoICT, 2021
Task parameters(1)

Li
Lateness
Ci

time
fi
Di relative deadline
Ci Execution
time

time
ai Si fi di
arrival time start time finishing absolute
time deadline

Xi Laxity
NLT, SoICT, 2021
Task parameters(2)
❑ Other task parameters
Response time: different between the finishing time and
the request time Ri = fi - ri
Criticality: Hard or Soft
Value vi: relative importance of task with respect to the
other tasks
Lateness: the delay of a task completion with respect to its
deadline Li = fi –di
Tardiness or Exceeding time: Ei = max(0,Li) is the time a
task stays active after its deadline.
Laxity or Slack time Xi = di – ai – Ci is the maximum time a
task can be delayed on its activation to complete within its
deadline

NLT, SoICT, 2021


Task parameters(3)
❑ Regularity of task activation
Periodic tasks: infinite sequence of identical activities
(jobs) activated at constant rate
Aperiodic: infinite sequence of identical activities
(jobs) with irregular activation
Sporadic: aperiodic tasks where consecutive
activation are separated by some minimum inter-
arrival time

NLT, SoICT, 2021


Periodic task and aperiodic task

NLT, SoICT, 2021


Precedence constraints
❑ Tasks can have precedence constraint:
A task has to be executed after another task is completed

❑ Notation:
Precedence relations are described by a directed acyclic
graph G.
: task Ja is a predecessor of task Jb
: task Ja is an immediate predecessor of Jb

NLT, SoICT, 2021


An example of precedence relations

To be executed
earlier
J1

J2 J3

J4 J5
To be executed
later

NLT, SoICT, 2021


Resource constraints
❑ Resource
Any software structure that can be used by the process
to advance its execution
Ex: data structure, a set of variables, main memory area,
a file, a piece of program, a set of registers of a
peripheral device

❑ Private resource:
A resource dedicated to a particular process

❑ Shared resource:
A resource that can be used by more tasks

NLT, SoICT, 2021


Shared resource & critical section

❑ Many shared resources do not allow


simultaneous access
→ require mutual exclusion

❑ Critical section
A piece of code under mutual exclusion constraints
Created by synchronization mechanism

❑ When tasks have resource constrains, they have


to be synchronized

NLT, SoICT, 2021


Example of data inconsistancy
❑ 𝜏𝑤 and 𝜏𝐷 share the
same buffer R.
❑ 𝜏𝑤 writes values to R,
𝜏𝐷 plots data from R.
❑ 𝜏𝑤 has lower priority
than 𝜏𝐷.
❑ Which values will be
plotted?

NLT, SoICT, 2021


Resource access with mutual exclusion
Higher priority Lower priority
J1 J2

Wait(s)
Wait(s)
Critical section Shared resource
R Critical section
Signal(s)
Signal(s)

Critical section is created by using the binary semaphore s

NLT, SoICT, 2021


Resource access with mutual exclusion
❑ Scheduling with preemption

preempts blocked

Higher priority
J1

Lower priority
J2

Locks resource Unlocks resource

normal execution critical section

NLT, SoICT, 2021


Waiting state caused by resource constraint

scheduling

activation Termination
READY RUN

preemption

Signal free Wait on busy


resource resource

WAITING

NLT, SoICT, 2021


Definition of scheduling problems
❑ Given
J = {J1,…,Jn}: A set of tasks
P = {P1,…,Pm}: A set of processors
R = {R1,…,Rr}: A set of resources
With precedence constraints and timing constraints

❑ Scheduling problem:
Assigning processors from P and resource from R to
tasks from J under given constraints

NLT, SoICT, 2021


Complexity of scheduling algorithm
❑ Complexity of scheduling decision problem
NP-complete in general
Has strong influence on the performance of dynamic
real-time systems

❑ Practical approach:
Simplify computer architecture: uniprocessor
Adopt additional conditions: preemptive model,
priority, task activation…
➔ Result in different classes of problem that can be
solved by different scheduling algorithms

NLT, SoICT, 2021


Classification of scheduling algorithms
❑ Preemptive
The running task can be interrupted at any time.

❑ Non-preemptive
A task, once started, is executed by the processor until
completion without interruption by any other tasks.

NLT, SoICT, 2021


Preemptive & non-preemptive algorithms

Higher priority task Lower priority task

<non-preemptive>

time
The arrival time of The arrival time of
the lower priority the higher priority
task task

The lower priority task is


preempted by the higher priority
<preemptive> task

time

NLT, SoICT, 2021


Classification of scheduling algorithms
❑ Static
Scheduling decisions are based on fixed parameters,
assigned to tasks before their activations.

❑ Dynamic
Scheduling decisions are based on dynamic parameters
that may change during system evolution.

NLT, SoICT, 2021


Static & dynamic algorithms

Task1: 1
Task2: 5 refers <Static>
Scheduler
Task3:2

dispatch
Fixed priorities

task Ready queue running

Dynamically Task1: 1 Scheduler <Dynamic>


determined Task2: 5
priorities Task3:2
dispatch

task Ready queue running

NLT, SoICT, 2021


Classification of scheduling algorithms
❑ Off-line
Scheduling decision is executed on the entire task
set before actual task execution.
❑ On-line
Scheduling decisions are taken at runtime every time
a new task enters the system or when a running task
terminates.

NLT, SoICT, 2021


Off-line & on-line algorithms
Task3 exe <Off-line>
Task1 exe refers
Task2 exe Scheduler Accepts large complexity
Small run-time overhead
dispatch Poor flexibility
Given schedule

task Ready queue running

<on-line>
Dynamically Scheduler
generates a Large run-time overhead
schedule
dispatch Good flexibility

task Ready queue running

NLT, SoICT, 2021


Classification of scheduling algorithms
❑ Optimal
(1) Minimizes some given cost function defined over
the task set.
(2) If no cost function is given, optimal algorithm only
fail to meet deadline only if no other algorithm can
meet the deadline.
❑ Heuristic
Searches for a feasible schedule using an objective
function (heuristic function).
Does not guarantee to find the optimal schedule.

NLT, SoICT, 2021


Guarantee-based algorithms
❑ In
hard real-time systems, feasibility of the
schedule should be guaranteed before task
execution.

❑ In order to guarantee feasibility:


Static real-time systems:
- Off-line scheduling is used. Requires high predictability, and
the system becomes inflexible.
Dynamic real-time systems:
- On-line scheduling with acceptance test

NLT, SoICT, 2021


Dynamic real-time schedule

scheduling
Activation Termination
Yes
ACCEPTANCE READY RUN
TEST

preemption
No Signal free Wait on busy
resource resource
WAITING

NLT, SoICT, 2021


Guarantee-based algorithms
❑ Acceptance test in on-line scheduling
For every arrival of new task Jnew, it is checked
whether J’=J  {Jnew} is schedulable or not.
If J’ is schedulabe, Jnew is accepted.
Otherwise, Jnew is rejected.
Generally based on worst-case assumption

❑ Demerit: unnecessary rejection


❑ Merit: detecting potential overload situations
Can avoid domino effects

NLT, SoICT, 2021


Domino effect
❑ A dangerous phenomena under transient overload
❑ The arrival of a new task causes all previously guaranteed
tasks to miss their deadlines.
New task
arrival
Jnew

J1

J2

J3

J4

NLT, SoICT, 2021


Best-effort algorithms
❑ In
soft real-time systems, some deadline
misses can be acceptable.
Ex. Multimedia applications

❑ Best-effort algorithms
Accepts all tasks that arrived
Aborts some tasks under real overload conditions
Cannot guarantee feasibility

❑ Demerit: domino effect


❑ Merit:
good average performance & avoiding
unnecessary rejection
NLT, SoICT, 2021
Metrics for performance evaluation

❑ Performance of scheduling algorithms is


evaluated by a cost function.
❑ Anoptimal scheduling algorithm generates a
schedule that minimizes a given cost function.
❑ Examples of cost functions
Average response time
Total completion time
Weighted sum of completion times
Maximum lateness
Maximum number of late tasks
Utility functions

NLT, SoICT, 2021


Cost functions table

NLT, SoICT, 2021


Example
d1 d2 d3 d4 d5

L1=3 L2=2 L3=1 L4=1 L5=2

J1 J2 J3 J4 J5

0 2 4 6 8 10 12 14 16 18 20 22 24 26 t

Lmax = L1 =3 Minimize Lmax

d1 d2 d3 d4 d5

L1=23 L2=-4 L3=-5 L4=-5 L5=-4

J2 J3 J4 J5 J1

0 2 4 6 8 10 12 14 16 18 20 22 24 26 t

Lmax = L1 =23 Only one task fails deadline


NLT, SoICT, 2021
Utility function
❑ A kind of cost function with respect to the completion
time of a task
❑ Evaluates lateness in a real-time task that depends on
the completion time

❑ Typical utility functions


Non real-time
Soft
On-time
Firm

❑ To evaluate whole schedule, the cumulative value of


utility function values of all tasks is used.

NLT, SoICT, 2021


Examples of cost functions

v(fi) v(fi)

Non real - time soft

(a) fi (b) di fi
v(fi)
v(fi)

on time
firm

di fi di
(c) (d)

NLT, SoICT, 2021


Scheduling anomalies
❑ Real-time computing  fast computing
❑ Increaseof computing power  improvement of
the performance
❑ Scheduling
is brittle: small changes can have big
unexpected consequences!
❑ Graham’s theorem
If a task set is optimally scheduled on a multiprocessor
with some priority assignment, a fixed number of
processors, fixed execution times, and precedence
constraints, then increasing the number of processors,
reducing execution times, or weakening the precedence
constraints can increase the schedule length.

NLT, SoICT, 2021


Examples of Graham’s theorem
❑J = {J1,…,J9}: A task set
Sorted by decreasing priorities
Has the precedence constraints in next slide

❑ Three processors
❑ Optimal schedule *
in Figure next slide
Global completion time is 12

NLT, SoICT, 2021


Task precedence constraints

J1 (3) J9 (9)

J8 (4)
J2 (2)

J7 (4)
J3 (2)

J6 (4)

J4 (2) J5 (4)

priority(Ji) > priority(Jj) for all i < j

NLT, SoICT, 2021


Optimal schedule

P1 J1 J9
P2 J2 J4 J5 J7
P3 J3 J6 J8
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 t

optimal schedule of task set J on a three-processor machine

➔ Find the global completion time if


• Extra processors are added
• Tasks execution time are reduced
• Precedence constrain is weakened

NLT, SoICT, 2021


Examples of Graham’s theorem(2)
❑ Increasing the number of processors
Global completion time is 15

P1 J1 J8
P2 J4 J5 J9
P3 J2 J6
P4 J3 J7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 t

Schedule of task set J on a four-processor machine

NLT, SoICT, 2021


Examples of Graham’s theorem(3)
❑ Reducing computation time
Global completion time is 13

P1 J1 J5 J8
P2 J2 J4 J6 J9
P3 J3 J7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 t

Schedule of task set J on a three-processor machine with


computation times reduced by one unit of time

NLT, SoICT, 2021


Examples of Graham’s theorem(4)

❑ Weakening precedence constraints


Global completion time is 16
J1 (3) J9 (9)

J8 (4)
J2 (2)

J7 (4)
J3 (2)
J6 (4)

J4 (2) J5 (4)

Precedence graph of task set J obtained by removing the constraints on


task J5 and J6

NLT, SoICT, 2021


Examples of Graham’s theorem(4)

❑ Weakened precedence constraints


Global completion time is 16

P1 J1 J8 J9
P2 J2 J4 J5
P3 J3 J7 J6
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 t

Schedule of task set J on a three-processor machine with


precedence constraints weakened

NLT, SoICT, 2021


Examples of Graham’s theorem(5)
❑ Anomalies under resource constrains
In real-time tasks with shared resource
Global completion time is increased by reducing the computation
time.

J1 J2

J3 J4 J5 tc = 17

0 2 4 6 8 10 12 14 16 18 20 22 24 26 t

J1 J2

J3 J4 J5 tc = 22

0 2 4 6 8 10 12 14 16 18 20 22 24 26 t

NLT, SoICT, 2021

You might also like