Module 5
Module 5
When a fighter jet control system runs on GPOS encountered a hill, and you attempted to avoid…
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 3
RTOS - INTRODUCTION
NEED FOR RTOS
While you drive your car at high speed, suddenly collided with another vehicle if airbag activation system
runs on EOS opens only after 10 sec….
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 4
RTOS - INTRODUCTION
WHAT IS 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
➢ “A real-time operating system (RTOS) is an operating system (OS) intended to
serve real-time application requests.” - Wikipedia
➢ “RTOS: Any OS where interrupts are guaranteed to be handled within a
certain specified time, there by making it suitable for control hardware in
embedded system & time critical applications.” - [Link]
➢ A real-time operating system (RTOS) is an operating system that guarantees
a certain capability within a specified time constraint to serve real time
embedded applications.
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 5
RTOS - INTRODUCTION
RTOS - FEATURES
➢ Multitasking and Pre-emptibility: An RTOS must be multi-tasked and pre-
emptible to support multiple tasks in real-time applications.
➢ Task Priority: In RTOS, pre-emption capability is achieved by assigning
individual task with the appropriate priority level.
➢ Inter Task Communication: For multiple tasks to communicate in a timely
manner and to ensure data integrity among each other, reliable and sufficient
inter-task communication and synchronization mechanisms are required.
➢ Priority Inheritance: To allow applications with stringent priority requirements
to be implemented, RTOS must have a sufficient number of priority levels
when using priority scheduling.
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 6
RTOS - INTRODUCTION
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.
Scalability is the
Consistency Reliability ability of a system to
handle increasing
workload, size, or
complexity without
degrading
performance.
Scalability Predictability
Performance
➢ Examples:
➢ Missile Navigation Systems
➢ Vehicle Air Bags Control
➢ Nuclear Power Plant Control
➢ Examples:
➢ Robot in car assembly section
➢ Food processing control system
➢ Weather monitoring system
➢ 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
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 11
RTOS - INTRODUCTION
RTOS - APPLICATIONS
Mars Curiosity Rover, Uses VxWorks (For Split-second decision taking) and µc/os-II (For Sample Analysis)
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 13
RTOS - INTRODUCTION
RTOS - APPLICATIONS
F-35 Fighter aircraft uses a Integrity DO-178B, a POSIX based RTOS developed by Green Hills Software
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 14
RTOS - INTRODUCTION
RTOS - APPLICATIONS
International medical technology group Elekta is basing its new generations of equipment on the LynxOS-SE (RTOS)
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 15
RTOS - INTRODUCTION
RTOS - APPLICATIONS
ThreadX RTOS is used by HP (All Printers) & Honeywell (Advanced security systems) in consumer electronics devices
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 16
RTOS - INTRODUCTION
RTOS IN EMBEDDED MARKET
There are more than 100 RTOS currently available in embedded market
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 17
RTOS - INTRODUCTION
MOST COMMONLY USED RTOS
20
Applications
RTOS
BSP
Hardware
• RTOS Layer
• This is the core software layer that manages system resources.
• Kernel (Heart of RTOS)
• The kernel provides:
• Task/Thread Management
• Scheduling (priority-based)
• Context Switching
• Inter-task Communication
• Synchronization (Mutex, Semaphore)
• Interrupt Handling
• Other RTOS Components (Middleware & Services)
• File System → Manages storage
• Networking Protocols → TCP/IP stack
• POSIX Support → Standard APIs compatibility
• C/C++ Libraries → Runtime support
• Device Drivers → Interface with hardware
• Device I/O → Peripheral handling
• Debugging Facilities → Trace, diagnostics
• Other Components → Timers, memory management, etc.
• These components make the RTOS scalable and feature-rich.
• Target Hardware
• This is the physical system:
• Microcontroller / Microprocessor
• RAM / ROM
• Peripherals (UART, SPI, I2C, ADC, Timers)
• Sensors and actuators
RTOS - ARCHITECTURE
RTOS – KERNEL
➢ Its services include managing memory and devices and also to provide an
interface for software applications to use the resources.
➢ Micro kernel
➢ Includes very small number of services within the kernel in an attempt to keep it small
and scalable.
➢ The services typically include low-level memory management, inter-process
communication (IPC), basic process synchronization to enable processes to cooperate.
➢ Hybrid kernel
➢ It has a 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.
MODULE-6 ECE4003-EMBEDDED SYSTEM DESIGN 26
RTOS - ARCHITECTURE
EOS - KERNEL
Monolithic Kernel
Microlithic Kernel
Hybrid Kernel
BASIS FOR
MONOLITHIC KERNEL MICROKERNEL HYBRID KERNEL
COMPARISON
Basic In monolithic kernel, both user In microkernel user services and Hybrid kernel is a kernel
services and kernel services are kernel, services are kept in separate combining aspects from both
kept in the same address space. address space. Monolithic and Microkernel
designs
Size larger than microkernel Microkernel are smaller in size. smaller than monolithic kernel
Execution Fast execution. Slow execution. Moderate speed
Extendible The monolithic kernel is hard to The microkernel is easily extendible. Dynamically loadable module
extend.
Security If a service crashes, the whole If a service crashes, it does effect on It depends on service called
system crashes in monolithic working of microkernel.
kernel.
Code To write a monolithic kernel, more To write a microkernel, less code is Moderate code size
code is required. required.
Example Linux, BSDs, Windows (95,98,Me), QNX, Symbian, L4Linux, Singularity, Windows NT and above, Mac OS
Solaris, OS-9, AIX, HP-UX, DOS. K42, Integrity, PikeOS,. X, BeOS, ReactOS, Syllable.
➢ All other basic services can be made part of user space and can be run in the
form of servers.
QNX Microkernel
Microkernel Model
➢ 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.
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 31
RTOS - ARCHITECTURE
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
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 32
PERFORMANCE METRICS
OF RTOS
33
➢ 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.
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 34
PERFORMANCE METRICS OF RTOS
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
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 35
PERFORMANCE METRICS OF RTOS
RTOS PERFORMANCE METRICS
➢ Performance of kernel services:
➢ An RTOS is likely to have many API (application program interface) calls, probably
numbering into the hundreds.
➢ To assess timing, it is not useful to try to analyse 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
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 36
PERFORMANCE METRICS OF RTOS
RTOS CONSIDERATIONS
➢ What are the real-time capabilities? Is it soft or hard real-time interrupt handling
➢ What is the target processor, and does it support the RTOS you have in mind?
➢ Does your processor development board have a BSP with your RTOS?
➢ Which development environments and debugging tools work with the RTOS?
➢ How flexible is the choice of scheduling algorithms in the RTOS? E.g., FIFO, round Robin,
rate-monotonic, sporadic, etc.
39
➢ Two or more instances of an aperiodic task might occur at the same time instant
because minimum separation gi, between two consecutive instances can be 0.
➢ Aperiodic task can recur in quick succession, therefore it is very difficult to meet
the deadlines of all instances of an aperiodic task.
➢ Soft real time systems can tolerate a few deadline misses. so, aperiodic tasks
are generally soft real time tasks.
➢ E.g. Logging task in a distributed systems. The logging task can be started by
different tasks running on different nodes.
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 43
• Sporadic Task
• Example:
• Emergency sensor interrupt:
• It may occur anytime.
• But cannot occur more frequently than once every 20 ms.
This makes it analyzable.
• Aperiodic Task
• Example:
• Keyboard press
• User may press keys anytime.
• Two presses can occur very close together.
TASK SPECIFICATIONS
REAL-TIME TASK - ASSUMPTIONS
➢ CPU utilization is a measure on how busy the processor could be during the
shortest repeating cycle: P1*P2*...*Pn
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 49
SCHEDULABILITY ANALYSIS
SCHEDULABILITY TEST
➢ Schedulability test determine whether a given task set is feasible to schedule?
➢ Necessary test:
➢ If test is passed, tasks may be schedulable but not necessarily
➢ If test is not passed, tasks are definitely not schedulable
➢ Sufficient test:
➢ If test is passed, then task are definitely schedulable
➢ If test is not passed, tasks may be schedulable but not necessarily
52
➢ By switching the CPU among processes, the operating system can make the
embedded system is more productive.
➢ The need for a scheduling algorithm arises from requirement for most modern
systems to perform multitasking.
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 53
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.
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 54
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)
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 55
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)
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 56
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
Since given task set passes Sufficient test we may
Necessary Test is passed hence given task set must be tested under sufficient test
conclude it is definitely schedulable under RMS
to conclude the Schedulability. But, if necessary test is failed we may conclude
given task set is definitely not schedulable by any scheduling algorithm.
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 57
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
MODULE-6
REAL TIME SCHEDULING
RATE MONOTONIC SCHEDULING(RMS) – EXAMPLE-2
➢ Consider set of four task (Ci, Pi) running on embedded system as follows,
T1= (1, 3) , T2= (1, 5) , T3= (1, 6) , T4= (2, 20)
Necessary Test Sufficient Test
𝑁 𝑁
𝐶𝑖 𝐶𝑖 1
𝑈= ≤1 𝑈= ≤ 𝑁(2 − 1)
𝑁
𝑃𝑖 𝑃𝑇𝑖
𝑖=1 𝑖=1
U=(1/3)+(1/5)+(1/6)+(2/10)=0.899 ≤ 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 due to
to conclude the Schedulability. But, if necessary test is failed we may conclude U=0.899 > B(4) we can’t conclude precisely whether
given task set is definitely not schedulable by any scheduling algorithm. given task set is schedulable or not under RMS
MODULE-6
REAL TIME SCHEDULING
RATE MONOTONIC SCHEDULING(RMS) – EXAMPLE-3
➢ Consider set of three task (Ci, Pi) running on embedded system as follows,
T1= (10, 30) , T2= (15, 40) , T3= (5, 50)
Necessary Test Sufficient Test
𝑁 𝑁
𝐶𝑖 𝐶𝑖 1
𝑈= ≤1 𝑈= ≤ 𝑁(2 − 1)
𝑁
𝑃𝑖 𝑃𝑇𝑖
𝑖=1 𝑖=1
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 due to
to conclude the Schedulability. But, if necessary test is failed we may conclude U=0.808 > B(3) we can’t conclude precisely whether
given task set is definitely not schedulable by any scheduling algorithm. given task set is schedulable or not under RMS
MODULE-6
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
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
T1
T2
T3
MODULE-6
REAL TIME SCHEDULING
EARLIEST DEADLINE FIRST (EDF) – EXAMPLE-1
T1
T2
T3
Although given task set failed to schedule under RMS, it can scheduled by EDF
MODULE-6
REAL TIME SCHEDULING
EARLIEST DEADLINE FIRST (EDF) – EXAMPLE-2
➢ Consider set of four task (Ci, Pi) running on embedded system as follows,
T1= (1, 3) , T2= (1, 5) , T3= (1, 6) , T4= (2, 20)
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
T1
T2
T3
MODULE-6
REAL TIME SCHEDULING
EARLIEST DEADLINE FIRST (EDF) – EXAMPLE-3
➢ Consider set of three task (Ci, Pi) running on embedded system as follows,
T1= (5, 20) , T2= (10, 40) , T3= (40, 80)
Necessary Test
𝑁
𝐶𝑖
𝑈= ≤1
𝑃𝑖
𝑖=1
U= (5/20)+(10/40)+(40/80)
= 0.25 + 0.25 + 0.5 = 1 ≤ 1
If cumulative CPU utilization is 1 then the CPU will be utilized 100% without idle condition
T1
T2
T3
At time instance 80ms all 3 tasks are having same deadline. When multiple tasks having deadline at same
instance of a time then one of the task will be chosen randomly and get executed.
MODULE-6
REAL TIME SCHEDULING
EARLIEST DEADLINE FIRST (EDF) - PROs & CONs
➢ PROs:
➢ It works for all types of tasks: periodic or non periodic
➢ Simple Schedulability test
➢ Best CPU utilization
➢ Optimal scheduling algorithm
➢ CONs:
➢ Difficult to implement due to the dynamic priority-assignment.
➢ Any task could get the highest priority even if the task is not important
➢ Non stable because if any task fails to meet its deadline, the system is
not predictable
MODULE-6 CSE3006 -EMBEDDED SYSTEM DESIGN 72