EmSys Module 5
EmSys Module 5
Real Time
Operating System
BCSE305L
Dr. Nitish Katal
1
Outline
▪ Introduction to Real-Time Concepts
▪ RTOS Internals & Real Time Scheduling,
▪ Performance Metrics of RTOS,
▪ Task Specifications,
▪ Schedulability Analysis,
▪ Application Programming on RTOS.
2
Why RTOS?
▪ Why NOT GPOS or EOS?
3
Why RTOS?
▪ When a fighter jet control system runs on GPOS encountered
a hill, and you attempted to avoid…
4
Why RTOS?
Any car driven at high speed, suddenly collided with another
vehicle if airbag activation system runs on EOS opens only
after 10 sec….
5
RTOS
❑ Real time in operating systems is the ability of the OS to provide a
required level of service in a bounded response time.”
- POSIX Standard 1003.1
❑ Task Priority:
❑ In RTOS, pre-emption capability is achieved by assigning individual
task with the appropriate priority level.
❑ Priority Inheritance:
❑ To allow applications with stringent priority requirements to be
implemented, RTOS must have a sufficient number of priority levels
7
when using priority scheduling.
RTOS : Features
❑ Control of Memory Management: To ensure predictable
response to an interrupt, an RTOS should provide way for task
to lock its code and data into real memory.
❑ Predefined Short Latencies:
❑ Task switching latency: The time needed to save the context of a
currently executing task and switching to another task is
desirable to be short.
❑ Interrupt latency: The time elapsed between execution of the
last instruction of the interrupted task and the first instruction in
the interrupt handler.
❑ Interrupt dispatch latency: The time from the last instruction
in the interrupt handler to the next task scheduled to run. 8
RTOS : Features
Reliability
Consistency Predictability
Scalability Performance
9
RTOS : Types
HARD RTOS
❑ Strictly adhere to the deadline associated with the tasks and the
degree of tolerance for missed deadlines is negligible.
❑ A missed deadline can result in catastrophic failure of the system
❑ Examples:
❑ Missile Navigation Systems
❑ Vehicle Air Bags Control
❑ Nuclear Power Plant Control
10
RTOS : Types
FIRM RTOS
❑ Tolerates a low occurrence of missing a deadline.
❑ Missing a deadline may result in an unacceptable reduction in
quality of a product not lead to failure of the complete system.
❑ Examples:
❑ Robot in car assembly section
❑ Food processing control system
❑ Weather monitoring system
11
RTOS : Types
SOFT RTOS
❑ Allows for frequently missed deadlines, and as long as tasks
are timely executed their results continue to have value.
❑ Even the soft real time systems cannot miss the deadline for
every task or process according to the priority it should meet the
deadline. (Best effort system)
❑ Examples:
❑ Multimedia transmission & reception
❑ Digital cameras & mobile phones
❑ Computer games
12
RTOS : Application
13
RTOS : Application
Mars Curiosity Rover, Uses VxWorks F-35 Fighter aircraft uses a Integrity
(For Split-second decision taking) and DO-178B, a POSIX based RTOS
µc/os-II (For Sample Analysis) developed by Green Hills Software 14
RTOS : Application
17
RTOS : Architecture
Applications
RTOS
BSP
Hardware
18
RTOS : Kernal
❑ Kernel is the smallest and core component of an OS.
19
RTOS : Kernal
❑ Monolithic Kernel
❑ Entire OS (device drivers, file system, and IPC) is working in kernel
space.
❑ Each component of Monolithic kernel could communicate directly with any
other component, and had unrestricted system access.
❑ Micro Kernel
❑ Includes very small number of services within the kernel in an attempt
to keep it small and scalable.
❑ Services typically include low-level memory management, inter-process
communication (IPC), basic process synchronization to enable processes to
cooperate.
❑ Hybrid Kernel
❑ Structure similar to microkernels, but implemented as a monolithic
kernel.
❑ Hybrid kernels have the ability to pick and choose what they want to run in
user mode and what they want to run in kernel mode. 20
RTOS : Kernal
Monolithic Kernel
Microlithic Kernel
Hybrid Kernel
21
RTOS : Kernal
BASIS FOR
MONOLITHIC KERNEL MICROKERNEL HYBRID KERNEL
COMPARISON
Both user services and User services and kernel services Kernel combining aspects
Basic kernel services are kept in are kept in separate address from both Monolithic and
the same address space. space. Microkernel designs
23
RTOS : Kernal
QNX Microkernel
Microkernel Model
24
RTOS : BSP
❑ In embedded systems, a board support package (BSP) is the
layer of software containing hardware-specific drivers and
other routines
❑ This allow a particular OS (traditionally a RTOS) to function in
a particular hardware environment integrated with the RTOS
itself.
❑ Third-party hardware developers who wish to support a
particular RTOS must create a BSP that allows that RTOS to run
on their platform.
❑ BSPs are typically customizable, allowing the user to specify
which drivers and routines should be included in the build based
on their selection of hardware and software options.
25
RTOS : BSP
❑ For instance, a particular single-board computer might be
paired with any of several graphics cards; in that case the
BSP might include a driver for each.
❑ While building the BSP image the user would specify which
graphics driver to include based in his choice of hardware.
❑ BSP is supposed to perform the following operations
❑ Initialize the processor
❑ Initialize the bus
❑ Initialize the interrupt controller
❑ Initialize the clock
❑ Initialize the RAM settings
❑ Load and run boot loader from flash
26
Performance Metrics
of RTOS
27
RTOS : Performance Metrics
❑ Three areas of interest when looking at the performance and
usage characteristics of an RTOS:
❑ (1) Memory (2) Latency (3) Kernel services
❑ Memory:
❑ How much ROM and RAM does the kernel need and how is this
affected by options and configuration?
❑ Factors that affect the memory footprint includes static or dynamic
configuration of Kernel, number of instruction related to processor, global
variable declaration etc.,
❑ With the help of optimization setting in embedded compilers code size
can be reduced, but that will most likely affect performance.
❑ Most RTOS kernels are scalable, but some RTOSes, scalability only
applies to the kernel. For others, scalability is extended to the rest of the
middleware.
28
RTOS : Performance Metrics
❑ Latency:
❑ Interrupt latency: It is the sum of the hardware dependent time,
❑ Which depends on the interrupt controller as well as the type of the interrupt,
and the OS induced overhead.
❑ Ideally, it should include the best and worst case scenarios.
❑ To measure a time interval, like interrupt latency, with any accuracy,
requires a suitable instrument and the best tool to use is an oscilloscope.
❑ Scheduling latency: Being real time, the efficiency at which threads or
tasks are scheduled is of some importance and the scheduler is at the core of
an RTOS.
❑ It is hard to get a clear picture of performance, as there is a wide variation in the
techniques employed to make measurements and in the interpretation of the
results.
❑ There are really two separate measurements to consider:
❑ (1) The context switch time
❑ (2) The time overhead that the RTOS introduces when scheduling a task 29
RTOS : Performance Metrics
❑ Performance of Kernel Services:
❑ An RTOS is likely to have many API calls, probably numbering into
the hundreds.
❑ To assess timing, it is not useful to try to analyze the timing of
every single call. It makes more sense to focus on the frequently used
services.
❑ For most RTOSes, there are four key categories of service call:
❑ Threading services
❑ Synchronization services
❑ Inter-process communication services
❑ Memory services 30
RTOS : Performance Metrics
❑ What are the real-time capabilities? Is it soft or hard real-time
interrupt handling
❑ What are the preemptive scheduling services?
❑ What is the target processor, and does it support the RTOS you
have in mind?
❑ How large is the RTOS memory footprint?
❑ What are the RTOS interrupt latencies?
❑ How long does it take the RTOS to switch contexts?
❑ Does your processor development board have a BSP with your
RTOS?
31
RTOS : Performance Metrics
❑ Which development environments and debugging tools work
with the RTOS?
❑ How much does the RTOS cost, is it open source or royalty-free?
❑ Does the RTOS have good documentation and/or forums?
❑ What is the RTOS supplier’s reputation?
❑ How flexible is the choice of scheduling algorithms in the
RTOS? E.g., FIFO, round Robin, rate-monotonic, sporadic, etc.
❑ Are there tools for remote diagnostics?
32
Task
Specifications
33
Task Specifications : RTS
REAL-TIME TASK
❑ Real time tasks normally recur a large number of times at
different instants of time depending on event occurrence time.
❑ Ex.: A temperature sensing task in chemical plant might recur
indefinitely with a certain period because the temperature is sampled
periodically,
❑ whereas a task handling a device interrupt might recur at random
instants.
❑ A real-time task is defined to be one in which there is a hard
deadline to meet; failure to meet the deadline can lead to disaster,
❑ Ex: task include process control such as a nuclear reactor,
automatic flight control, etc.
❑ Based on the way real-time tasks recur over a period of time, it is
possible to classify them into
❑ (1) Periodic tasks
❑ (2) Sporadic tasks
❑ (3) Aperiodic tasks 34
Task Specifications : RTS
REAL-TIME TASK - PERIODIC
❑ A periodic task is one that repeats after a certain fixed time interval
also referred as clock-driven tasks.
❑ A vast majority of the tasks present in a typical real-time system are
periodic, for example, monitoring certain conditions, polling
information from sensors at regular intervals to carry out certain action
at regular intervals.
❑ Periodic task 𝑻𝒊 can be represented using four tuples: (Ø𝒊 , 𝒑𝒊 , 𝒆𝒊 , 𝒅𝒊 )
❑ Where,
❑ Ø𝒊 - phase of the task
❑ 𝒑𝒊 , - Period of task
❑ 𝒆𝒊 , - Worst case execution time of the task
❑ 𝒅𝒊 , - Relative deadline of the task
35
Task Specifications : RTS
REAL-TIME TASK - SPORADIC
❑ A sporadic task is one that recurs at random instants.
❑ E.g. In a robot a task that gets generated to handle an obstacle that
appears suddenly is a sporadic task. In a factory , the task that handles
fire condition is a sporadic task.
❑ Sporadic task 𝑻𝒊 can be represented using three tuples: 𝒆 𝒊 , 𝒈𝒊 , 𝒅𝒊
❑ Where, 𝒆𝒊 - Worst case execution time of the task
❑ 𝒈𝒊 - the minimum separation between two consecutive instances of the task.
❑ 𝒅𝒊 - Relative deadline of the task
38
Schedulability
Analysis
39
Scheduling Criteria
❑ CPU utilization: CPU should be working most of the time (Ideally 100% all
the time)
❑ Throughput: total number of processes completed per unit time(10 tasks/sec.)
❑ Turnaround time (TAT): amount of time taken to execute a particular
process, TATi=CTi–ATi (Where CTi → Completion Time, ATi → Arrival Time)
❑ Waiting time(WT): time periods spent waiting in the ready queue by a
process to acquire get control on the CPU,
❑WTi = TATi – BTi (Where BTi → CPU burst time)
❑ Sufficient test:
❑ If test is passed, then task are definitely schedulable
❑ If test is not passed, tasks may be schedulable but not necessarily
44
Real Time Scheduling
❑ Scheduling refers to the way processes are assigned to run on the
available CPUs, since there are typically many more processes running
on the system.
❑ Real-time scheduling deals with the problem of deciding which of the
processes in the ready queue is to be allocated the CPU in RTOS.
❑ By switching the CPU among processes, the operating system can make
the embedded system is more productive.
❑ Scheduling algorithm is the method by which threads, processes or data
flows are given access to system resources
❑ The need for a scheduling algorithm arises from requirement for most
modern systems to perform multitasking. 45
Real Time Scheduling
REAL-TIME SCHEDULING ALGORITHM
❑ The purpose of a real-time scheduling algorithm is to ensure that critical timing
constraints, such as deadlines and response time, are met.
❑ Real-time systems use preemptive multitasking with priorities are assigned to tasks,
and the RTOS always executes the task with highest priority.
❑ Most algorithms are classified as
❑ Fixed-priority: Algorithm assigns priorities at design time, and those priorities
remain constant for the lifetime of the task. Ex.: Rate-Monotonic Scheduling
(RMS)
❑ Dynamic-priority: Assigns priorities dynamically at runtime, based on execution
parameters of tasks, such as upcoming deadlines. Ex.: Earliest Deadline First
(EDF)
❑ Mixed-priority: This algorithm has both static and dynamic components.
46
Real Time Scheduling
TYPES OF SCHEDULING ALGORITHM
❑ Pre-emptive Algorithm:
❑ Shortest remaining Time First(SRTF)
❑ Round Robin(RR)
❑ Priority(Pre-emptive)
❑ Rate Monotonic Scheduling (RMS)
❑ Earliest Deadline First (EDF)
47
Real Time Scheduling
RATE MONOTONIC SCHEDULING (RMS)
❑ Concept: Task with the shortest period executes with the highest priority.
❑ Working :
❑ Rate-monotonic is a fixed priority based scheduling.
❑ The scheduling scheme is pre-emptive; it ensures that a task is pre-
empted if another task with a shorter period is expected to run.
❑ Used in embedded systems where the nature of the scheduling is
deterministic.
❑ Consider three tasks with a period Task-1(10ms), Task-2(15ms), Task-
3 (20ms), then as per RMS priority of the tasks can be assigned as:
priority (task1) > priority (task2) > priority (task3)
48
Real Time Scheduling
RATE MONOTONIC SCHEDULING(RMS) – EXAMPLE-1
➢ Consider set of task running on a Automotive control system as follows
➢ Speed measurement Task (T1): C=20ms, P=100ms, D=100ms
➢ ABS control Task (T2): C=40ms, P=150ms, D=150ms
➢ Fuel injection Task (T3): C=100ms, P=350ms, D=350ms
➢ Other software with soft deadlines e.g. audio, air condition etc.,
U=(20/100)+(40/150)+(100/350) B(3)=0.779
= 0.2 + 0.2666 + 0.2857 = 0.7517 ≤ 1 U=0.7517 ≤ B(3) ≤ 1
▪ Necessary Test is passed hence given task set must be tested under sufficient Since given task set passes Sufficient test we may
test to conclude the Schedulability. conclude it is definitely schedulable under RMS
▪ But, if necessary test is failed we may conclude given task set is definitely not
49
schedulable by any scheduling algorithm.
Real Time Scheduling
RATE MONOTONIC SCHEDULING(RMS) – EXAMPLE-1
This is only for the first periods. But this is enough to conclude that the task set is schedulable
50
Real Time Scheduling
RATE MONOTONIC SCHEDULING(RMS) – EXAMPLE-2
U=(1/3)+(1/5)+(1/6)+(1/10)=0.8 ≤ 1 B(4)=0.756
Necessary Test is passed hence given task set must be tested under sufficient test The given task set fails in the Sufficient test
to conclude the Schedulability. But, if necessary test is failed we may conclude due to U=0.8 > B(4) we can’t conclude
given task set is definitely not schedulable by any scheduling algorithm. precisely whether given task set is
schedulable or not under RMS
51
Real Time Scheduling
RATE MONOTONIC SCHEDULING(RMS) – EXAMPLE-2
52
Real Time Scheduling
RATE MONOTONIC SCHEDULING(RMS) – EXAMPLE-3
U=(10/30)+(15/40)+(5/50)=0.808 ≤ 1 B(3)=0.779
Necessary Test is passed hence given task set must be tested under sufficient test The given task set fails in the Sufficient test
to conclude the Schedulability. But, if necessary test is failed we may conclude due to U=0.808 > B(3) we can’t conclude
given task set is definitely not schedulable by any scheduling algorithm. precisely whether given task set is
schedulable or not under RMS
53
Real Time Scheduling
RATE MONOTONIC SCHEDULING(RMS) – EXAMPLE-3
54
Real Time Scheduling
RATE MONOTONIC SCHEDULING(RMS) - PROs & CONs
❑ PROs:
❑ Simple to understand
❑ Easy to implement (static/fixed priority assignment)
❑ Stable: though some of the lower priority tasks fail to meet deadlines,
others may meet deadlines
❑ CONs:
❑ Lower CPU utilization
❑ Requires D=T
❑ Only deal with independent tasks
❑ Non-precise Schedulability analysis
55
Real Time Scheduling
EARLIEST DEADLINE FIRST (EDF)
❑ Concept: Task closest to the end of its period assigned the highest priority
❑ Working :
❑ Earliest deadline first is a dynamic priority based scheduling.
❑ The scheduling scheme is pre-emptive; it ensures that a task is pre-empted
if another task having a nearest deadline is expected to run.
❑ EDF is optimal scheduling i.e it can schedule the task set if any other
scheduling algorithm can schedule.
❑ If two task have the same deadlines, need to chose one of the two at
random but in most of the case it proceed with the same task which is
currently executing on the CPU to avoid context switching overhead.
56
Real Time Scheduling
EARLIEST DEADLINE FIRST (EDF) – EXAMPLE-1
➢ Consider set of task running on a weather monitoring station as follows
➢ Temperature measurement Task (T1): C=1ms, P=4ms
➢ Humidity measurement Task (T2): C=2ms, P=5ms
➢ Co2 measurement Task (T3): C=2ms, P=7ms
Necessary Test
𝑁
𝐶𝑖
𝑈= ≤1
𝑃𝑖
𝑖=1
U=(1/4)+(2/5)+(2/7)
= 0.25 + 0.4+ 0.2857
= 0.935 ≤ 1
Necessary Test is passed hence given task set must be schedulable by EDF
57
Real Time Scheduling
EARLIEST DEADLINE FIRST (EDF) – EXAMPLE-1
58
Real Time Scheduling
EARLIEST DEADLINE FIRST (EDF) – EXAMPLE-1
Although given task set failed to schedule under RMS, it can scheduled by EDF
59
Real Time Scheduling
EARLIEST DEADLINE FIRST (EDF) – EXAMPLE-2
Necessary Test
𝑁
𝐶𝑖
𝑈= ≤1
𝑃𝑖
𝑖=1
U=(1/3)+(1/5)+(1/6)+(2/10)=0.899 ≤ 1
Necessary Test is passed hence given task set must be schedulable by EDF
60
Real Time Scheduling
EARLIEST DEADLINE FIRST (EDF) – EXAMPLE-2
61
Real Time Scheduling
EARLIEST DEADLINE FIRST (EDF) – EXAMPLE-3
Necessary Test
𝑁
𝐶𝑖
𝑈= ≤1
𝑃𝑖
𝑖=1
U= (5/20)+(10/40)+(40/80)
= 0.25 + 0.25 + 0.5 = 1 ≤ 1