The document discusses various topics related to embedded systems and real-time operating systems (RTOS), including:
1) It explains the differences between bare-metal applications, which run directly on hardware without an OS, and OS applications, which run on top of an operating system.
2) It describes some key characteristics and services provided by RTOS kernels, such as deterministic behavior and task scheduling.
3) It discusses different types of real-time systems, including soft and hard real-time, and explains the importance of meeting timing deadlines in real-time systems.
4. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
OS Application
Write your application on the top
of the Operating System.
On desktop computers, the
selection of an operating system
(OS) is largely a matter of taste -
Windows vs Apple vs Linux. There
is relatively little choice.
For an embedded system, the
matter is much more complex. The
large number of options available
reflect the wide diversity of
embedded applications.
4
OS
Application
OS
Drivers
• Linux Command
• Graphical GUI “ex.QT application”
• C/C++ app.
• RTOS Tasks
• Autosar SW Component
Embedded Linux
• Linux Kernel “Linux.org”
• Open embedded YOCTO
• Android
• Windows IOT
Embedded windows
RTOS
Autosar• Free RTOS
• μC/OS-III
• RTOS OS + BSW +RTE
Embedded Linux Drivers
RTOS Drivers
MCAl Layer
9. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
What is an Operating System
9
A software layer between the application software
and the hardware
Typical embedded system (ES) solves a problem by
decomposing it into smaller pieces called tasks
that work together in an organized way
System is called multitasking system and
design aspects include:
Exchanging/sharing data between tasks
Synchronizing tasks
Scheduling tasks
The piece of software that provides the required
coordination is called an operating system (OS)
When the control must ensure that task execution
satisfies a set of specified time constraints, the
OS is called a real-time operating system (RTOS)
10. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
The Operating System (OS)
10
Also it provides a services:
–Process or task management
•Creation and deletion of user and system processes
–Memory management
•Includes tracking and control of which tasks are loaded into
memory, monitoring memory, administer dynamic mem
–I/O System management
•Interaction with devices done through a special piece of software
called a device driver
•The internal side of that software is called a common calling
interface (an application programmer's interface, API)
–File system management
–System protection
–Networking
11. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Memory Management
In multiprogramming, OS decides which
process will get memory when and how much.
•Allocates the memory when the process
requests it to do so.
•De-allocates the memory when the process
no longer needs it or has been terminated.
Sometimes if the CPU hardware “MMU”
provides mapping between virtual and physical
addresses, the OS “memory management” will
manage the Virtual memory for each task
11
12. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Processor Management
OS decides which process gets the
processor when and how much time.
This function is called process
scheduling.
Operating System does the following
activities for processor management.
•Keeps tracks of processor and status of
process. Program responsible for this task
is known as traffic controller.
•Allocates the processor (CPU) to a process.
•De-allocates processor when processor is
no longer required.
12
13. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Device Management (I/O System management )
OS manages device communication via
their respective drivers. Operating
System does the following activities for
device management.
•Keeps tracks of all devices. Program
responsible for this task is known as the I/O
controller.
•Decides which process gets the device when
and for how much time.
•Allocates the device in the efficient way.
•De-allocates devices.
13
17. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
There are types of Real Time
System
Soft real time
• Tasks are performed by the system as fast as
possible, but tasks don’t have to finish by
specific times
• Multitask based on Priority scheduling
• Ex. Multimedia streaming
25 frames per second on an average
Hard real time
• Tasks have to be performed correctly
and on time
• Multitask based on Deadline scheduling
• Ex. Aircraft controller, Nuclear reactor
controller
17
18. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
The Real-Time Operating
System (RTOS)
An RTOS provides an Abstraction Layer between
Application Software and Embedded Hardware
Key characteristic of an RTOS is that it has
deterministic behavior
Basic Services provided by a Real-Time Operating System
Kernel
“Real-time” means keeping deadlines, not speed
Advantages of RTOS in SoC design
• Shorter development time
• Less porting efforts
• Better reusability
18
Task0Task1
21. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Foreground/Background Systems
An application consists of an infinite loop that calls modules (that is,
functions) to perform the desired operations (background).
Interrupt Service Routines (ISRs) handle asynchronous
events (foreground).
So Foreground is also called interrupt level while background is called task level
21
Background
Foreground
ISR ISR
ISR
(more
important)
Interrupts may be
prioritised, so that an ISR
may itself be interrupted so
that a more important event
may be serviced.
24. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Reentrancy
A reentrant function is a function
that can be used by more than
one task without cause of data
corruption.
A reentrant function can be
interrupted at any time and
resumed at a later time without
loss of data.
An error caused by a non-
reentrant function may not show
up in your application during the
testing phase
24
non-reentrant
int temp; // global
function swap(int *x,
int *y) {
temp = *x;
*x = *y;
*y = temp;
}
reentrant version
function swap (int *x,
int *y) {
int temp; // local
temp = *x;
*x = *y;
*y = temp;
}
26. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Process or Task
A task is usually implement in C by writing a function
When a C function is executing, it is called a process or task
So Task is not code
Two tasks can be executing the same code simultaneously
When a process is created, it is allocated a number f
resources by the OS, which may include:
–Process stack
–Memory address space
–Registers (through the CPU)
–A program counter (PC)
–I/O ports, network connections, file descriptors, etc.
These resources are generally not shared with other
processes
26
int x;
int Func() {
int y;
y = y + 1;
x = x + 1;
...
}
Task 1 Task 2
28. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Context Switch (or Task Switch)
When a multitasking kernel decides to
run a different task, it simply saves the
current task's context (CPU registers)
in the current task's context storage
area – it’s stack.
Once this operation is performed, the
new task's context is restored from
its storage area and then resumes
execution of the new task's code.
This process is called a context switch
or a task switch. Context switching adds
overhead to the application.
28
29. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Tasks conclusion
Each task thinks it has the CPU to itself
Each task has its own stack space and its own set of CPU
registers
Each task has a priority
A task may well take the form of
a sequence of initialisation code; followed by
a loop which iterates forever (or until a flag is set to exit it)
Body of loop will contain application code
A task calls functions which are preferably reentrant:
Can be safely called by more than one function at a time
Interrupting them does not corrupt their data
Typically implemented using local variables
Reusable and reentrant Code
29
Void task1()
{
// a sequence of
//initialisation code
While(1)
{
//application code
}
}
32. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Task States
READY-A task is READY when it
can be executed but its priority is
less than that of the task currently
being run.
In this state, the task actively
competes with all other ready tasks
for the processor’s execution time.
The kernel’s scheduler uses the
priority of each task to determine
which task to move to the running
state.
32
Waiting
Dormant Ready Running ISR
Task Create
Task Delete
Highest Priority Task
Task is Preempted
Task Pending EventsTask Gets Event
Task Delete
Interrupt
Int. Exit
Task Delete
μC/OS-II
33. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Task States
RUNNING-A task is RUNNING when it has
control of the CPU and it's currently being
executed.
On a single-processor system, only one task can
run at a time.
When a task is preempted by a higher priority
task, it moves to the ready state.
It also can move to the blocked state if:
Making a call that requests an unavailable resource
Making a call that requests to wait for an event to
occur
Making a call to delay the task for some duration
33
Waiting
Dormant Ready Running ISR
Task Create
Task Delete
Highest Priority Task
Task is Preempted
Task Pending EventsTask Gets Event
Task Delete
Interrupt
Int. Exit
Task Delete
μC/OS-II
34. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Task States
WAITING-A task is WAITING
when it requires the occurrence of
an event (waiting for an I/O
operation to complete, a shared
resource to be available, a timing
pulse to occur, time to expire,
etc.).
34
Waiting
Dormant Ready Running ISR
Task Create
Task Delete
Highest Priority Task
Task is Preempted
Task Pending EventsTask Gets Event
Task Delete
Interrupt
Int. Exit
Task Delete
μC/OS-II
35. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Task States
BLOCKED
CPU starvation occurs when higher
priority tasks use all of the CPU
execution time and lower priority
tasks do not get to run
ISR(Interrupt Service Routine)-A
task is in ISR state when an
interrupt has occurred and the CPU
is in the process of servicing the
interrupt.
35
Waiting
Dormant Ready Running ISR
Task Create
Task Delete
Highest Priority Task
Task is Preempted
Task Pending EventsTask Gets Event
Task Delete
Interrupt
Int. Exit
Task Delete
μC/OS-II
38. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Task Scheduling
Schedulers are parts of the kernel responsible for determining
which task runs next
In a Multitasking System, the main objective is to have the CPU
utilized at all times.
At each opportunity, OS asks:
IF N Tasks are ready ?
Which one Should I run ?
The scheduling can be handled automatically..
Many kernels also provide a set of API calls that allows
developers to control the state changes.
38
40. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Non-preemptive
It is also called cooperative multitasking;
tasks cooperate with each other to share the CPU.
tasks decide for themselves when to give up the CPU; when
completed.
The new higher priority task will gain control of the CPU only when
the current task gives up the CPU.
asynchronous events are handled using interrupts
tasks still have an assigned priority
advantages:
- interrupt latency time is low
- less need to “guard” shared resources
-At the task level, non-preemptive kernels can also use non-reentrant
functions.
disadvantages:
- responsiveness is non-optimal & non-deterministic
- one task may lock-up system
40
Time
ISR
Low-priority Task
High-priority Task
ISR makes the
high-priority task ready
low-priority task
Relinquishes the CPU
(low priority task is finished)
41. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Preemptive
the highest priority task (ready) is given
control of the CPU
current running task is suspended
after an Interrupt Service Routine (ISR), the
new highest priority task is run
Most commercial real-time kernels are
preemptive
advantages:
- responsiveness is optimal & deterministic
(Execution of the highest-priority task is
deterministic)
-Task-level response time is minimized
disadvantages:
- shared resources must be properly guarded
-Should not use non-reentrant functions unless
exclusive access to these functions is ensured
41
Time
ISR
Low-priority Task
High-priority Task
ISR makes the
high-priority task ready
high-priority task
Relinquishes the CPU
42. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Scheduler Concepts
CPU utilization : keep the CPU as busy as possible.
How much of CPU (usually in %) is in use ?
Throughput : number of processes (# pf processes) that complete their execution per
time unit.
Turnaround : total time between submission of a process and its completion.
(waiting + executing + IO Time)
Waiting time : a mount of time a process has been waiting in the ready queue.(task
enters by System initially), then it is placed in “ready state” when the conditions are
met.
Response time : amount of time it takes from when a request was submitted until the
first response is produced.
42
46. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Shortest Job First Algorithm
Also known as Shortest Job First (SJF).
With this strategy the scheduler arranges processes with the
least estimated processing time remaining to be next in the
queue.
If a shorter process arrives during another process
'execution, the currently running process may be interrupted.
This algorithm is designed for maximum throughput in most
scenarios.
Starvation is possible, especially in a busy system with many
small processes being run
This algorithm can be
Preemptive (Running task can be interrupted by another task).
Non Preemptive
46
Kindly Note Starvation, in general, means a situation
where some process has been denied service for a long
time or has been repeatedly denied service.
47. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_systemRound-robin scheduling Algorithm
(time-slicing)
Designed especially for time shared systems.
Similar to first –come first-served, with preemption
added to switch between process.
tasks with the same priority are only allowed to run for a
predetermined time period, “a quantum” This is also called
time slicing.
not necessarily supported by all preemptive kernels
microC/OS-II does not support round-robin
47
Assumption: Time Slice = 20 time unites
55. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Priority Inheritance Solution
55
Task 1 (high)
Task 2 (medium)
Task 3 (low)
1 2 3 4 5 6
In a kernel that supports priority
inheritance,
As soon as task 1 wants the resource (point 3)
locked by task 3, task 3 is temporarily
promoted to task 1’s priority so that it can
continue running until it releases the resource
(point 4)
Then, task 1 becomes ready because it can
acquire the resource released by task 3.
Task 1 releases the resource at point 5, but
keeps running also task 2 is ready, because it
has the highest priority
Finally at point 6, task 1 finishes and task 2 can
run. Task 3, with the lowest priority, waits.
NB Task 2 could have become ready at point 2
or any after thereafter, with no difference in
outcome..
56. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Scheduling - the “tick” or quantum
A hardware Timer generates interrupts at a regular interval
10 -- 1000 Hz; usually derived from power line frequency
Used to provide delays and timeouts
The ISR for a clock tick calls a kernel service to signal a tick.
This may well cause a context switch
A higher priority task may have become ready to run in the interim
In “round robin” scheduling, tasks of equal priority get a fixed number of
ticks’ CPU time than go to the back of the queue
The clock interrupt has higher priority than all tasks (because it
is an interrupt) but lower priority than other interrupts.
56= special interrupt
Periodical
= RTOS heartbeat
58. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Shared Data between Tasks
58
how does the consumer know that (new) data is available?
Data is shared between several tasks and, in
order to keep the data in a consistent state,
we need to protect it from concurrent
access. In other words: access to the data
must be serialized.
This use case is mainly about task synchronization:
the interplay between producer and consumer must work
smoothly. How does a consumer know that new data is
available?
how to serialize access to shared data?
59. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Two shared Resource Control
59
the mutex = MUTual EXclusion.
the semaphore
Resource
A resource is any entity used by a task. A resource can thus be
an I/O device such as a printer, a keyboard, a display, etc. or a
variable, a structure, an array, etc.
Shared Resource
A shared resource is a resource that can be used by more than one task. Each task should
gain exclusive access to the shared resource to prevent data corruption. This is called
Mutual Exclusion.
60. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
the mutex
A critical section is a piece of code (part of a
function) from which the access to the shared
resource must be atomic. At a certain moment in
time, one and only one critical section is allowed to
access the shared data
The mutex implements two operations:
acquire: enter the critical section, or sleep if another
task has already acquired the mutex, and
release: leave the critical section. Other tasks now
have the possibility to acquire the mutex.
60
62. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Mutual Exclusion
Disable interrupts Mechanism
Demand: exclusive use of a resource
Disable interrupts
Used on critical sections of code
disabling/enabling interrupts
used to access a few variables or small data structures
used when the process time is minimal
affects interrupt latency
you should not disable interrupts for any longer than the kernel does
in C/OS-II:
OS_ENTER_CRITICAL( );
… // you can access shared data here
OS_EXIT_CRITICAL( ):
62
Using Test-And-Set to access a resource
65. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
performing test-and-set operations (indivisibly)
perform test-and-set operations (indivisibly)
two tasks may agree that to use a common
resource, they must first check a global
variable
while it is being checked, and before it is set,
by one task, it must be made inaccessible by
the other
this can be achieved by:
disabling interrupts (above) or
some processors now support a single TAS
instruction
Make the following
test test-and-set variable
Set test-and-set variable to 1
Access resource
Set test-and-set variable to 0
65
Kindly Note: DPRAM test-and-set instructions can
work in many ways
When CPU 1 issues a test-and-set instruction, the
DPRAM first makes an "internal note" of this by
storing the address of the memory location in a special
place. If at this point, CPU 2 happens to issue a test-
and-set instruction for the same memory location, the
DPRAM first checks its "internal note", recognizes the
situation, and issues a BUSY interrupt, which tells CPU
2 that it must wait and retry.
67. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Mutual Exclusion - semaphores
a protocol mechanism to control
access to shared resources, signal
the occurrence of an event or allow
two tasks to synchronize
one analogy is that in order for a
task to use a resource it must first
draw a key for it
if the key(s) is in use, the task
suspends
wait on (pend) & signal (post)
semaphore
two types of semaphores:
binary and counting (general)
67
task 2
task 1 1. pend
4. post
Printer
2. pend
6. post
5. “my name is task 2”
3. “my name is task 1”
80. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
References
Embedded Microcomputer Systems Real Time Interfacing Third Edition
Jonathan W. Valvano University of Texas at Austin.
MicroC/OS-II the real-time kernel second edition jean j.labrosse.
RTOS Concepts http://www.embeddedcraft.org.
Task Synchronization Prof. Chung-Ta King.
Study of an operating system: FreeRTOS Nicolas Melot
Real-Time Operating Systems Prof. Stephen A. Edwards
Real Time Operating Systems for Networked Embedded Systems
Subhashis Banerjee Indian Institute of Technology Delhi
USING THE FREERTOS REAL TIME KERNEL A Practical Guide
Richard Barry
http://www.rtos.be/2013/05/mutexes-and-semaphores-two-concepts-
for-two-different-use-cases/
80
81. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
References
https://www.embedded.com/design/operating-
systems/4436454/Selecting-an-operating-system-for-an-embedded-
application-
http://free-electrons.com/training/
CENG-336 Introduction to Embedded Systems Development
https://nawid2012.wordpress.com/2012/11/13/real-time-systems/
Embedded Real-Time Systems Memory management
Unit 4 Real Time Operating System
Real Time Operating Systems
Real Time Operating Systems Johan Dams
81