Embedded System and Design (Theory Notes) Autonomous Course: Department OF Electronics & Communication Engineering
Embedded System and Design (Theory Notes) Autonomous Course: Department OF Electronics & Communication Engineering
OF
ELECTRONICS & COMMUNICATION ENGINEERING
EMBEDDED SYSTEM AND DESIGN
(Theory Notes)
Autonomous Course
Prepared by
Prof. SANTHOSH KUMAR R
Module – 5 Contents
Real Time Operating Systems: Real Time and
Embedded, Operating Systems, Schedule Management
for Multiple Tasks by an RTOS, Interrupt Routines in
RTOS Environment, RTOS Task Scheduling models,
Interrupt latency and response times for the Tasks as
Performance Metrics.
UNIT V
REALTIME OPERATING SYSTEM
❖ The OS manages the system resources & make them available to the user application/task.
Computing system is collection of different I/O sub-system working & storage memory.
Primary Function of OS
The Kernel
❖ The kernel is core of the OS & is responsible for managing the system & the
communication among the hardware & the system services. Kernel acts as the
abstraction layer between system resources & user application. Kernel contains a set of
system libraries of services. For general purpose OS the kernel contains different
services for handling the management.
1. Process Management:
❖ Managing the process/tasks
❖ Setup the memory space for process Load
❖ Program/code into space(memory)
❖ Scheduling & managing the execution of the process
❖ Setting up & managing the PCB
❖ Inter process communication & system synchronization process termination & deletion
2
DSCE ECE DEPT.
Embedded Systems Design ESD Module - 5
❖ It is volatile memory
❖ The MMU of kernel is responsible for Keeping track of which part of memory area is correctly
❖ Kernel is responsible for routing the I/O request coming from different user
applications Direct access of I/O devices are not allowed , we can access the I/O
devices through the API imposed by kernel
3
DSCE ECE DEPT.
Embedded Systems Design ESD Module - 5
❖ Device manager of the kernel is responsible for handling I/O device related
operations The kernel talks to the I/O device through the device driver , this is
responsible for
❖ The secondary storage management deals with secondary storage memory device, if
any connected to the system
What is RTOS?
1) A real time operating system (RTOS) is an operating system that guarantees a certain
Capability within a specified time constraint.
2) An OS is a system program that provides an interface between Application programs
and the computer system (hardware)
3) The applications where dependability that a certain task will finish before a particular
deadline is just as obtaining the correct results.
4) Besides meeting deadlines RTOS must also be able to respond predictably to
unpredictable events and process multiple events concurrently.
4
DSCE ECE DEPT.
Embedded Systems Design ESD Module - 5
Learning the difference between real-time and standard operating systems is as easy as
imagining yourself in a computer game. Each of the actions you take in the game is like a
program running in that environment. A game that has a real-time operating system for its
environment can feel like an extension of your body because you can count on a specific "lag
time:" the time between your request for action and the computer's noticeable execution of your
request. A standard operating system, however, may feel disjointed because the lag time is
unreliable. To achieve time reliability, real-time programs and their operating system
environment must prioritize deadline actualization before anything else. In the gaming
example, this might result in dropped frames or lower visual quality when reaction time and
visual effects conflict.
Real-Time Kernel
The heart of a real-time OS (and the heart of every OS, for that matter) is the kernel. A
kernel is the central core of an operating system, and it takes care of all the OS jobs:
1. Booting
2. Task Scheduling
3. Standard Function Libraries
In an embedded system, frequently the kernel will boot the system, initialize the ports and
the global data items. Then, it will start the scheduler and instantiate any hardware timers that
need to be started. After all that, the Kernel basically gets dumped out of memory (except for
the library functions, if any), and the scheduler will start running the child tasks. Standard
function libraries: In an embedded system, there is rarely enough memory (if any) to maintain a
large function library. If functions are going to be included, they must be small, and important.
Kernel which is a major part of an operating system that provides the most basic services to
application software running on a processor. The "kernel" of a real-time operating system
("RTOS") provides an "abstraction layer" that hides from application software the hardware
details of the processor (or set of processors) upon which the application software will run.
Scheduling
5
DSCE ECE DEPT.
Embedded Systems Design ESD Module - 5
Most tasks are blocked or ready most of the time because generally only one task can run at a
time per CPU. The number of items in the ready queue can vary greatly, depending on the
number of tasks the system needs to perform and the type of scheduler that the system uses.
On simpler non-pre-emptive but still multitasking systems, a task has to give up its time on the
CPU to other tasks, which can cause the ready queue to have a greater number of overall tasks
in the ready to be executed state (resource starvation).Usually the data structure of the ready
list in the scheduler is designed to minimize the worst-case length of time spent in the
scheduler's critical section, during which pre-emption is inhibited, and, in some cases, all
interrupts are disabled. But the choice of data structure depends also on the maximum number
of tasks that can be on the ready list.
If there are never more than a few tasks on the ready list, then a doubly linked list of ready tasks
is likely optimal. If the ready list usually contains only a few tasks but occasionally contains
more, then the list should be sorted by priority. That way, finding the highest priority task to run
does not require iterating through the entire list. Inserting a task then requires
walking the ready list until reaching either the end of the list, or a task of lower priority than
that of the task being inserted.
Care must be taken not to inhibit pre-emption during this search. Longer critical sections should
be divided into small pieces. If an interrupt occurs that makes a high priority task ready during the
insertion of a low priority task, that high priority task can be inserted and run immediately before
the low priority task is inserted.
The critical response time, sometimes called the fly back time, is the time it takes to queue a new
ready task and restore the state of the highest priority task to running. In a well-designed RTOS,
readying a new task will take 3 to 20 instructions per ready-queue entry, and restoration of the
highest-priority ready task will take 5 to 30 instructions.
Types of Kernel:
Based on kernel design, kernel can be classified into Monolithic & Micro
❖ In Monolithic kernel architecture all the kernel service run in the kernel space – means all
kernel module run with same memory space under single kernelthread
❖ The drawback of Monolithic kernel is that any error or failure in any of the kernel module
i.e. leads to the crashing of entire kernel application. Ex : LINUX,SOLARIS,MS-DOS
6
DSCE ECE DEPT.
Embedded Systems Design ESD Module - 5
Micro kernel: design incorporates only the essential set of OS services into the kernel. The
rest of the OS services are implanted in programs known as “servers” which runs in user
space.
✓ Memory management
✓ Process management
✓ Timer system
✓ Interrupt handler
1. Robust
2. Configurability
7
DSCE ECE DEPT.
Embedded Systems Design ESD Module - 5
Depending on the type of kernel & kernel services purpose & type of in computing system
OS are classified into two types
The kernel of such OS is more generalized & it contains all kinds of services required
for executing generic applications
✓ Processing time requirements are measured in tenth of seconds. Hard RTOS has less jitter
than soft RTOS.
[Jitter: Variation in time between packet arriving with time congestion & time.]
✓ Real-time implies deterministic timing behavior – means the OS services consumes only
known & expected amount of time regardless the no of services.
✓ RTOS decides which application should run in which order & how much time needs to
be allocated for each applications.
The RTOS is referred to as Real Time kernel in complement to conventional OS kernel. The
kernel is highly specialized & it contains only the minimal set of services required for running
the user application/
8
DSCE ECE DEPT.
Embedded Systems Design ESD Module - 5
Tasks.
1. Task/Process Management
2. Task/Process Scheduling
3. Task/Process Synchronization
4. Error/Exception Handling
5. Memory Management
6. Interrupt Handling
7. Time Management
Task/Process Management: Deals with setting up the memory space for the tasks, loading
the tasks code into memory space, allocating system resources, setting up a Task Control
Block [TCB] for the task &task/process termination/deletion.
TCB: Task Control Block is used for holding the information corresponding to a task. The TCB
contains the following set of information
✓ Task Type: Indicates what is the type of this task, the task can be hard real time or soft real
time or background task
✓ Task Context Pointer: context pointer, pointer for saving context Task
Memory Pointer: Pointers to the code memory, data memory &
✓ stack memory for the task
✓ Task System Resource Pointers: Pointer to system resource used by the task
9
DSCE ECE DEPT.
Embedded Systems Design ESD Module - 5
The TCB parameters vary across different kernels based on the task management
Implementation
10
DSCE ECE DEPT.
Embedded Systems Design ESD Module - 5
Error/Exception Handling: Deals with registering& handling the errors occurred during the
execution of tasks. Ex: Insufficient memory, time outs, dead locks, dead line missing, bus
error, divide by zero, unknown instruction execution. Errors & exceptions can happen at two
levels of services * Kernel Level Service * At Task Level
Memory Management: RTOS makes use of ‘Block Based Memory’ allocation techniques
instead of the usual dynamic memory allocation technique used by GPOS. RTOS kernel uses
blocks of fixed size dynamic memory & block is allocated for a task on a need of basis. A
few RTOS kernel implements Virtual Memory concepts avoid the garbage collection
overhead.
Interrupt Handler: Deals with the handling of several of interrupts. Interrupts provide Real
Time behaviour embedded system design process
to systems. Interrupts inform the processor that an external device or an associated task
required immediate attention of the CPU. Interrupts can
be either Synchronous Asynchronous. Interrupts which occurs in synch with the currently
executing task is known as synchronous interrupts. Usually the system interrupts fall under the
synchronous category Ex
Divide by zero, memory segmentation error. A synchronous interrupts are those which occurs
at any point of execution of any task & are not in sync with currently executing tasks.
Task: Refers to something that needs to be done. The tasks refers can be one assigned by
manages or the one assigned by only one of the processor family needs. In addition we have an
order of priority & scheduling timeline for executing these tasks. In OS context, a task is
defined as the program in execution & related information maintained by the OS. Task is also
known as “JOB” in the OS context.
Process: A process is a program as part of the program, in execution process is also known as
instance of the program in execution, multiple instance of the same program can execute
simultaneously. Process requires various system resources like CPU for executing the
11
DSCE ECE DEPT.
Embedded Systems Design ESD Module - 5
process, memory for storing the code corresponding to the process, I/O devices for in function
exchange etc.
• Memory organization of a process and its associated threads Run: A task enters this state as it
starts executing on the process Ready state of those tasks that are ready to execute but
•
12
DSCE ECE DEPT.
Embedded Systems Design ESD Module - 5
• cannot be executed because the processor is assigned to another task Blocked a tasks enters
this state when it executes synchronization primitive to wait for an event
• Ex:-a wait primitive on a semaphore this case the task is inserted in a queue associated with
the semaphore.
• Created (idle) a periodic job enters this state when it computes its execution and has to wait
for the beginning of the next period Process management:
• Process management deals with the creating a process, setting up the memory space for the
process, loading the process code with the memory space, allocating the system resources.
Setting up a process control block (PCB) for the process and process termination/detection.
Task scheduling:
• Multitasking evolves the execution switching among the different tasks. There should be same
mechanism in place to show the CPU among the
• different takes and to decide which process/task is to be executed at a given point of time.
Determining which task/process is to be executed at a give point of time is known as
task/process scheduling. Task
• scheduling from the basis of multitasking Scheduling policies from the guidelines for
determining which task is to be executed when.
• The work of choosing the order of running process is known as scheduling. A scheduling
policy defines how processes are selected for
• promotion from the ready state to the running sate. The process scheduling decision may take
place when a process Switches its states Ready state from Running state Blocked/wait sate
from Running state Ready state from Blocked/wait state Completed (executed) state.
13
DSCE ECE DEPT.
Embedded Systems Design ESD Module - 5
Worst-case latency
• Same for every task
• Tworst = {(sti + eti )1 + (sti + eti )2 +...+ (sti + eti)N-1 + (sti +
eti)N} + tISR.
• tISR is the sum of all execution times for the ISRs
• For an i-th task, switching time from one task to another be is sti and
task execution time be is eti
• i = 1, 2, …, N 1 , N, when number of tasks = N
• Program counter assignments (switch) at different times, when the on
the scheduler calls the o tasks from the list one by one in the circular
queue from the list
Messages from the scheduler and task programs contexts at various instances in
washing machine tasks scheduling
• None of the tasks does block in-between anywhere during the ready to finish states.
• The service is in the order in which a task is initiated on interrupt
Worst-case latency
• Same for every task in the ready list
• Tworst = {(dti + sti + eti )1 + (dti + sti + eti )2
• +...+ (dti + sti + eti )n-1 + (dti + sti + eti )n} + tISR.
• tISR is the sum of all execution times for the ISRs
• For an i-th task, let the event detection time with when an event is
brought into a list be is dti , switching time from one task to another
be is sti and task execution time be is eti
• i = 1, 2, …, n 1,n
Scheduler in which the scheduler inserts into a list the ready tasks for a sequential
execution in a cooperative mode
Cooperative Scheduling in the order of Ready Tasks using an Ordered List as per
priority precedence
• Scheduler using a priority parameter, taskPriority does the ordering of list of the
tasks─ ordering according to the precedence of the interrupt sources and tasks.
• The scheduler first executes only the first task at the ordered list, and the total,
equals the to period taken by the first task on at the list. It is deleted from the list
after the first task is executed and the next task becomes the first.
• Cooperative Scheduling in the order of Ready Tasks using an Ordered List as
per priority precedence
• The insertions and deletions for forming the ordered list are made only at the
beginning of the cycle for each list
Worst-case latency
• Not Same for every task. Varies from (dti +
• sti + eti ) p(m)} + tISR to{(dti + sti + eti )p1 + (dti + sti + eti ) p2
+...+ (dti+ sti + eti ) p(m-1) + (dti + sti + eti ) p(m)} + tISR.
Program counter assignments at different times on the scheduler calls to the ISRs
and the corresponding
• Start of a time frame is the scheduling point for the next task in the
cycle.
• Tcycle is the cycle for repeating cycle of execution of tasks in order 1,
2 and 3 and equals start of task 1 time frame to end of task 3 frame.
• Tcycle is period after which each task time frame allotted to that
repeats
• Case : tc Cycle = N Sum of the maximum times for each task
• Then each task is executed once and finishes in one cycle itself.
• When a task finishes the execution before the maximum time it can
takes, there is a waiting period in-between period between two cycles.
• The worst-case latency for any task is then N Sum of the maximum
times for each task. A task may periodically need execution. A task
period for the its need ofrequired repeat execution of a task is an
integral multiple of tcycle
Worst-case latency
• Not Same for every task
• Highest priority task latency smallest
• Lowest priority task latency highest
• Different for different tasks in the ready list
• Tworst = {(dti + sti + eti )1 + (dti + sti + eti )2
• +...+ (dti + sti + eti )p-1 + (dti + sti + eti )p} + tISR.
• tISR is the sum of all execution times for the ISRs
• For an i-th task, let the event detection time with when an event is
brought into a list be is dti , switching time from one task to another
be is sti and task execution time be is eti
• i = 1, 2, …, p 1when number of higher priority tasks = p 1 for the
pth task.
ISRs Handling
• Hardware polls to determine whether an ISR with a higher priority ISR than
the present one needs the service at the end of an instruction during execution
of an ISR, if yes, then the higher priority ISR is executed.
RTOS method for Preemptive Scheduling of tasks
An infinite loop in each task
• Each task design is like as an independent program, in an infinite loop
between the task ready place and the finish place.
• The task does not return to the scheduler, as a function does.
• Within the loop, the actions and transitions are according to the events
or flags or tokens.
When priority of task_1 > task_2 > task_3
• (1) At RTOS start, scheduler sends a message (Task_Switch_Flag) to task 1
to go to un-blocked state and run, and thus highest priority task 1 runs at
start.
• (2) When task 1 blocks due to need of some input or wait for IPC or delay for
certain period, a message (Task_Switch_Flag) will be sent to RTOS, task 1
context saves and the RTOS now sends a message (Task_Switch_Flag) to task 2
to go to un- blocked state and run.
• (3) Task 2 now runs on retrieving the context of task 2. When it blocks due to
need of some input or wait for IPC or delay for certain period, a message
(Task_Switch_Flag) will be sent to RTOS, task 2 context saves andan RTOS
message (Task_Switch_Flag) makes the task 3 in un- blocked state. Task 3
will run now after retrieving the context of task 3.
• (4) If during running of task 3, either task 2 or task 1 becomes ready with the
required input or IPC or delay period is over, task 3 is preempted, a message
(Task_Switch_Flag) will be sent to RTOS, task 3 context saves, and task 1,
and if task 1 not ready, then task 2 runs after retrieving the context of task 2.
• (5) A message (Task_Switch_Flag) is sent to RTOS after task 2
blocks due to wait of IPC or need of sum input and task 2 context
saves and task 1 if ready then task 1 runs on retrieving the context of
task 1
• (6) task 1 if not ready then task 3 runs on retrieving the context of task 3
• (7) Task 1 when ready to run preempts tasks 2 and 3, and Task 2
when ready to run preempts task 3
• Specifying timeout for waiting for the token or event
• Specify timeout for waiting for the token or event.
• An advantage of using timeout intervals while designing task codes is
that worst- case latency estimation is possible.
• There is deterministic latency of each tasks
In a system the ISR should functions as following. 1.ISR have higher priorities over the OS
functions and the applications tasks.an ISR does not wait for a semaphore mailbox message
or queue message 2.An ISR does not also wait for mutex else it has to wait for other critical
sections code to finish before the critical codes in the ISR can run. only the accept functon
for these events can be used
There are three alternative systems for the used to respond to the hardware
source calls from the interrupts
• The following sections explain the OS is to respond to the hardware source
calls from the interrupts
1.direct call to an ISR by an interrupting.
2.RTOS first interrupting on an interrupt then 0S calling the corresponding
ISR.
3.RTOS first interrupting on an interrupt then RTOS initiating the ISR and
then an ISR
• On an interrupt of a task system k-th task the OS first gets the hardware
source call(step1).
• And initiates the corresponding ISR after saving present process status (step2)
• An i-th interrupt source causes the OS to get the notes of that after step1 it
finish the critical code till the pre-emption point and calls the i-th ISR –I
executs (step3).
• The preemptions point is the last instructions of the presently running OS
function after which the ISR being of highest priority is called the ISR(step4).
.Can post the event or mailbox messages to the 0S for initiating the j-th task or
k-th task after the return(step5).
• From the ISR and after returning the j-th or k-th context.
• The system priorities are ISR and then tasks or (ISTS).IST is just a task on
signal or message from an ISR .
• The use of FLISR reduce the interrupt latency for an interrupt service and
jitter (worst case and best case latencies difference )for an interrupt service.
• An IST functions as a deferred procedure call (DPC) of the ISR
• An i-th IST is thread to service an i-th interrupt source call. The ISR during
executing then can send one or more outputs for the events and messages in to
the mailbox for the IST
• For the per-emption from the same or other hardware sources
• The ISR in the FIFO that have received the message from the ISR
executes(step6).