0% found this document useful (0 votes)
7 views21 pages

RTOS FULL Notes

The document provides an overview of operating systems (OS) and real-time operating systems (RTOS), highlighting their functions, task management, and scheduling mechanisms. It distinguishes between hard and soft RTOS, explains multitasking, polling, and interrupts, and discusses critical sections, shared resources, and synchronization methods like semaphores and mutexes. Additionally, it addresses issues such as deadlocks, priority inversion, and race conditions, emphasizing the importance of design in real-time applications.

Uploaded by

bennypon2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views21 pages

RTOS FULL Notes

The document provides an overview of operating systems (OS) and real-time operating systems (RTOS), highlighting their functions, task management, and scheduling mechanisms. It distinguishes between hard and soft RTOS, explains multitasking, polling, and interrupts, and discusses critical sections, shared resources, and synchronization methods like semaphores and mutexes. Additionally, it addresses issues such as deadlocks, priority inversion, and race conditions, emphasizing the importance of design in real-time applications.

Uploaded by

bennypon2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

su

Ba
m
tta
U

linkedin.com/in/uttambasu
What is an OS?
Has direct access to hardware.

o Manage hardware according to predefined rules and policies.


o Hide hardware complexity from the applica�on perspec�ve.
o Enable mul�tasking.

Applica�ons (WEB, Games, Text editors, etc)

su
OS
Ba

Kernel
m
tta

Hardware (RAM, CPU, GPU, Peripherals, etc)


U

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 and Mul�tasking:


m

Task:
tta

o Task also called A thread.


U

o It's the basic block of an applica�on writen under RTOS.


o It's a simple program with an infinite loop.
o Task has its own stack, CPU registers, and priority.

Example:

void vTaskFunc�on( void *pvParameters)

for(;;)

{--Task applica�on code here. -- }

2
linkedin.com/in/uttambasu
Mul�tasking,

o Is the process of switching the CPU between several tasks.


o It maximizes the u�liza�on of the CPU.
o Provide modular construc�on for our applica�on, makes the
o applica�on design is easier.

Polling and interrup�ons

Polling:

o We are checking an event through an infinite loop.


o checks all devices in a round robin fashion.
o The main drawback of this method the applica�on needs to wait and check whether
the new informa�on has arrived, so, its a waste of �me of the processor.
su
o Also, it may miss some events.
Ba

Interrupt

o An external or internal event that interrupts the processor to inform it that a device
m

needs its service.


tta

o When an event happens, the processor jumps to the Interrupt Service rou�ne (ISR).
U

o ISR is a func�on executes once the related interrupt happens.

What is a Resource?

A Resource is an en�ty used by the task; it can be:

o I/O device, Printer, Keyboard, Display, Variable, Array, Structure, File

What is a Shared 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.

Cri�cal Sec�on of Code

o Also called cri�cal region.


o Is a sec�on of code that shouldn't be interrupted.
o Most RTOS Systems enable us to disable the interrupt before
o this sec�on then enables it again a�er that.
o There are also other methods to protect this cri�cal sec�on.

RTOS Kernel
su
Kernel is the core component of any OS.
Ba

kernel components:
m

o Scheduler is a set of algorithms that determines which task executes when.


tta

o Objects are special kernel constructs that help developers create applica�ons for real-
U

�me embedded systems.


o Services are opera�ons that the kernel performs on an object.

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.

RTOS Task status

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.

o ISR makes the highest priority task run.


o The kernel next run the highest priority task in the ready queue.
o Here the response �me to the highest priority task is it's best.
o Corrup�on of data may happen for non-protected shared resources.

6
linkedin.com/in/uttambasu
RTOS Scheduler

Scheduler is the core Component of any RTOS kernel.

o It’s a set of algorithms that determines which task executes when.


su
o It's keeping track on the status of each task and decides which to run.
o In Most RTOSs the developers are the ones who sets the priority of each task,
Ba

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

long �me, this is called CPU starva�on.


o It's keeping track on the status of each task and decides which to run.
o Scheduler has no control on tasks on the blocked status.
o If tasks are blocked the scheduler waits an event to unblock these tasks, like an
external interrupt from pushing a buton.
o If no events happened, surely, it's a bad design from your side.

What if two tasks have the same priority are ready?

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 The Tasks the system uses to run its internal opera�ons.


o The RTOS system tasks have reserved priori�es that we shouldn't use in our
applica�on tasks as it may affect the overall system performance or behaviour.

Examples of system tasks include:

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

period of idle �me.


o logging task: logs system messages.
m

o excep�on-handling task: handles excep�ons.


tta

o debug agent task: allows debugging with a host debugger.


