RTOS FULL Notes
RTOS FULL Notes
Ba
m
tta
U
linkedin.com/in/uttambasu
What is an OS?
Has direct access to hardware.
su
OS
Ba
Kernel
m
tta
What is RTOS?
Real-�me opera�ng systems have similar func�ons as general-purpose OS (GPOS), like Linux,
Microso� Windows, but are designed to run applica�ons with very precise �ming. RTOS is
designed for cri�cal systems and for devices like microcontrollers that are �ming-specific.
1
linkedin.com/in/uttambasu
Hard vs so� RTOS:
Hard real-�me:
o A system that can always meet the desired deadlines (100 percent), even under a
worst-case system load. In hard real-�me systems, missing a deadline, even a single
�me, can have fatal consequences. Hard real-�me systems are used in cases where a
par�cular task, usually involving life safety issues, needs to be performed within a
par�cular �me frame, otherwise a catastrophic event will occur.
So� real-�me:
o A system that can meet the desired deadlines on average. A so� real-�me system will
give reduced average latency but not a guaranteed maximum response �me.
o So� RTOS can miss a few deadlines (such as dropping a few frames in a video
applica�on) without failing the overall system.
su
Ba
Task:
tta
Example:
for(;;)
2
linkedin.com/in/uttambasu
Mul�tasking,
Polling:
Interrupt
o An external or internal event that interrupts the processor to inform it that a device
m
o When an event happens, the processor jumps to the Interrupt Service rou�ne (ISR).
U
What is a Resource?
A shared Resource is a resource that can be used by more than one task.
3
linkedin.com/in/uttambasu
o Each task should have exclusive access to the shared resource to prevent data
corrup�on.
o There are techniques to ensure exclusive access of the resource like mutual exclusion.
RTOS Kernel
su
Kernel is the core component of any OS.
Ba
kernel components:
m
o Objects are special kernel constructs that help developers create applica�ons for real-
U
4
linkedin.com/in/uttambasu
Priority Based Kernels
o Are Kernels that decide which task will run regarding its priority.
o Most real �me kernels are priority based.
o Each task takes a priority based on its importance.
o The Highest priority task is always ready to run.
o There are two types of priority-based kernels: Non-Preemp�ve and Preemp�ve
Kernel.
su
Ba
m
tta
U
5
linkedin.com/in/uttambasu
Non preemp�ve kernel:
o An ISR (Interrupt Service Rou�ne) makes higher priority task ready to run.
o The ISR always return to the interrupted task.
o The new higher priority task will run when the current task gives up the CPU.
o This minimizes the data corrup�on risks in mul�tasking as each task finish before the
other begins.
o Task response �me will equal to the �me of the longest task.
su
Ba
m
tta
U
Preemp�ve kernel:
Here the highest priority task ready to run always given the control to the CPU.
6
linkedin.com/in/uttambasu
RTOS Scheduler
regarding to this priority the scheduler will decide which task will run.
o The scheduler assumes that you knew what you were doing while se�ng tasks
m
priority.
tta
o A bad design for tasks priority, may leads to a high priority task hogs the processor for
U
o Some RTOSs make it illegal to set two tasks with the same priority, and here the
kernel limits the number of tasks in an applica�on to the number of priority levels.
o Others will �me slice between the two tasks (Round robin).
7
linkedin.com/in/uttambasu
o Some will run one task un�l it blocks the run of the other task.
System tasks
o ini�aliza�on or startup task: Ini�alizes the system and creates and starts system
tasks,
o idle task: uses up processor idle cycles when no other ac�vity is present, set to the
lowest priority, and executes in an endless loop, ensures the processor program
counter is always valid when no other tasks are running, user can implement his own
su
idle task for example: a power conserva�on code, such as system suspension, a�er a
Ba
o a name,
o a unique ID,
o a priority,
o a task control block (TCB),
o a stack,
o and a task rou�ne,
8
linkedin.com/in/uttambasu
Task Context
Context switching: Is how the processor switch between the context of one task to the
another, so the system must:
su
o Save the state of the old process.
o Then load the saved state for the new process.
Ba
o The new process con�nues from where it le� off just before the context switch.
m
o When the task is not running, its context is frozen within the TCB, to be restored the
next �me the task runs.
tta
o The Dispatcher: Is the part of the scheduler that performs context switching.
U
o Context Switch Time: Is the �me it takes for the scheduler to switch from one task to
another.
o frequent context switching makes a performance overhead.
The shared data problem occurs when several func�ons (or ISRs or tasks) share a variable.
Shared data problem can arise in a system when another higher priority task finishes an
opera�on and modifies the data or a variable before the comple�on of previous task
opera�ons.
9
linkedin.com/in/uttambasu
Non reentrant func�ons
Reentrant func�ons
Is a func�on that can be used between more than one task without fear of data corrup�on.
Some Func�ons and some opera�on on a shared data are processor and compiler
m
dependent.
tta
Example:
U
10
linkedin.com/in/uttambasu
Disable and enable interrupts
o Disabling the interrupt for long �me affect the response to your system which known
by Interrupt Latency.
o Interrupt Latency: is the �me taken by a system to respond to an interrupt.
This is the only way for a task to share a variable with ISR.
If we don't share data with any ISR, then it's beter to disable and enable scheduling.
su
While the scheduler is locked, the interrupts is enabled, and if interrupt happen, the ISR is
executed immediately.
Ba
will return to the interrupted task not the highest priority one.
tta
Semaphores
o Is a kernel object that one or more threads of execu�on can acquire or release for the
purposes of synchroniza�on or mutual exclusion.
o A semaphore is like a key that enables a task to carry out some opera�on or to access
a resource.
o When a task acquires the semaphore, no other task can access the resource that is
protected by the semaphore.
o Other tasks acquire the semaphore will be suspended un�l the semaphore is released
by its current owner.
11
linkedin.com/in/uttambasu
Semaphores parameters and data structures
Semaphore Types
Binary Semaphore:
Binary semaphore:
U
12
linkedin.com/in/uttambasu
Coun�ng semaphore:
su
Ba
m
tta
Using semaphore to access shared data doesn't affect the interrupt latency.
U
If ISR or the current running task makes a higher priority task to run it run immediately.
When two tasks are wai�ng the resource held by the other.
Example:
13
linkedin.com/in/uttambasu
o Both the tasks will be blocked, and a DeadLock happen.
o Through a �meout, If the resource is not available for a certain �me, the task will
resume execu�ng.
o Good Design.
Priority inversion
su
Ba
m
tta
U
14
linkedin.com/in/uttambasu
Priority inversion problem (Extended priority inversion)
su
Ba
15
linkedin.com/in/uttambasu
Race Condi�on
when two or more tasks have access to a shared resource and they try to edit it at the same
�me,
To prevent race condi�ons from occurring, you would typically put a lock around the shared
data to ensure only one thread can access the data at a �me.
CPU Starva�on
CPU starva�on: occurs when higher priority tasks use all of the CPU execu�on �me and lower
priority tasks do not get to run.
In a preemp�ve mul�tasking environment, If higher priority tasks are not designed to block,
CPU starva�on can result.
su
Ba
Mutex
m
o Mutex is a special type of binary semaphore used for controlling access to the shared
resource.
U
16
linkedin.com/in/uttambasu
Mutex vs Binary Semaphore
Ownership:
o Mutex semaphore is "owned" by the task that takes it, so when a task locks (acquires)
a mutex only it can unlock (release) it.
o Binary semaphore has no owner, can be unlocked by any task,
Usage:
Determinism vs Responsiveness
Why RTOS?
U
Mul�tasking:
Services:
o Kernel services are provided for resource management, memory management, event
handling, messaging, interrupt handling and more.
Structure:
Portability:
17
linkedin.com/in/uttambasu
o Your applica�on will run on any pla�orm the RTOS runs on.
o You can build an applica�on for mul�ple targets from the same codebase.
Security:
o Some RTOS offers some security features, like encryp�on support for various
communica�on protocols memory protec�on unit (MPU) support.
Debugging:
o Some IDES (such as IAR Embedded Workbench) have plugins that show nice live data
about your running process such as task CPU u�liza�on and stack u�liza�on.
Support:
o Some RTOSs provides you with technical support and gives you access to the
exper�se of the engineers who developed each module, and they can advise and
support you on your project.
su
Ba
Resources:
tta
Determinism:
Design:
Debugging:
18
linkedin.com/in/uttambasu
Cost:
su
Ba
m
tta
U
19
linkedin.com/in/uttambasu
su
Ba
m
tta
U
20
linkedin.com/in/uttambasu