real time systems and algorithms
DESCRIPTION
Algorithms and Introduction to Real Time Systems.TRANSCRIPT
04/18/2023 Real Time Systems 1
REAL TIME SYSTEMS
04/18/2023 Real Time Systems 2
Real Time System
• Those systems in which the correctness of the system depends not only on the logical result of computation, but also on the time at which the results are produced.
– Example – Flight Control System• All tasks in that system must execute on time
04/18/2023 Real Time Systems 3
And Few More Examples…
• Chemical and nuclear plant control• Control of complex production processes• Railway reservation systems• Automotive applications• Environmental acquisition and monitoring• Telecommunication systems• Industrial automation• Robotics• Military systems• Space missions
04/18/2023 Real Time Systems 4
Real Time System
• In General: any system where a timely response by the computer to external stimuli is vital
• Timely » Deadline
• Hard/soft RTS
04/18/2023 Real Time Systems 5
Soft Vs Hard RTS!!
• Soft real-time system : If the consequences consist of a degradation of performance, but not failure
– e.g.- time adjusting system on computers over the network, video game etc..
• Hard real-time system : If the consequences are system failure and catastrophic result
– e.g. emergency patient management system in hospitals
04/18/2023 Real Time Systems 6
RTS CHARACTERISTICS
• Time constraints : Deadlines
• New Correctness Criteria : logical correctness of results + time constraints
• Embedded: An embedded computer system is physically ‘embedded’ in its environment and often controls it
• Safety – Criticality: failure can cause severe damages!!
04/18/2023 Real Time Systems 7
RTS: Common Terminologies
• Release time: The time at which all the data required to begin execution of the task are available
• Deadline: The time by which the task must complete its execution– Absolute: Time ‘0’ to the actual instant of Deadline’s occurrence – Relative: Time of arrival to the corresponding deadline’s
occurrence
• Response time: The time duration from the occurrences of the event generating the task to the time the task produces its result
04/18/2023 Real Time Systems 8
Issues
• Architecture: Is the architecture suitable?– safety– reliability– fault- tolerance
• Operating system : Is the Operating System suitable?– Task assignment and scheduling– Communication protocol– Failure management and recovery– Interrupt Latency
04/18/2023 Real Time Systems 9
Allocation And Scheduling Issue
• Given: a set of tasks task precedence constraints resource requirements task characteristics Deadlines
• Objective: to devise a feasible allocation/schedule
04/18/2023 Real Time Systems 10
Task Precedence
• A task is said to precede another task, if :the first task must complete before the second task can start
• If task Ti’s output is needed as input by task Tj, then task Tj is constrained to be precede by task Ti
• Precedence constraints can be represented by means of a precedence graph
04/18/2023 Real Time Systems 11
Real Time Task Characteristics • Three types:
A. Periodic - Task is released periodically
B. Sporadic – • These tasks may be released at any time instant, but a
minimum separation exists between releases of consecutive instances of the same sporadic task
• Can be modeled by periodic tasks with periods equal to the minimum separation; BUT.. Unlike periodic tasks, sporadic tasks are released irregularly or may not be released at all
04/18/2023 Real Time Systems 12
Real Time Task … Contd.
C. Aperiodic – Tasks which are not periodic and which also have no upper
bound on their invocation rate
Can arise at random instants
Minimum separation between two consecutive instances may be 0
04/18/2023 Real Time Systems 13
Scheduling Algorithms
• Uniprocessor, Multiprocessor Scheduling
• Uniprocessor: Rate Monotonic, Earliest Deadline First, Deadline Monotonic etc.
• Multiprocessor: Utilization Balancing, Next-fit, Bin-packing etc.
04/18/2023 Real Time Systems 14
Scheduling Algorithms : Classification
• Preemptive: • The running task can be interrupted at any time to assign
the processor to ANOTHER task, according to a predefined policy
• Non- Preemptive: a) With Non- Preemptive algorithms, a task, once started,
is executed by processor until completionb) All scheduling decisions taken after this task’s
execution
04/18/2023 Real Time Systems 15
Classification… Contd.
• Static: sch. Decisions are based on fixed parameters, assign to tasks before their activation
• Dynamic: dynamic parameters, may change during sys. Evolution
• Offline : execution on entire set of tasks before actual task activation
• Online: sch. Decisions are taken at RUNTIME every a new task enters the sys or, when a running task terminates
04/18/2023 Real Time Systems 16
Classification… Contd.
• Optimal: minimizes some given cost function defined over the task set.
• Heuristic: a) Searches for a feasible schedule Using an OBJECTIVE
FUNCTION(Heuristic Function)b) Do NOT guarantee to find an optimal schedule
04/18/2023 Real Time Systems 17
Scheduling AnOMaLiEs
In General / Fast Computing :Increase of computational Power in the supporting Hardware always
RESULTS an Improvement on the performance of a task set
In Real Time computing: ????
04/18/2023 Real Time Systems 18
NO!!!
I. Anomalies like this have been first described by Graham in 1976
II. are refer to task sets with precedence constraints and executed in Multi-processor environment
• RM•EDF•DM•EDD•LDF
And few more…
Uniprocessor Algorithms
Real Time Systems
04/18/2023 Real Time Systems 20
Rate Monotonic (RM) Algorithm
• An Optimal Static- priority Uniprocessor scheduling algo
• The task set consists of – Periodic tasks– Pre-emptible tasks
whose deadlines = the task’s periods
• Task priorities are static and inversely related to their periods
04/18/2023 21
RM (Rate Monotonic)
• Optimal static-priority scheduling• It assigns priority according to period• A task with a shorter period has a higher priority• Executes a job with the shortest period
(4,1)
(5,2)
(7,2)
5
5
10
10 15
15
T1
T2
T3
04/18/2023 22
RM (Rate Monotonic)
• Executes a job with the shortest period
(4,1)
(5,2)
(7,2)
5
5
10
10 15
15
T1
T2
T3
04/18/2023 23
RM (Rate Monotonic)
• Executes a job with the shortest period
(4,1)
(5,2)
(7,2)
Deadline Miss !
5
5
10
10 15
15
T1
T2
T3
04/18/2023 24
Response Time
• Response time – Duration from released time to finish time
(4,1)
(5,2)
(10,2)
5
5
10
10 15
15
T1
T2
T3
04/18/2023 25
Response Time
• Response time – Duration from released time to finish time
(4,1)
(5,2)
(10,2)
Response Time
5
5
10
10 15
15
T1
T2
T3
04/18/2023 26
Response Time
• Response Time (ri)
• HP(Ti) : a set of higher-priority tasks than Ti
(4,1)
(5,2)
(10,2)
k
THPT k
iii e
p
rer
ik
)(
5
5
10
10
T1
T2
T3
04/18/2023 27
RM - Schedulability Analysis
• Real-time system is schedulable under RM if and only if ri ≤ pi for all task Ti(pi,ei)
04/18/2023 28
RM – Utilization Bound
• Real-time system is schedulable under RM if ∑Ui ≤ n (21/n-1)
• Liu & Layland’s Theorem
04/18/2023 Real Time Systems 29
Necessary And Sufficient Condition For RM-schedulable• Necessary condition: A set of periodic real –time tasks
would not be RMA Schedulable unless they satisfy the following condition:
U = = ≤ 1
• Sufficient Condition: If the task set is such that the
processor utilization is Less Than Equal To n(21/n – 1)(This is known as Liu and Layland’s condition)
then the task set is definitely RM-schedulablei.e., U ≤
04/18/2023 Real Time Systems 30
RM Scheduling
• Let:- e:: execution time; p:: period such that p1 ≤ p2 ≤ …. pn
• Start with n = 2: T1 and T2 ; so that total utilization
U= = (e1/p1) + (e2/p2) ≤ 2(21/2 – 1), ≤ 0.824
Similarly, for n=3: U= (e1/p1) + (e2/p2) + (e3/p3) = ≤ 3(21/3 – 1)
≤ 0.78
then the tasks are RM-schedulable
04/18/2023 Real Time Systems 31
RM Scheduling - Example
• T1(1,4), T2(1,5), T3(1,10)
U= ∑Ui = 1/4 + 1/5 + 1/10
= 0.55 ( ≤ 1; therefore, necessary condition satisfied)
Now, = 3 (21/3-1) ≈ 0.78
Since, U (0.55) is ≤ 0.78, So sufficient condition(Liu and Layland’s condition )also satisfied
Thus, {T1, T2, T3} is schedulable under RM
04/18/2023 32
RM Utilization Bounds
0.5
0.6
0.7
0.8
0.9
1
1.1
1 4 16 64 256 1024 4096
The Number of Tasks
Util
izat
ion
RM – Utilization Bound• Real-time system is schedulable under RM if
∑Ui ≤ n (21/n-1)
04/18/2023 Real Time Systems 33
Preemptive Earliest Deadline First (EDF) Algorithm• Optimal Dynamic-priority Uniprocessor scheduling algo
• Always executes the task whose absolute deadline is the earliest
• Priorities changes depending on the closeness of their absolute deadline
• A special case of Deadline Monotonic algorithm
04/18/2023 Real Time Systems 34
Preemptive EDF… Contd.
Assumptions:• No task has any non-preemptive section and the cost of
preemption is negligible
• Only processing requirements are significant, memory, I/O and other resource requirements are negligible
• All tasks are independent; there are no precedence constraints
• Tasks need not be periodic
04/18/2023 35
EDF (Earliest Deadline First)
(4,1)
(5,2)
(7,2)
5
5
10
10 15
15
T1
T2
T3
04/18/2023 36
EDF (Earliest Deadline First)
• Executes a job with the earliest deadline
(4,1)
(5,2)
(7,2)
5
5
10
10 15
15
T1
T2
T3
04/18/2023 37
EDF (Earliest Deadline First)
• Executes a job with the earliest deadline
(4,1)
(5,2)
(7,2)
5
5
10
10 15
15
T1
T2
T3
04/18/2023 38
EDF (Earliest Deadline First)
• Executes a job with the earliest deadline
(4,1)
(5,2)
(7,2)
5
5
10
10 15
15
T1
T2
T3
04/18/2023 39
EDF (Earliest Deadline First)
• Optimal scheduling algorithm – if there is a schedule for a set of real-time tasks, EDF can schedule it.
(4,1)
(5,2)
(7,2)
5
5
10
10 15
15
T1
T2
T3
04/18/2023 40
EDF – Summary
• Optimal Dynamic-priority Uniprocessor scheduling algo
• Always executes the task whose absolute deadline is the earliest
• Priorities changes depending on the closeness of their absolute deadline
• A special case of Deadline Monotonic algorithm
04/18/2023 41
EDF – Utilization Bound
• Real-time system is schedulable under EDF if and only if
∑Ui ≤ 1
• Liu & Layland Theorm• Necessary as well as sufficient condition??
04/18/2023 Real Time Systems 42
Schedulability TestU =
P = lcm (P1…Pn)
Let:hT(t) be the SUM of the execution times of all tasks in
set T whose absolute deadlines are less than t
04/18/2023 Real Time Systems 43
Schedulability Test… Contd.
• A task set of n tasks is not EDF feasible Iffi) U > 1 orii) There exists
such that hT(t) > t.
04/18/2023 44
• Domino effect during overload conditions– Example: T1(4,3), T2(5,3), T3(6,3), T4(7,3)
EDF – Overload Conditions
T1
50 7
T2 T3 T4
3 6
Deadline Miss !
T1
50 7
T3
3 6
Better schedules :
T1
50 7
T4
3 6
04/18/2023 Real Time Systems 45
Preemptive EDF… Contd.
Example:Aperiodic tasks: t = 0: T1 arrives, starts execution
t = 4: T2 arrives, preempts T1
t = 5: T3 arrives, waits t =7: T2 completes and T3 starts execution
t = 17: T3 completes and T1 starts execution
t = 23: T1 completes
Task Arrival Time
Execution Time
Abs. Deadline
T1 0 10 30
T2 4 3 10
T3 5 10 25
EDF- Example
Example: Periodic tasks:• Two tasks: T1(2,5) and T2(4,7)
– U = ?– Schedulable Under :
• RMA?• EDF?
– Scheduled diagram from 0 to 35?
04/18/2023 46
Example… contd.
04/18/2023 47
• Schedule under EDF
1a 2a 1b 2b 1c 2c 1d 2c 1e 2d 1f
2 5 6 7 8 10 12 14 15 17 20 21 22 25 26 28
04/18/2023 48
RM vs. EDF• Rate Monotonic
– Simpler implementation, even in systems without explicit support for timing constraints (periods, deadlines)
– Predictability for the highest priority tasks
• EDF– Full processor utilization– Misbehavior during overload conditions
04/18/2023 Real Time Systems 49
Deadline Monotonic Algorithm
• Static priority algorithm• For those tasks having Period ≠ Relative Deadline• Assigns priorities to tasks according to their
relative deadline.Task Release
TimePeriod Execution
TimeRelative deadline
Priority
T1 50 50 (1) 25 100 3
T2 0 62.5 (2) 10 20 1
T3 0 125 (3) 25 50 2
04/18/2023 Real Time Systems 50
DM Algorithm… Contd.
• When the relative deadline of every task is proportional to its period, RM and DM algorithms are identical
• When the relative deadlines are arbitrary, DM algorithm performs better
• DM can produce a feasible schedule when RM algorithm fails, While RM algorithm always fails when the DM algorithm fails
04/18/2023 Real Time Systems 51
APERIODIC TASK SCHEDULING
• Deals with scheduling of Real Time APERIODIC tasks on a single machine environment
• To facilitate the description of Aperiodic Scheduling problems: Graham proposed a Notation:
α/β/ϒ
• Notation Classifies all algorithms using these 3 fields
04/18/2023 Real Time Systems 52
Notation α/β/ϒ
• α: describes machine environment on which task set has to be scheduled(Uniprocessor, multiprocessor)
• β: describes TASK and RESOURCE characteristics(preemptive Vs. Non- Preemptive , Independent Vs. Precedence constraint, SYNCHRONOUS Activations etc.)
• ϒ: describes the optimality criterion(performance measure)
04/18/2023 Real Time Systems 53
Notation α/β/ϒ• Example: 1/prec/Lmax : denotes “the problem of scheduling” a set of tasks on a
Uniprocessor machine with precedence constraints and the “objective is to minimize” the maximum lateness If no additional constraints are indicated in the second field then ,
preemption is allowed at any time and tasks have arbitrary arrivals
2/sync/ Σ latei : denotes “the problem of scheduling” a set of tasks on a Two- processor machine. Tasks have synchronous arrival times and Do NOT have other constraints. the “objective is to minimize” the number of late tasks
3/no_preem/ Σ Ri : ???
04/18/2023 Real Time Systems 54
Jackson’s Algorithm
• The Problem considered by this algo. is: 1/sync/Lmax
• Here: – All tasks consists of a single job, have syn. arrival times, BUT can
have different execution times and deadlines– Tasks are independent, can’t have precedence relations and can’t
share resources in exclusive mode
• A simple algo. that solves this Problem was found by Jackson in 1955:
• It is called as Earliest Due Date Algorithm and can be expressed by the following rule:
04/18/2023 Real Time Systems 55
Jackson’s Rule : EDD Algorithm
• “Given a set of n independent tasks:any algorithm that executes the tasks in order of increasing deadlines is OPTIMAL with respect to Minimizing the MAXIMUM Lateness ”
04/18/2023 Real Time Systems 56
EDD Algorithm
• Consider a set of 5 tasks, simultaneous activated at t=0:
• Scheduling the tasks produced by EDD algo:1. The maximum lateness = -1 : due to J4(completes a unit of time
before its deadline)2. Since the lateness is negative: we can conclude that all tasks have
been executed within their deadlines
J1 J2 J3 J4 J5
ei 1 1 1 3 2
Di 3 10 7 8 5
04/18/2023 Real Time Systems 57
EDD …Contd.
• Note:The optimality of EDD algo. CAN NOT Guarantee the
Feasibility of the schedule for a task set. It ONLY guarantees that:“if there exists a feasible schedule for a task set, then EDD
will Find it”
04/18/2023 Real Time Systems 58
Scheduling With Precedence Constraints Latest Deadline First(LDF) Algorithm
• Problem : 1/PREC,SYNC/Lmax
• Given: – a set J of tasks – a direct acyclic graph(DAG) describing tasks’ precedence relations
i. Among the tasks without successors or whose successors have all been selected, select the task with the latest deadline
ii. Remove this task from the precedence graph and put itinto a stack
04/18/2023 Real Time Systems 59
LDF …Contd.
iii. Repeat until all tasks are in the stack
iv. The stack represents the order in which tasks should be scheduled
• LDF is optimal
04/18/2023 Real Time Systems 60
LDF: Example
• Solve the example• construct the LDF schedule• Find Lmax and feasibility of schedule• Now, construct the EDF schedule. Compare.
04/18/2023 Real Time Systems 61
Allowing For Precedence And Exclusion Conditions(PREC)• Relaxing the assumptions:
– Tasks are independent and – Tasks are always pre-emptible by other tasks
• E.g. Consider a set of tasks with a precedence graph, which are released at time 0
T1
T2 T3
T4 T5
T6
04/18/2023 Real Time Systems 62
Example… Contd.
• Assumption: Deadline is specified for each task, such that there will be enough time to execute its children in the task graph by their deadlines even if a task completes at its deadline
Ti Ei Di
1 3 6
2 3 7
3 2 20
4 5 21
5 6 27
6 6 28
04/18/2023 Real Time Systems 63
Algorithm PREC
• Tasks are numbered so that D1 ≤ D2 ≤ D3 … ≤ Dn
1. Schedule task Tn in the interval , [Dn-en, Dn]
2. While ( all the tasks have not been scheduled), let A be the set of as-yet-unscheduled tasks all of whose
successors if any, have been scheduled
Schedule task Tk , k = max { m/ m є A} as late as possible
enddo
3. Move the tasks forward to the extent possible keeping their order of of execution as specified in step 2
04/18/2023 Real Time Systems 64
Handling Critical sections• A task that is currently holding the un-sharable resource is
said to be in the critical section associated with the resource
• Binary semaphores can be used so that only one task can access the critical section
• Properly nested: Serialization– Lock S1
• Lock S2• Unlock S2
– Unlock S1
04/18/2023 Real Time Systems 65
Handling Critical…Contd.
• Assumptions• Once a task starts, it continues until it finishes• It can be preempted by some higher priority task• It can be blocked by lower priority task that holds
the lock on a critical section that it needs
04/18/2023 Real Time Systems 66
Priority Inversion Problem
• When a Lower priority task (TL)blocks Higher priority task(TH) indefinitely
• Can occur in any priority-based preemptive scheduling scheme
• Occurs when circumstances within the system force a higher priority task to wait for a lower priority task
• Indefinite priority inversion– Occurs when a task of medium priority preempts a task of lower
priority which is blocking a task of higher priority
04/18/2023 Real Time Systems 67
Indefinite Priority Inversion: Example
J1, J2, J3 are 3 tasks ( Descending order of priority to share a processor)
S – Critical Section used by J1 and J3
– t0 : J3 begins execution– t1 : J3 enters S– t2 : J1 released, it preempts J3 and start executing– t3 : J1 wants to enter S, S is locked by J3 which is
suspended, J1 is blocked and J3 continues in S– t4 : J2 is released which preempts J3– t5 : J2 completes, J3 resumes– t6 : J3 exits S; J1 preempts J3
04/18/2023 Real Time Systems 68
Indefinite… Contd.
(try to lock S)blocked by J3preempt J3
lock S
preempt J3
timet0 t1 t2 t3 t4
J1
J2
J3
indefinite blocking !
04/18/2023 Real Time Systems 69
Solution:
Use of Priority Inheritance Protocol (PIP):
If THigh is blocked by TLow , TLow temporarily inherits the priority of THigh. When blocking ceases, TLow resumes its original priority
• For previous example of Priority Inversion Problem:– At time t3, when J3 blocks J1, J3 inherits the higher priority of J1– So, when J2 is released at t4, It can not interrupt J3– As a result , J1 is not indirectly blocked by J2
04/18/2023 Real Time Systems 70
Problems in PIP1. Deadlocks
– Due to crossing nested semaphores
2. Long Blocking Delays– Due to
a. Transitive Blocking– Blocking by indirectly involved semaphores which are
accessed in nested form by blocking jobsb. Chain Blocking
– Blocking can occur sequentially whenever accessing each semaphore
04/18/2023 Real Time Systems 71
PIP : Deadlocking Problem Example• Let 2 Tasks: J1 and J2 use Critical Sections S1 & S2 (while J1 > J2)
– J1: Lock S2 LockS1 Unlock S1 Unlock S2– J2: Lock S1 Lock S2 Unlock S2 Unlock S1
t0 : J2 starts executiont1 : J2 locks S1t2 : J1 is initiated, it preempts J2t3 : J1 locks S2t4 : J1 attempts to lock S1 but blocked. Now J2 inherits
priority of J1, starts executiont5 : J2 tries to lock S2, it cannot do so, since J1 has a lock on
it
Both T1 and T2 are deadlocked!
04/18/2023 Real Time Systems 72
PIP : Deadlocking…Contd.
J1 = {..., P(S2) ..., P(S1),...,V(S1) ...,V(S2),...}
J2 = {..., P(S1) ..., P(S2),...,V(S2) ...,V(S1),...}crossing nested
semaphores
2J
1Jlock S1
preempt J2 lock S2(try to lock S1)blocked by J2
(try to lock S2)blocked by J1
deadlock!
time0t 1t 2t 3t 4t 5t
04/18/2023 Real Time Systems 73
PIP: Chain Blocking Problem ExampleJ1 = {..., P(S1), ..., V(S1), ..., P(S2), ..., V(S2), ...}J2 = {..., P(S2) , ..., V(S2), ...}J3 = {..., P(S1) , ..., V(S1), ...}
3J
1J
2J
time
preempt J3
(attempt to lock S1)blocked by J3 complete
(attempt to lock S2)blocked by J2
unlockS1
unlock S2
unlock S2
lock S2
lock S1
lock S1
unlockS1
lock S2
t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13
complete
complete
04/18/2023 Real Time Systems 74
What Really Happened on Mars?
04/18/2023 Real Time Systems 75
Repeated Resets in Mars Pathfinder
• The Mars Pathfinder mission was widely proclaimed as "flawless" in the early days after its July 4th, 1997 landing on the Martian surface....
• But a few days into the mission, not long after Pathfinder started gathering meteorological data, the spacecraft began experiencing total system resets, each resulting in losses of data.
• The press reported these failures in terms such as "software glitches" and "the computer was trying to do too many things at once"....
04/18/2023 Real Time Systems 76
Priority Ceiling Protocol
• Goals: Solve problems of PIP: Prevent deadlocks, transitive blocking
and blocking chains
• Basic Idea:– Priority ceiling of a semaphore is:
“The priority of the highest priority task that may use the semaphore”
04/18/2023 Real Time Systems 77
Priority Ceiling Protocol
• Similar to Inheritance except that :“a task can also be blocked from entering a critical
section(CS) if there exists any semaphore currently held by some other task whose priority ceiling is greater than or equal to the priority of T”i.e., Additional condition for allowing a job J to start a new
critical section is:only if J’s priority is higher than all priority ceilings of all the semaphores locked by jobs other than J
04/18/2023 Real Time Systems 78
Illustration: Priority Ceiling
• Tasks: T1, T2, T3 T1> T2> T3
04/18/2023 Real Time Systems 79
PCP Example:
• 4 real time tasks: J1,J2,J3,J4 Sharing two CS: S1, S2 such that: – S1: J1, J2, J3– S2: J1, J4
• P(J1) < P(J2) < P(J3) < P(J4)Thus, from the given info:
– Ceil(S1) = max { P(J1), P(J2), P(J3) } = P(J3)– Ceil(S2) = max { P(J1), P(J4) } = P(J4)
• Initial Situation : Task J1 is executing and after some time, it moves to acquire S1
– Since S1 is free, task J1 entered and locked it– After locked, Ceiling of S1 is set (= P(J3))
04/18/2023 Real Time Systems 80
PCP Example… Contd.Now, • Situation 1: After some time, J4 Becomes Ready
– Since P(J4) > P(J1), J4 preempts J1 and starts execution– After some time, J4 Requests S2– Since P(J4) > Ceil (S1){Note that at present, only S1 is locked}, J4 is
granted S2– After locked, Ceil(S2) is now set (= P(J4))– When J4 completes its execution, J1 will get a chance to execute
• Situation 2: After some time, J3 becomes Ready – Since P(J3) > P(J1), J3 preempts J1 and starts execution– After some time, J3 Requests S1– Since P(J3) is NOT Greater Than Ceil (S1), J3 will NOT granted S1– Thus, J3 would be blocked and J1 inherit the priority of J3 and starts
execution
04/18/2023 Real Time Systems 81
PCP Advantage #1• Prevent deadlocks :
J1 = {..., P(S0) ..., V(S0),...}
J2 = {..., P(S1) ..., P(S2),...,V(S2) ...,V(S1),...}J3 = {..., P(S2) ..., P(S1),...,V(S1) ...,V(S2),...}
nested crossing semaphores
3J
1J
2J
lock S2
time
preempt J3
2t 3t1t0t 4t 5t 6t 7t
complete
complete
unlock S2
unlock S1lock S1
lock S2
unlock S1
lock S0
(attempt to lock S1)blocked by J3
9t 10t 11t 12t 13t8t
completelock S1 unlock S2
preempt J3
unlock S0
14t 15t
04/18/2023 Real Time Systems 82
PCP Advantage #2• Prevent transitive blocking :
J1 = {..., P(S2) ..., V(S2),...}
J2 = {..., P(S2) ..., P(S1),...,V(S1) ...,V(S2),...}J3 = {..., P(S1),...,V(S1) ...,}
nested semaphores
1J
2J
3J
time
0t 1t 2t 3t 4t 6t5t 7t 8t 9t 10t 11t 12t 13t
lock S1
preempt J3 (attempt to lock S2)blocked by J3
preempt J3
lock S2 unlock S2
complete
unlock S1
lock S2
lock S1 unlock S1
unlock S2complete
complete
83
PCP Advantage #3• Prevent Chain Blocking :
J1 = {..., P(S1), ..., V(S1), ..., P(S2), ..., V(S2), ...}J2 = {..., P(S2) , ..., V(S2), ...}J3 = {..., P(S1) , ..., V(S1), ...}
preempt J3
(attempt to lock S1)blocked by J3 completeunlock
S1
unlock S2
unlock S2lock
S1
lock S1
unlockS1
lock S2
complete(attempt to lock S2)blocked by J3
lock S2
1J
2J
3J
t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13
complete
04/18/2023 Real Time Systems 84
Practice Problem:
S1 T1 T4 P(T1) S2 T2, T4, T5 P(T2)
04/18/2023 Real Time Systems 85
Different Types of Priority Inversions Under PCP• Tasks, sharing a set of resources using PCP, may undergo 3 types of
priority inversions:1. Direct Inversion: Occurs when a higher priority task waits for a
lower priority task to release a resource that it needs
– At present, a lower priority task TL is holding a critical section CS
– Now, if a higher priority task TH needs this resource, it would have to wait till TL finishes using CS and release it
– Thus a lower priority task DIRECTLY causes a higher priority task to undergo inversion by holding the resource it needs
THCS
TL
86
Different Types… Contd.
2. Inheritance- Related Inversion:
– When a lower priority task (TL) is holding a resource and a higher priority task (TH) is waiting for it
– Then, the priority of TL is raised to that of the waiting TH by implementing PIP
– As a result, the Intermediate tasks (TI ) not needing the resource can not execute due to the raised priority of TL and therefore, TI undergo Inheritance- Related Inversion
TL
CS
TH
TI
04/18/2023 Real Time Systems 87
Different Types… Contd.3. Avoidance- Related Inversion:
– In PCP- when a task J request a resource its priority is checked against all the priority ceilings of all the semaphores locked by task other than J
– Requesting task J is granted the use of the shared resource only when its priority is greater then the highest Ceiling Value(CSV)
Ceil(S1)= 10CSV= 10 P(TH) = 8
Thus, even when a resource that task is requesting is idle, the requesting task may be denied access to the shared resource if this requesting task’s priority is less then CSV
TL
CS1 TH
CS2
04/18/2023 Real Time Systems 88
Different Types… Contd.
– A task whose priority is greater then the currently executing task , but lesser then CSV and needs a shared resource that is currently not in use, is said to undergo Avoidance- Related Inversion
– Thus, a higher priority task may get blocked for a shared resource that is not being used by any of the tasks
– Though this restriction might appear too restrictive and wasteful, it necessarily leads to prevent deadlocks
– Therefore, this type of inversion is popularly called Deadlock- Avoidance Inversion: blocking is caused due to the Priority Ceiling rule in order to avoid Deadlocks
04/18/2023 Real Time Systems 89
Example: Different Types of Inversions
• A systems has 6 Tasks and 3 CS: S1, S2 and S3; such that:
– P(T1) > P(T2)> P(T3)>P(T4) >P(T5) >P(T6) – The exact resource requirements of these tasks & the duration(in msec) for which
the tasks needs the resources can be shown as:
T1 T2 T3 T4 T5
T6
S1 S2 S3
20
1030
4050
10 10
04/18/2023 Real Time Systems 90
Example… Contd.Challenge: Computing the different types of inversions that each task might
have to undergo in the worst case
Solution: • The ceiling of any CS ‘S’ can be calculated as:
Ceil(S) = max ( P{Ti}) ; such that Ti may need S
– Thus, Ceil(S1) = max (P{T1, T3, T6}) = P{T1}– Similarly, Ceil(S2) = P{T2}; and Ceil( S3) = P{T3}
• One by one for each task, determine the 3 different types of Inversions that each task might suffer, and the duration of the inversions:
04/18/2023 Real Time Systems 91
Example… Contd.a. For task T1:– T1 can suffer direct- inversion by T3(10 sec due to S1), OR , by T6(10 sec due to S1)– T1 will not suffer any inheritance- related inversion or deadlock- avoidance
inversion since it is the highest priority task
b. For task T2:– T2 can suffer direct- inversion by T4(40 sec due to S2)– T2 can also suffer inheritance- related inversion on account of T3(10 sec ) when T3
acquired S1 and T1 waits for S1, OR, on account of T6(10 sec ) when T6 acquired S1 and T1 waits for S1
– T2 can suffer Deadlock- avoidance inversion by T3(10 sec ) when T3 acquired S1and ceiling value of S1 is now set to P(T1). At this moment, if T2 might request for S2, it is denied access to S2 because presently, the highest ceiling value(CSV) = P(T1) which is obviously higher then P(T2)
OR, T2 can also suffer Deadlock- avoidance inversion on account of T6(10 sec ) when T6 acquires S1 and ceiling value of S1 is now set to P(T1)
04/18/2023 Real Time Systems 92
Example… Contd.
c. For task T3:– Direct inversion: by T6(10 sec due to S1) – Inheritance- related inversion: on account of T4(40 sec) when T4 acquired S2 and
T2 waits for S2, OR, on account of T6(10 sec ) when T6 acquired S1 and T1 waits for S1
– Deadlock- avoidance inversion: by T4(40 sec), OR, by T6(10 sec )
d. For task T4:– Direct inversion: T4 will not suffer any direct inversion since it does not share any
resource with a lower priority task– Inheritance- related inversion: on account of T6(10 sec ) when T6 acquired S1 and
T1 waits for S1– Deadlock- avoidance inversion: by T6(10 sec )
04/18/2023 Real Time Systems 93
Example… Contd.
e. For task T5:– Direct inversion: T5 will not suffer any direct inversion since it does not share any
resource with a lower priority task– Inheritance- related inversion: on account of T6(10 sec ) when T6 acquired S1 and
T1 waits for S1– Deadlock- avoidance inversion: T5 will not suffer any direct inversion since it does
not require any shared resource(CS)
f. For task T6:– T6 will not suffer any priority inversion, as it is the lowest priority task
• Now, constructing the inversion table for representing above values:
04/18/2023 Real Time Systems 94
Example… Contd.
Direct Inheritance Avoidance
Task T2 T3 T4 T5 T6 T2 T3 T4 T5 T6 T2 T3 T4 T5 T6
T1 x 10 x x 10 x x x x x x x x x x
T2 x x 40 x x x 10 x x 10 x 10 x x 10
T3 x x x x 10 x x 40 x 10 x x 40 x 10
T4 x x x x x x x x x 10 x x x x 10
T5 x x x x x x x x x 10 x x x x x
Inversion Table
04/18/2023 Real Time Systems 95
Extending The RM Algorithm: Lehoczky’s Test• RMS theory of Liu and Layland: Utilization bound based approach• RMS theory of Lehoczky: Time demand based approach
• In RM scheduling:“even when a task set fails Liu and Layland’s
Test, we should not conclude that it is not schedulable under RM algorithm. We need to test further to check if the task set is RM schedulable”
• A test that can be performed to check whether a task set is RM schedulable when it fails Liu and Layland’s test is the Lehoczky’s Test
04/18/2023 Real Time Systems 96
Lehoczky’s Test
• Lehoczky’s test can be expressed as:“ A set of periodic real-time task is RM schedulable
under any task phasing, if all the tasks meet their respective first deadlines under zero phasing ”
Response Time: (Finishing Time) – (Arrival Time or Release time) In phase /Zero phase: Two(or more) periodic tasks are said to be in
phase if their first Instances release simultaneously
The Worst Case Response time for a task occurs when it is in phase with its higher priority tasks
04/18/2023 Real Time Systems 97
Lehoczky’s Test T1{10, 30} and T2{60,120} P{T1}> P{T2}
a) T1 is in phase with T2,Response Time = 90 msec
b) T1 has a 20 msec phase with respect with T2 Response Time = 80 msec
• Thus, worst case Response time for a task occurs when it is in phase with its higher priority task
T1 T2 T1 T2 T1 T2
10 30 40 60 70 90
T2 T1 T2 T1 T2
20 30 50 60 80
04/18/2023 Real Time Systems 98
Lehoczky’s Test: Formal expression
• Let:– we have tasks set: {T1 T2 T3 …… Ti} ; such that P{T1} > P{T2}.... > P{Ti}
– Let the task Ti arrives at time t= 0
– During the first instance of the task Ti , 3 instances of the task T1have occurred
1Ti
• Each time T1 occurs, Ti has to wait since T1 has higher priority then Ti
1T1 1T1 1T1
04/18/2023 Real Time Systems 99
Lehoczky’s Test: Formal expression• In general:The exact no. of times that T1occurs within a single instance of Ti is given by:
[pi / p1]
• Since T1‘s execution time is e1, The total execution time required by T1 before the deadline of Ti is:
[pi / p1] e1
• This expression can easily be generalized to consider the execution times of all the tasks having higher priority then Ti(i.e., T1 T2 T3 …… Ti-1)
• Therefore the time for which Ti will have to wait due to all its higher priority tasks can be expressed as:
04/18/2023 Real Time Systems 100
Lehoczky’s… Contd.
• This is total time required to execute all the tasks with higher priority then Ti for which Ti would have to wait
• Thus, the task Ti would meet its first deadline, if:
ei + ≤ pi
That is, “if the sum of (the execution times of all the higher priority
tasks occurring before Ti ‘s first deadline) and (the execution time of the task Ti itself) is less then equal to its period, then the task Ti
would complete before its first deadline ”
04/18/2023 Real Time Systems 101
Lehoczky’s… Contd.
• In above expression, it is implicitly assumed that the task periods are equal to their respective deadlines, i.e., pi = di
when pi < di: the expression need the following modification:
ei + ≤ di
04/18/2023 Real Time Systems 102
Example:• Problem: 3 periodic real time tasks : T1(10, 20), T2(15, 60),
T3(20,120) to be run on a uni-processor. Determine whether the task is schedulable under RMA
• Solution: 1. Ordering all tasks acc. to Priority : T1 > T2> T32. Try the sufficiency test for RMA Schedulability( Liu and Layland’s Test):
the task set is schedulable if Σ Ui ≤
≤ 3 (21/3-1) ≈ 0.78
Σ Ui = (10/20 + 15/60 + 20/120) = 0.91
This is greater then 0.78. Thus, the task set fails Liu and Layland’s test
04/18/2023 Real Time Systems 103
Example… Contd.3. Now, try Lehoczky’s Test: • Ordering all tasks acc. to decreasing order of Priority : T1 > T2> T3
– Testing for T1: since e1 < d1 here, thus T1 would meet its first deadline
– Testing for T2: 15 + [60/20] x 10 ≤ 60
or, 15 + 30 =45 ≤ 60This is satisfied. Thus, T2 would meet its first deadline
– Testing for T3: 20 + [ 120/20] x 10 + [120/60] x 15 = 20 + 60 + 30 = 110msec
This is satisfied. Thus, T3 would also meet its first deadline
Since all the tasks meet their respective first deadlines, the task set is RMA schedulable according to Lehoczky’s result
04/18/2023 Real Time Systems 104
Practice
• 3 periodic real time tasks : T1(20, 100), T2(30, 150), T3(90,200) to be run on a uni-processor. Determine whether the task is schedulable under RMA.
• Use lehoczky’s test.
04/18/2023 Real Time Systems 105
Handling Overload Conditions• Problem: scheduling RT tasks in overloading conditions
overloading conditions: Situations in which the computational demand requested by the task set exceeds the time available on the processor, and hence not all tasks can complete within their deadlines
• Types of transient overload condition :1. Transient overloads due to Activation Overruns:
• Due to excessive arrivals of Aperiodic jobs• This type is typical in Event-Triggered systems • In EDF: Arrival of a new task can cause all the previous tasks to miss
their deadlines – Domino Effect
04/18/2023 Real Time Systems 106
Handling… Contd.
(a) Feasible schedule with EDF in normal load condition; (b) domino effect
04/18/2023 Real Time Systems 107
Handling… Contd.
2. Transient overloads due to Execution Overruns: • Due to periodic/ Aperiodic tasks that sporadically execute more than
expected• Can occur in both: Event Triggered and Time Triggered systems• Under RM, an overrun in a task Ti does not affect tasks with higher priority, but
any of the lower priority task could miss its deadline• Under EDF: Domino Effect can be feasible
3. Permanent overloads in Periodic systems:• When the total utilization factor of the task set is greater then 1• Can happen either because of:
a. The execution requirements of the task set was not correctly estimatedb. Some unexpected activation of new periodic tasksc. Some of the current tasks increased their activation rate to react to some
changes in the environment
04/18/2023 Real Time Systems 108
Typical Scheduling Schemes
1. Best Effort: – This class includes those algorithms with no prediction for overload
conditions– At its arrival, a new task is always accepted into a ready queue– So the system performance can only be controlled through a proper
priority assignment that takes task ’s priority values into account
Task
2. Guaranteed:– Includes those algorithms with Admission Control, in which the
load on the processor is controlled by an Acceptance Test executed at each task interval
Ready Queue Run
04/18/2023 Real Time Systems 109
Typical Scheduling… Contd.– Typically, whenever a new task enters the system, a Guarantee
Routine verifies the Schedulability of the task set based on the Worst case Assumptions. If the task set is found to be schedulable, the new task is accepted in the ready queue; otherwise, it is Rejected
Task accepted
rejected
3. Robust Scheduling:– Includes those algorithms that separate timing constraints and
Importance by considering 2 different policies: one for task acceptance and one for task rejection
Guarantee Routine
Ready Queue Run
04/18/2023 Real Time Systems 110
Typical Scheduling… Contd.
– Whenever a new task enters the system, an Acceptance Test verifies the Schedulability of the new task set based the worst case assumptions
– If the task set is found to be schedulable, the new task is accepted in the ready queue; otherwise, 1 or more tasks are rejected based on a different policy
Task Scheduling Policy
Rejection
Policy
Planning Ready Queue Run
Reject Queue
04/18/2023 Real Time Systems 111
The RED Algorithm
• Robust Earliest Deadline (RED): A Robust scheduling algo for dealing with firm aperiodic tasks in overloaded environments
• Proposed by Buttazzo and Stankovic • The algorithm synergistically combines many features including:
– Graceful degradation in overloads– Deadline tolerance, and – Resource reclaiming
• Advantages:– operates in normal and overload conditions with excellent
performance– able to predict not only deadline misses but also the size of the
overload, its duration, and its overall impact on the system
04/18/2023 Real Time Systems 112
RED… Contd.
• In RED: 1. Each task Ji{Ci,Di,Mi,Vi) is characterized by 4 parameters: – A worst-case execution time (Cj)– a deadline {Di)– a deadline tolerance(Mi)– an importance value {Vi)
• Deadline Tolerance: – The amount of time by which a task is permitted to be late; that is, the amount
of time that a task may execute after its deadline and still produce a valid result
– Useful in many real applications where the deadline timing semantics is more flexible than scheduling theory generally permits
• E.g.: Robotics and multimedia systems
04/18/2023 Real Time Systems 113
RED… Contd.
2. The primary deadline plus the deadline tolerance provides a sort of secondary deadline, and used to run the acceptance test in overload conditions
– NOTE: having a tolerance greater than zero is different than having a longer deadline
3. Tasks are scheduled based on their primary deadline but accepted based on their secondary deadline
4. A schedule is said to be strictly feasible if all tasks complete before their primary deadline, whereas is said to be tolerant if there exists some task that executes after its primary deadline but completes within its secondary deadline
04/18/2023 Real Time Systems 114
RED… Contd.
5. The guarantee test performed in RED is formulated in terms of residual laxity
• Residual Laxity (Li ): of a task is defined as the interval between its estimated finishing time (fi) and its primary (absolute) deadline (Di)
6. Each residual laxity can be efficiently computed using the result of the following Lemma:
Lemma: Given a set J = {J1, J2, J3…..Jn} of active aperiodic tasks ordered by increasing
primary (absolute) deadline, the residual laxity Li of each task Ji at time t can be computed as
Li = Li-1 + {Di - Di-1} - Ci{t},
04/18/2023 Real Time Systems 115
RED… Contd.
where
L0 = 0, D0 = t (the current time) Ci{t} is the remaining worst-case computation time of task Ji at time t
Proof:
• By definition, a residual laxity is Li = Di- fi. • Since tasks in the set J are ordered by increasing deadlines, task Ji is
executing at time t, and its estimated finishing time is given by the current time plus its remaining execution time (f1 = t + C1)
• As a consequence, L1 is given by:
L1 = D1- f1 = D1- t - C1
04/18/2023 Real Time Systems 116
RED… Contd.
• Any other task Ji, with i > 1, will start as soon as Ji-i completes and will finish Ci units of time after its start :{fi = fi-i + Ci}
• Hence, we have:
Li = Di- fi
= Di- fi-1 – Ci
= Di- {di-1 - Li-1} - Ci
= Li-1 + {Di- di-1} - Ci