Rtos PDF
Rtos PDF
Operating Systems
Summary
¿ Introduction
¿ Basic concepts
¿ RT Scheduling
¿ Aperiodic task scheduling
¿ Periodic task scheduling
¿ Embedded RTOS
¿ Source:
G.Buttazzo, “Hard Real-Time Computing Systems –
Predictable Scheduling Algorithms and Applications“,
Kluwer Academic Publishers.
1
Introduction
¿ Real-time system:
“A real-time system is a computer system in which the
correctness of the system behavior depends not only on
the logical results of the computation, but also on the
physical instant at which these results are produced”
Introduction (2)
¿ Examples of real-time systems:
¿ plant control
¿ control of production processes / industrial automation
¿ railway switching systems
¿ automotive applications
¿ flight control systems
¿ environmental acquisition and monitoring
¿ telecommunication systems
¿ robotics
¿ military systems
¿ space missions
¿ household appliances
¿ virtual / augmented reality
2
Introduction (3)
¿ Real-time
¿ Time: main difference to other classes of computation
¿ Real: reaction to external events must occur during
their evolution.
¿ System time (internal time) has to be measured
with same time scale as controlled environment
(external time)
¿ Real time does not mean fast but predictable
¿ Concept of deadline:
¿ Time after which a computation is not just late,
but also wrong
3
Hard vs. real time (2)
¿ Typical hard real time activities
¿ sensory data acquisition
¿ detection of critical conditions
¿ actuator servoing
¿ low-level control of critical system components
¿ Areas of application:
¿ Automotive:
• power-train control, air-bag control,
steer by wire, brake by wire
¿ Aircraft:
• engine control, aerodynamic control
¿ Areas of application:
¿ Communication systems:
• voice over IP, cellular telephony
• user interaction
• comfort electronics (body electronics in cars)
4
RTOS
5
Real-time Operating Systems (2)
¿ Desirable features of a RTOS:
¿ Timeliness
• OS has to provide mechanisms for:
• time management
• handling tasks with explicit time constraints
¿ Predictability
¿ Fault tolerance
¿ Design for peak load
¿ Maintainability
11
12
6
Real-time Operating Systems (3)
¿ Timeliness
¿ Achieved through proper scheduling algorithms
• Core of an RTOS!
¿ Predictability
¿ Affected by several issues:
• I/O & interrupts
• Synchronization & IPC
• Architecture
• Memory management
• Applications
• Scheduling!
13
14
7
Achieving predictability: interrupts
¿ First solution: Disable all interrupts but timer
interrupts
¿ all peripheral devices have to be handled by
application tasks
¿ data transfer by polling
¿ great flexibility, data transfers can be estimated
precisely
¿ no change of kernel needed when adding devices
¿ Problems:
¿ degradation of processor performance (busy wait)
¿ no encapsulation of low level details
15
¿ Problems
¿ degradation of processor performance (still busy waiting
- within I/0 routines)
¿ more inter-process communication than first solution
¿ kernel has to be modified when adding devices
16
8
Achieving predictability: interrupts
¿ Third solution: Enable external interrupts and
reduce the drivers to the least possible size
¿ driver only activates proper task to take care of device
¿ The task executes under direct control of OS, just like
any other task
¿ control tasks than have higher priority than device task
17
18
9
Achieving predictability:
system calls & IPC
¿ All kernel calls have to be characterized by
bounded execution time
¿ each kernel primitive should be preemptable
¿ Usual semaphore mechanism not suited for real
time applications:
¿ Priority inversion problem (high priority task is blocked
by low priority task)
¿ Solution: use special mechanisms:
• Basic Priority Inheritance protocol
• Priority ceiling
19
20
10
Priority inversion (2)
¿ Basic priority inheritance protocol:
1. A job J uses its assigned priority, unless it is in its CS
and blocks higher priority jobs
In which case, J inherits PH , the highest priority of the
jobs blocked by J
When J exits the CS, it resumes the priority it had at
the point of entry into the CS
2. Priority inheritance is transitive
¿ Advantage:
¿ Transparent to scheduler
¿ Disadvantage:
¿ Deadlock possible
21
22
11
Priority inheritance
and priority ceiling
¿ Inheritance
¿ Ceiling
23
Achieving predictability:
architecture
¿ Obvious source for unpredictability:
¿ Memory accesses not deterministic
¿ Example:
• hit ratio = 90 %, Tcache= 10ns, Tmem= 50ns
• => 10 % of accesses have access time 18% larger than
cacheless case
¿ Solutions:
¿ systems without cache (or disabled)
¿ precise estimation of caching behaviour
• Systems with application-specific buffers
24
12
Achieving predictability:
memory management
¿ Problems:
¿ flexibility reduced
¿ careful balancing required
25
Achieving predictability:
Applications
¿ Current programming languages not expressive
enough to prescribe precise timing
¿ Need of specific RT languages
¿ Desirable features:
¿ no dynamic data structures
¿ no recursion
¿ only time-bound loops
26
13
What RTOS?
¿ Proprietary:
¿ VxWorks by WindRiver
¿ LynxOS by Lynx
¿ Free/Academical/Open-source:
¿
¿ QNX
¿ RTLinux
¿ Spring
¿ RTX
¿…
27
Real-time
Process Management
& Scheduling
28
14
Processes
¿ Called tasks in the RT community
¿ Basic concepts:
¿ Task scheduling
¿ Scheduling problems & anomalies
29
Scheduling: preliminaries
¿ Key fact:
Any RT scheduling policy must be preemptive:
¿ Tasks performing exception handling may need to
preempt running tasks to ensure timely reaction
¿ Tasks may have different levels of criticalness. This can
be mapped to a preemption scheme
¿ More efficient schedules can be produced with
preemption
30
15
Scheduling: definition
¿ Given a set of tasks J = {J1 , ...Jn } a schedule is
an assignment of tasks to the processor so that
each task is executed until completion.
¿ Formally:
¿ A schedule is a function σ : R+ → N such that:
¿ ∀t ∈ R +, ∃ t1, t2 ∈ R +, ∀t∈ [t1,t2) σ(t) = σ(t‘)
¿ In practice, σ is an integer step function
σ(t) = k means task Jk is executing at time t,
σ(t) = 0 means CPU is idle
¿ Each interval [ti,ti+1) with σ(t) constant for t ∈ [ti,ti+1) is
called a time slice
31
Scheduling: example
¿ A schedule is called feasible if all tasks can be
completed according to a set of specified
constraints
¿ A set of tasks is called schedulable if there exist at
least one algorithm that can produce a feasible
schedule
¿ Example:
32
16
Scheduling constraints
¿ The following types of constraints are considered:
¿ Timing constraints
• meet your deadline
¿ Precedence constraints
• respect prerequisites
¿ Resource constraints
• access only available resources
33
Timing constraints
¿ Real-time systems are characterized mostly by
timing constraints
¿ Typical timing constraint: deadline
¿ Deadline missing separates two classes of RT
systems:
¿ Hard : missing of deadline can cause catastrophic
consequences
¿ Soft : missing of deadline decreases performance of
system
34
17
Task characterization
¿ Arrival time ai :
¿ the time Ji becomes ready for execution
¿ Also called release time ri
¿ Computation time Ci:
¿ time necessary for execution without interruption
¿ Deadline di :
¿ time before which task has to complete its execution
¿ Start time Si :
¿ time at which Ji start its execution
¿ Finishing time fi:
¿ time at which Ji finishes its execution
Ji Ci
ai Si fi di t
35
¿ Tardiness Ei:
¿ Ei = max (0, Li)
¿ time a task stays active after its deadline
36
18
Periodic and aperiodic tasks
¿ Periodic task τi consists of infinite sequence of
identical activities, called instances or job
¿ regularly activated at a constant rate
¿ Activation of first instance of τ is called ϕi
¿ Ti = period of the task
¿ Each task τi can be characterized by Ci , Ti, , Di
• Ci , Ti, , Di constant for each instance
• In most cases: Ci =Di
¿ Aperiodic task Ji consists of infinite sequence of
identical activities (instances)
¿ Their activations are not regular
37
38
19
Precedence constraints
¿ Task have often to respect some precedence
relations
¿ Described by a precedence graph G:
• Nodes N(G): tasks
• Edges E(G): precedence relations
¿ G induces partial order on task set.
39
40
20
Real-time scheduling
41
Scheduling: formulation
¿ Given:
¿ a set of n tasks J = {J1 , ..., Jn}
¿ a set of m processor P = {P1 , ..., Pm}
¿ a set of s resources R = {R1 , ..., Rs}
¿ Precedences specified using a precedence graph
¿ Timing constraints may be associated to each task.
42
21
Scheduling: classification
¿ Preemptive/non-preemptive
¿ Static
¿ scheduling decisions based on fixed parameters
¿ Dynamic
¿ scheduling decisions based on parameters that change
during system evolution
¿ Off-line
¿ scheduling algorithm is preformed on the entire task set
before start of system
¿ On-line
¿ scheduling decisions are taken at run-time every time a
task enter or leaves the system.
43
Scheduling: guarantee-based
algorithms
¿ Hard RT systems:
¿ feasibility of schedule has to be guaranteed in
advance
¿ Solutions:
¿ Static RT systems:
• Schedule all task activations can be pre-calculated off-line
• Entire schedule can be stored in table
¿ Dynamic RT systems:
• Activation of new tasks subject to acceptance test:
• J=current task set, previously guaranteed
• Jnew: newly arriving task
• Jnew: accepted iff task set J’= J Ç Jnew is schedulable
• Guarantee mechanism based on worst case assumptions Æ
pessimistic (task could unnecessarily rejected).
44
22
Scheduling metrics
Ci
n tasks
Ji
¿
¿ Average response time ai Si fi di
t
tr = Σi=1…N wi fi
¿ Maximum lateness
Lmax = maxi=1…N (fi - di)
¿ Maximum # of late tasks
Scheduling metrics
¿ Average response time/total completion time not
appropriate for hard real time tasks
¿ Maximum lateness: useful for “exploration”
¿ Max # of late task more significant
¿ Often conflicting:
Minimizing maximum
lateness does not
minimize number
of tasks that miss
their deadlines:
46
23
Aperiodic task scheduling
47
48
24
Jackson’s algorithm
[1955]
¿ 1 | sync | Lmax
¿ No other constraints are considered:
¿ tasks are independent
¿ no precedence relations
49
50
25
Jackson’s algorithm (3)
¿ Example of feasible schedule:
51
Horn’s algorithm
¿ 1 | preem| Lmax
¿ Principle: Earliest Deadline First (EDF)
¿ It can be proved that:
¿ Given a set of n independent tasks with arbitrary arrival
times, any algorithm that at any instant executes the
task with the earliest absolute deadline among all the
ready tasks is optimal with respect to minimizing the
maximum lateness.
¿ Complexity:
¿ per task: inserting a newly arriving task into an ordered
list properly:
¿ n tasks => total complexity O(n2)
26
Horn’s algorithm
¿ Example:
53
54
27
Aperiod task scheduling:
summary
55
56
28
Introduction
¿ Periodic activities represent the major
computational demand in many applications
¿ sensory data acquisition
¿ control loops
¿ system monitoring
57
Assumptions
1. Instances of a task are regularly activated at constant
rate. Interval between two consecutive activations =
period of task.
2. All instances of a task have the same Ci
3. All instances of a task have the same deadline Di,
and Di = Ti (deadline = period)
4. All periodic tasks are independent
(i.e. no precedence relations, no resource constraints )
5. No task can suspend itself (e.g. for I/0)
6. All tasks are released as soon as they arrive
7. All overheads due to the RTOS are assumed to be zero
29
Characterization of periodic tasks
¿ A periodic task τ can be characterized
(see 1-4) by:
¿ (phase φi)
¿ period Ti
¿ worst case computation time Ci
¿ Additional parameters:
¿ Response time Ri = fi – ri
¿ Release jitter of a task
• maximum deviation of the start time of two-consecutive instances:
RRJi= maxk | (si,k - ri,k ) - (si,k-1 - ri,k-1 ) |
¿ Finish jitter of a task
• maximum deviation of the finish time of two-consecutive instances:
RRJi= maxk | (fi,k - ri,k ) - (fi,k-1 - ri,k-1 ) |
¿ Critical instant (of a task):
• Release time of a task instance resulting in the largest response time
59
30
Processor utilization factor (2)
¿ Upper bound of U, Uub(Γ,A):
¿ Value of U (for a given task set and scheduling
algorithm) for which the processor is fully utilized
¿ Γ is schedulable using A but any increase of
computation time in one of the tasks makes set
infeasible
¿ Least upper bound of U, Ulub:
¿ Minimum of Uub over all task sets for a given algorithm
61
Γ3 U ub3
U
Γ4
U ub4
U ubm
Γm
0 U lub 1
YES ? NO
62
31
Scheduling of periodic tasks
¿ Static scheduling
¿ Dynamic (process-based) scheduling
63
Static scheduling
¿ With a fixed set of purely periodic tasks it is
possible to layout a schedule such that the
repeated execution of this schedule will cause all
processes to run at their correct rate.
¿ Essentially a table of procedure calls, where each
procedure represents part of a code for a
“process.”
¿ Cyclic executive approach
64
32
Cyclic executive approach
¿ Schedule is essentially a table of procedure call
¿ Tasks are mapped onto a set of minor cycles
¿ The set of minor cycles constitute a major cycle (the
complete schedule)
¿ Typically:
¿ Major cycle M = max. period among task set
¿ Minor cycle m = min. period among task set
¿ Must be: M = k·m
65
¿ Schedule:
Major cycle
(100)
Minor cycle Minor cycle Minor cycle Minor cycle
(25) (25) (25) (25)
A B C A B D E A B C A B D
23 24 23 22
66
33
Cyclic executive approach: example
¿ Actual code that
implements the above
loop
wait_for_interrupt
cyclic executive schedule Procedure_For_A
Procedure_For_B
Procedure_For_C
wait_for_interrupt
Procedure_For_A
Procedure_For_B
Procedure_For_D
Procedure_For_E
wait_for_interrupt
Procedure_For_A
Procedure_For_B
Procedure_For_C
wait_for_interrupt
Procedure_For_A
Procedure_For_B
Procedure_For_D
end loop
67
34
Process-based scheduling
¿ Fixed-priority scheduling
¿ Rate-monotonic (RM) scheduling
¿ Deadline-monotonic (DM) scheduling
¿ Dynamic-priority scheduling
¿ EDF
69
¿ Intrinsically preemptive:
¿ currently executing task is preempted by a newly
released task with shorter period.
70
35
RM scheduling
¿ RM is proved to be optimal:
¿ If any process set can be scheduled (using preemptive
priority-based scheduling) with a fixed priority-based
assignment scheme, then RM can also schedule the
process set
71
RM schedulability test
¿ Consider only the utilization of the process set
[Liu and Layland, 1973]:
n
Ci
Ulub = ∑
1
≤ n (2 − 1)
n
i = 1 Ti
U(n) 100.0%
80.0%
60.0%
40.0%
20.0%
0.0%
0 5 10 15 20 25 30 35
n
72
36
RM schedulability test (2)
¿ For large values of n, the bound asymptotically
reaches 69.3% (ln2)
¿ Any process set with a combined utilization of less
than 69.3% will always be schedulable under RM
¿ NOTE:
¿ This schedulability test is sufficient, but not
necessary
• If a process set passes the test, it will meet all deadlines; if it
fails the test, it may or may not fail at run-time
¿ The utilization-based test only gives a yes/no answer.
• No indication of actual response times of processes!
¿ More significant test: Response-time analysis
73
10
Task_1
Task_2
Task_3
0 10 20 30 40 50 60 Time
74
37
RM schedulability test: Example (2)
Process Period, Computation Priority, Utilization,
T time, C P U
Task_1 50 32 1 0.400
Task_2 40 5 2 0.125
Task_3 30 4 3 0.250
21 6 5
Task_1
Task_2
Task_3
0 10 20 30 40 50 60 Time
75
Task_1
Task_2
Task_3
0 10 20 30 40 50 60 70 80 Time
76
38
EDF algorithm
¿ Dynamic scheduling algorithm
¿ Dynamic priority assignment
¿ Idea as for aperiodic tasks:
¿ Tasks are selected according to their absolute deadlines
¿ Tasks with earlier deadlines are given higher priorities
77
Example
Process Period, T WCET, C
T1 5 2
T2 7 4
EDF schedule
T1
0 5 10 15 20 25 30 35
T2
0 7 14 21 28 35
RMA schedule
T1
0 5 10 15 20 25 30 35
Deadline miss
3 1
T2
0 7 14 21 28 35
78
39
EDF schedulability test
¿ Schedulability of a periodic task set scheduled by
EDF can be verified through the processor
utilization factor U
¿ EDF Schedulability Theorem [Liu and Layland, 1973]:
A set of periodic tasks is schedulable with EDF iff:
n
Ci
∑
i =1 Ti
≤1
79
80
40
Deadline monotonic
(DM) scheduling
¿ Assumption up to now:
relative deadline = period
¿ DM scheduling weakens this assumption
¿ Static algorithm
¿ For DM each periodic tasks τ i is characterized by
four parameters:
¿ (phase φi)
¿ Relative deadline Di (equal for all instances)
¿ Worst case computation time Ci (equal for all instances)
¿ Period Ti
81
DM scheduling
¿ DM = generalization of RM
¿ RMA optimal for D = T
¿ DMA extends this optimality for D < T
¿ Priority of a process inversely proportional to its
deadline
¿ Given tasks τi and τj, Di < Dj Æ Pi > Pj
τi Ci
t
Di
Ti
82
41
DM scheduling: example
¿ Task set not schedulable with RM but schedulable
with DM
Process Period, Deadline, Computation Priority, Response
T D time, C P time, R
Task_1 20 5 3 4 3
Task_2 15 7 3 3 6
Task_3 10 10 4 2 10
Task_4 20 20 3 1 20
83
DM schedulability analysis
¿ Schedulability can be tested replacing the period
with the deadlines in the definition of U
U = Σi=1…n Ci/Di
¿ Too pessimistic!
¿ Actual guarantee test based on a modified
response time analysis
¿ Intuitively: for each τi , the sum of its processing time
and the interference (preemption) imposed by higher
priority tasks must be ≤ Di
Ci + Ii ≤ Di ∀i: 1 ≤ i ≤ n
Ii = Σ(j=1…i-1) Ri / Tj Cj
84
42
EDF for D<T
¿ EDF applies as is to the case D < T
¿ Different schedulability test
¿ Based on the processor demand criterion
¿ The processor demand of a task τi in any interval
[t, t+L] is the amount of processing time required
by τi in [t, t+L] that has to be completed at or
before t+L
¿ With deadlines, this is the amount of processing
time required in [t, t+L] that has to be executed
with deadlines ≤ t+L
85
∀L ≥ 0
Number of checkpoints
Is actually limited Number of completions
Between 0 and L-Di
86
43
Processor demand for EDF: example
¿ Consider:
Task T D C
τ1 8 7 3
τ2 8 4 2
7 15 23
τ1
4 12 20
τ2
L L
¿ Schedulability test (L=22):
¿ ((22-7)/8+1) • 3 + ((22-4)/8+1) • 2 =
¿ 6 + 6 = 12 < 22 OK
¿ Schedulability test (L=24):
¿ ((24-7)/8+1) • 3 + ((24-4)/8+1) • 2 =
¿ 9 + 6 = 15 < 22 OK
87
88
44
Periodic task scheduling:
summary
Di = Ti Di ≤ Ti
RMA DMA
i =1 i
T
EDF EDF
Processor utilization Processor demand approach
approach
n
Ci n
L − Di
Dynamic ∑ T ≤1 ∀ L > 0, L ≥ ∑ + 1 Ci
Priority i =1 i i =1 Ti
89
Priority servers
90
45
Introduction
¿ Requirements in most real-time applications:
¿ periodic and aperiodic tasks
• typically periodic tasks are time-driven, hard real-time
• typically aperiodic tasks are event-driven, soft or hard RT
¿ Main objective for RT kernel:
• guarantee hard RT tasks
• provide good average response time for soft RT
¿ Fixed priority servers:
¿ Periodic tasks scheduled by fixed priority algorithm
¿ Dynamic priority servers:
¿ Periodic tasks scheduled by dynamic priority algorithm
91
Background scheduling
¿ Simplest solution:
¿ Handle soft aperiodic tasks in the background behind
periodic tasks, that is, in the processor time left after
scheduling all periodic tasks.
¿ Aperiodic tasks just get assigned a priority lower than
any periodic one.
¿ Organization of background scheduling:
Periodic tasks
High priority queue RM
CPU
FCFS
Aperiodic tasks
Low priority queue
92
46