U

Task object data

Each task has an associated:

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

o Every task has its own context (its own Data).


o Every Task Created has its own data structure called Task Control Block (TCB).
o Task saves its data like tasks status, ID, priority, stack pointer, Pointer to func�on (task
itself) ......, in its TCB.
o Task context also saved in its own stack and CPU registers.

Context switching between tasks

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.

Shared data problem

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

o Is a func�on that can' t be used between more than one task.


o Can't be interrupted or a data loss will happen.

Reentrant func�ons

Is a func�on that can be used between more than one task without fear of data corrup�on.

Can be interrupted at any �me and resumed without loss of data.

Reentrant func�ons either:

o use local variables, or use protected global variables.


o Can't Call other non reentrant func�on.
su
Ba

Gray area of reentrancy

Some Func�ons and some opera�on on a shared data are processor and compiler
m

dependent.
tta

Example:
U

o prin�() Func�on Is reentrant or non reentrant?

The answer is it's depended on the processor and on the compiler.

How to protect shared data

Using Mutual Exclusion access. Examples on Mutual Exclusion methods are:

o Disable and enable interrupts.


o Disable the Scheduler.
o Using Semaphores.

10
linkedin.com/in/uttambasu
Disable and enable interrupts

Most of Systems have this technique.

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.

So, disable the interrupt should be for as litle �me as possible.

This is the only way for a task to share a variable with ISR.

Disable and enable the scheduler

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

As the scheduling is disabled, when the ISR finish, the kernel


m

will return to the interrupted task not the highest priority one.
tta

Disabling the scheduler also is not the best solu�on.


U

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

When a semaphore is created, the kernel assigns to it:

o An associated semaphore control block (SCB).


o A unique ID.
o A value (binary or a count) depending on its type.
o A task wai�ng list.

Semaphore Types

Binary Semaphore:

o Its value = 0, if it's not available.


o Its value = 1, if it's available.
su
Coun�ng Semaphore:
Ba

o Its value = 0, if it's not available.


o Its value > 0, if it's available.
m
tta

Binary semaphore:
U

12
linkedin.com/in/uttambasu
Coun�ng semaphore:

Mutual exclusion with binary 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.

Dead lock problem:

Also called Deadly Embrace.

When two tasks are wai�ng the resource held by the other.

Example:

o Task1 has an exclusive access to Resourcel,


o And Task2 has an exclusive access to Resource2.
o If Task1 needs an exclusive access to Resource2,
o And Task2 needs an exclusive access to Resourcel.

13
linkedin.com/in/uttambasu
o Both the tasks will be blocked, and a DeadLock happen.

Avoid dead lock

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

Extended Priority inversion solu�on (Priority Inheritance)


m
tta
U

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 short for Mutual Exclusion.


tta

o Mutex is a special type of binary semaphore used for controlling access to the shared
resource.
U

o Mutexes include a priority inheritance mechanism to avoid extended priority


inversion problem, this ensure the higher priority task is kept in the blocked state for
the shortest �me possible.
o Priority inheritance does not cure priority inversion! It just minimizes its effect in
some situa�ons.
o Hard real �me applica�ons should be designed such that priority inversion does not
happen in the first place.

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:

o Mutex is a locking mechanism used to synchronize access to a resource.


o Semaphore is signalling mechanism ("I am done, you can carry on" kind of signal.

Determinism vs Responsiveness

o Determinism: determine the �me that every task will be executed.


su
o Responsiveness: How your applica�on be responsive to (External or Internal) events.
Ba

o RTOS Increase responsiveness and decrease determinism.


m
tta

Why RTOS?
U

Mul�tasking:

o Allows you to break a complex problem into simpler Pieces.


o Focus on the development of each task rather than building a scheduler.
o Increase the Efficiency of using the CPU.

Services:

o Kernel services are provided for resource management, memory management, event
handling, messaging, interrupt handling and more.

Structure:

o Structure design of your applica�on.

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

Why not RTOS?


m

Resources:
tta

o RTOS will require extra resources.


U

o Processing overhead due to context switch complex algorithms.


o memory usage overhead due to OS source size.

Determinism:

o RTOS decreases determinism.

Design:

o Needs very careful design, and more developing skills.


o It is never easy to port an RTOS.

Debugging:

o complex debugging (due to race condi�ons on resources shared among tasks)

18
linkedin.com/in/uttambasu
Cost:

o Lots of RTOS needs expensive license, so product cost Will increase.

su
Ba
m
tta
U

19
linkedin.com/in/uttambasu
su
Ba
m
tta
U

20
linkedin.com/in/uttambasu

You might also like