ESunit6
ESunit6
faster performance,
Hard RT
vs.
Firm vs.
Soft RT
Typical Real Time Activities:
• Hard RT Activities:
• Sensory data acquisition
• Detection of critical conditions
• Actuator servoing
• Low-level control of critical system components
• Typical application areas:
• automotive : power-train control, air-bag control, steer
by wire, brake by wire
• aircraft : engine control, aerodynamic control
• Typical Firm RT Activities:
• decision support
• value prediction
• Typical application areas:
• Weather forecast
• Decisions on stock exchange orders
• Typical Soft RT Activities:
• command interpreter of user interface
• keyboard handling
• displaying messages on screen
• transmitting streaming data
• Typical application areas:
• communication systems (voice over IP!)
• user interaction
• comfort electronics (body electronics in cars)
foreground/background or super-
loops.
• An application consists of an infinite loop that calls
functions to perform the desired operations
(background).
• Interrupt Service Routines (ISRs) handle
asynchronous events (foreground).
• Foreground is also called Interrupt level while
background is called Task level.
• Critical operations must be performed by the ISRs
to ensure that they are dealt with in a timely
fashion.
• The information for a
background module made
available by an ISR is not
processed until the background
routine gets its turn to execute.
• The worst case task level
response time depends on how
long the background loop takes
to execute.
• Because the execution time of
typical code is not constant, the
time for successive passes
through a portion of the loop is
non-deterministic.
• Furthermore, if a code change is
made, the timing of the loop is
affected.
• Most high volume microcontroller-based
applications (e.g., microwave ovens, telephones,
toys, and so on) are designed as
foreground/background systems.
• Also, in microcontroller-based applications, it may
be better (from a power consumption point of
view) to halt the processor and perform all of the
processing in ISRs.
Design Problem
Draw flow chart to design software to achieve following functionality:
1. Generate sine wave from DAC_OUT with 100 samples per period T of sine
wave with frequency of sine wave expected is 10 KHz
2. Read temperature data from AD1.2 with sampling rate of 1000 samples per
second with temperature range of 0 to 1023oC
3. Display measured temperature after averaging reading of 100 samples
4. Read set value of temperature by from AD1.3 derived from potentiometer
5. Display the set value on display
6. Amplitude of the sine wave generated is controlled by:
DAC_OUT= SET_VALUE - MEASURED_VALUE * gain + offset
7. Switch off DAC_OUT and turn on hooter operated by relay if panic switch is
pressed
8. Restart the system on reset or on power on reset
9. Reset watch dog timer after every 1000ms
10. Any other?
Solution
[Link] two interrupts
a. Timer interrupt to send DAC data at rate 1µsec: Low
priority
b. External interrupt 0 for panic switch: Highest priority
[Link] two resets
a. Power on reset
b. Watchdog reset
ISR1_
LP
INC 1µsTickCounter,
Send out DAC_data
IRE
T
ISR2_
HP
Diagram RET
T2 4 3 0
T1 0 4 13
0 5 10 15 20 25
Series 1 Series 2 Series 3 Series 4
Mutual Exclusion
• The easiest way for tasks to communicate with each
other is through shared data structures.
• Tasks can thus reference global variables, pointers,
buffers, linked lists, ring buffers, etc.
• While sharing data (to simplify the exchange of
information), you must ensure that each task has
exclusive access to the data to avoid contention and
data corruption.
• The most common methods to obtain exclusive access
to shared resources are:
a) Disabling interrupts
b) Test-And-Set
c) Using semaphores
Disabling interrupts
• The easiest and fastest way to gain exclusive access
to a shared resource is by disabling and enabling
interrupts
• You must be careful, however, to not disable
interrupts for too long because this affects the
response of your system to interrupts.
Mutual Exclusion using Test-And-Set
(For system without kernel)
• If you are not using a kernel, two functions could
‘agree’ that to access a resource, they must check a
global variable, and if the variable is 0 the function has
access to the resource.
• To prevent the other function from accessing the
resource, however, the first function that gets the
resource simply sets the variable to 1. This is commonly
called a Test-And-Set (or TAS) operation.
• The TAS operation must either be performed indivisibly
(by the processor instructions) or you must disable
interrupts when doing the TAS on the variable
Mutual Exclusion, Semaphores
• Semaphores are used to:
a) control access to a shared resource (mutual exclusion);
b) signal the occurrence of an event;
c) allow two tasks to synchronize their activities.
• A semaphore is a key that your code acquires in
order to continue execution.
• If the semaphore is already in use, the requesting
task is suspended until the semaphore is released
by its current owner.
Binary and counting semaphore
• There are two types of semaphores: binary
semaphores and counting semaphores.
• As its name implies, a binary semaphore can only
take two values: 0 or 1.
• A counting semaphore allows values between 0
and 255, 65535 or 4294967295, depending on
whether the semaphore mechanism is
implemented using 8, 16 or 32 bits, respectively.
• Along with the semaphore's value, the kernel also
needs to keep track of tasks waiting for the
semaphore's availability.
Counting Semaphore cont.
• A task desiring the semaphore will perform a WAIT
operation.
• If the semaphore is available (the semaphore value is
greater than 0), the semaphore value is decremented
and the task continues execution.
• If the semaphore's value is 0, the task waiting for
semaphore is placed in a waiting list.
• Most kernels allow you to specify a timeout:
• If the semaphore is not available within a certain amount of
time, the requesting task is made ready to run (or under take
otherwise works) and an error code (indicating that a timeout
has occurred) is returned to the semaphore caller function in
that task.
• A task releases a semaphore by performing a SIGNAL
operation.
• If no task is waiting for the semaphore, the semaphore value
is simply incremented from zero to one.
• However, If any task is waiting for the semaphore, then, the
tasks is made ready to run and the semaphore value is not
incremented from zero to one; and the key is given to one of
the tasks waiting for it.
• Depending on the kernel, the task which will receive
the semaphore is either:
a) the highest priority task waiting for the semaphore, or
b) the first task that requested the semaphore (First In First Out,
or FIFO).
Example
• Semaphores are
especially useful
when tasks are
sharing I/O devices.
• A counting
semaphore is used
when a resource
can be used by
more than one task
at the same time.
Priority Inversion Problem
• When a lower - priority task blocks a higher -
priority task, a priority inversion is said to occur.
• Let three tasks, τ 1 , τ 2 , and τ 3 , have decreasing
priorities (i.e., τ 1 > τ 2 > τ 3 , where “ > ” is the
precedence symbol), and τ 1 and τ 3 share some
data or resource that requires exclusive access,
while τ 2 does not interact with either of the other
two tasks.
• Access to the critical section is carried out through
the wait and signal operations on semaphore s
T1 blocked due to
non availability of
Priority Inversion Problem
semaphore
T1
preemp
T1 ts
preempts
T3 T2
preempts
T3 T3
T3
locked
releases
semaph
semaph
ore
ore
T3
execute
s
• Now, consider the following execution scenario,
illustrated in Figure
• Task τ 3 starts at time t 0 , and locks semaphore s at
time t 1 . At time t 2 , τ 1 arrives and preempts τ 3
inside its critical section.
• After a while, τ 1 requests to use the shared resource
by attempting to lock s , but τ 1 gets blocked, as τ 3 is
currently using it. Hence, at time t 3 , τ 3 continues to
execute inside its critical section.
• Next, when τ 2 arrives at time t 4 , it preempts τ 3 , as it
has a higher priority and does not interact with either
τ1 or τ3 .
• The execution time of τ2 increases the period of
blocking of τ1 , as it is no longer dependent solely on
the length of the critical section executed by τ3 .
priority inheritance protocol
• In the priority inheritance protocol, the priorities of
tasks are dynamically adjusted so that the priority
of any task in a critical region gets the priority of
the highest - priority task using that same critical
region.
• Rule: when a task, τi , blocks one or more higher -
priority tasks, it temporarily inherits the highest
priority of the blocked tasks
Priority inheritance: example
T3=T1,
stops
preemptio
n of T3
due to T2
Mailboxes
• Mailboxes provide an intertask communication mechanism,
• A mailbox is actually a special memory location that one or
more tasks can use to transfer data, or more generally for
synchronization.
• The tasks rely on the kernel to allow them to write to the
mailbox via a post operation or to read from it via a pend
operation — direct access to any mailbox is not allowed.
• Two system calls, pend(d, &s) and post(d, &s) , are used to
receive and send mail, respectively
• d , is the mailed data and the second parameter, &s , is the mailbox
location
• the pending task is suspended while waiting for data to appear and
avoid CPU time wastage .
Event Flags
• Event flags are used when a
task needs to synchronize
with the occurrence of
multiple events.
• The task can be synchronized
when any of the events have
occurred. This is called
disjunctive synchronization:
(logical OR).
• A task can also be
synchronized when all events
have occurred : (logical AND).
Message Queues,
• A message queue is used to send one or more
messages to a task.
• A message queue is basically an array of mailboxes.
• Through a service provided by the kernel, a task or
an ISR can deposit a message (or pointer) into a
message queue.
• Similarly, one or more tasks can receive messages
through a service provided by the kernel.
• Generally, the first message inserted in the queue
will be the first message extracted from the queue
(FIFO).
• A task desiring to receive a message from an empty
queue will be suspended and placed on the waiting
list (with time out) until a message is received
• When a message is deposited into the queue,
either the highest priority task or the first task
waiting for the message will be given the message.
Timer and Clock Services
• In developing real - time software, it is desirable to
have easy - to - use timing services available.
• For example, suppose a diagnostic task checks the
“ health ” of an elevator system periodically.
• Essentially, the task would execute one round of
diagnostics and then wait for a notification to run
again, this task will be repeating forever.
• This is usually accomplished by having a
programmable timer that is set to create the
required time interval.
• A system call, delay , is commonly available to suspend
the executing task until the desired time has elapsed,
after which the suspended task is moved to the ready
list.
• The delay function has one integer parameter, ticks , to
specify the length of the delay.
• In order to generate an appropriate time reference, a
timer circuit is configured to interrupt the CPU at a
fixed rate, and the internal system time is incremented
at each timer interrupt.
• The interval of time with which the timer is
programmed to interrupt defines the unit of time in the
system — also called a “ tick ” or time resolution.
Clock Ticks
• A clock tick is a special interrupt that occurs
periodically.
• The time between interrupts is application specific
and is generally between 10 and 200 mS.
• The clock tick interrupt allows a kernel to delay
tasks for an integral number of clock ticks and to
provide timeouts when tasks are waiting for events
to occur.
• The faster the tick rate, the higher the overhead
imposed on the system.
A situation where higher priority tasks and ISRs execute
prior to the task, which needs to delay for 1 tick.
As you can see, the task attempts to delay for 20 mS but
because of its priority, actually executes at varying
intervals.
This will thus cause the execution of the task to jitter
Memory Management in
RTOS
• Dynamic memory allocation is support for on
demand memory requests by applications tasks
and the operating system itself.
• The operating system has to perform effective
memory management in order to keep the tasks
isolated
• Risky allocation of memory is any allocation of
memory that can loose system deterministic
nature.
• Such an allocation can produce the stack over flow,
or a deadlock situation.
• Therefore, it is truly important to avoid risky
allocation of memory, while at the same time
reducing the overhead incurred by memory
management.
• This overhead is a significant component of the
context - switch time and must be minimized
Memory Manager
• When a task / process is created, the memory
manager allocates the memory addresses (blocks)
to it by mapping the process address space.
• Threads of a process share the memory space of
the process
• Memory manager of the OS has to be secure,
robust and well protected.
Memory manager Contd.
• Memory manager should not produce memory
leaks and stack overflows
• Memory leaks means attempts to write in the
memory block that is not allocated to a process or
data structure.
• Stack overflow means that the stack exceeding the
allocated memory block(s)
Memory Managing Strategies for
a system
• Fixed-blocks allocation
• Fixed-size blocks allocation, also called memory pool allocation, uses
a free list of fixed-size blocks of memory (often all of the same size).
This works well for simple embedded systems where no large objects
need to be allocated, but suffers from fragmentation, especially with
long memory addresses. However, due to the significantly reduced
overhead this method can substantially improve performance for
objects that need frequent allocation / de-allocation and is often used
in video games.
• Dynamic -blocks Allocation
• Dynamic Page-Allocation
• Dynamic Data memory Allocation
• Dynamic address-relocation
• Multiprocessor Memory Allocation
• Memory Protection to OS functions
Memory allocation in RTOSes
• RTOS may disable the support to the dynamic block
allocation, MMU support to dynamic page allocation
and dynamic binding as this increases the latency of
servicing the tasks and ISRs
• RTOS may not support to memory protection of the OS
functions, as this increases the latency of servicing the
tasks and ISRs.
• User functions are then can run in kernel space and run
like kernel functions
• RTOS may provide for disabling of the support to
memory protection among the tasks as this increases
the memory requirement for each task
Memory Manager provide
following functions
• Use of memory address space by a process,
• Specific mechanisms to share the memory space and
• Specific mechanisms to restrict sharing of a given
memory space
• Optimization of the access periods of a memory by
using an hierarchy of memory (caches, primary and
external secondary magnetic and optical memories)
• Remember that the access periods are in the following
increasing order: caches, primary and external
secondary magnetic and then or optical.
Fragmentation: Memory
Allocation Problems
• Time is spent in first locating next free memory address
before allocating that to the process.
• A standard memory allocation scheme is to scan a
linked list of indeterminate length to find a suitable free
memory block.
• When one allotted block of memory is de-allocated, the
time is spent in first locating next allocated memory
block before de-allocating that to the process.
• The time for allocation and de-allocation of the
memory and blocks are variable (not deterministic)
when the block sizes are variable and when the
memory is fragmented.
• In RTOS, this leads to unpredictable task performance
• Embedded systems developers commonly implement custom memory-management facilities on top of what the underlying RTOS
provides. Understanding memory management is therefore an important aspect of developing for embedded systems.
• Knowing the capability of the memory management system can aid application design and help avoid pitfalls. For example, in many
existing embedded applications, the dynamic memory allocation routine, malloc, is called often. It can create an undesirable side
effect called memory fragmentation. This generic memory allocation routine, depending on its implementation, might impact an
application's performance. In addition, it might not support the allocation behavior required by the application.
• Many embedded devices (such as PDAs, cell phones, and digital cameras) have a limited number of applications (tasks) that can run
in parallel at any given time, but these devices have small amounts of physical memory onboard. Larger embedded devices (such as
network routers and web servers) have more physical memory installed, but these embedded systems also tend to operate in a
more dynamic environment, therefore making more demands on memory. Regardless of the type of embedded system, the
common requirements placed on a memory management system are minimal fragmentation, minimal management overhead, and
deterministic allocation time.
• This chapter focuses on:
• memory fragmentation and memory compaction,
• an example implementation of the malloc and free functions,
• fixed-size, pool-based memory management,
• blocking vs. non-blocking memory functions, and
• the hardware memory management unit (MMU).
•