You are on page 1of 33

• An RTOS makes it easy to divide your code into smaller

blocks,
tasks,
which execute in parallel and independent of each other
• Having fully independent tasks is rarely possible practice.
• In many cases, tasks need to be activated on a particular event
• e.g., from an interrupt service routine or from another task requesting a
service.
• In such cases, tasks often need to receive related input, i.e., parameters.
• Tasks often need to share hardware resources such as communication
interfaces which can only be used by one task at a time, i.e. mutual exclusion,
a type of synchronization.
• It semaphores can cause various bugs.
• At least some of those bugs stem from undisciplined use:
• allowing code in many different modules to use the same semaphore and hoping that
they all use it correctly.
• These bugs can be compressed before they get crawling simply by hiding the
semaphore and the data that it protects inside of a module, thereby
encapsulating both.
• Similarly encapsulating queues that tasks use to receive messages from other
tasks
To handle a shared flash memory:
• That code deals correctly with synchronizing the requests for reading from
and writing to the flash memory, which was the point.
• However, it is not recommended to use that code in a real system.
Encapsulating Semaphores
• The act of putting Give and Get semaphore routines throughout the
code causes an increased probability of mistakes being made.
• Encapsulate Semaphore routines so that tasks that want to execute
some behavior involving the semaphore make a call to a single routine
• The Get and Give calls are inside this encapsulated routine.
Encapsulating Queues
Mistakes dealing with queues can be reduced by encapsulation.
• Queues typically have a protocol or format associated with.
• Multiple message passing calls increase the probability of a communication
mistake.
• Encapsulating such calls into functions that take specific arguments reduces
these errors.
Encapsulating Consideration
• When encapsulating semaphores or queues:
• Routines are shared by many tasks
• Care must be taken to avoid shared data problems
• Routines must be reentrant.
HARD REAL-TIME SCHEDULING
•Theory Exists For Guaranteeing Absolute Deadlines
•Consider a set of n periodic tasks, each with a period Ti and a worst-case execution time C
•(C1 /T1)+(C2 /T2) +…+(Cn/Tn)≤ bound
•Based On N Tasks, Worst Case Task Times Cn, And Tn Units Of Time.
•To More Complex Analysis Depending On Jitter And Deadlines

•Having predictable execution times is almost as important as being fast.

•It is important to write routines that always execute in the same amount of time or have clear worst case
times.
• Hard real Time Systems - Systems were time constraints are absolutely
critical
• Soft real Time Systems - Systems with time constraints where minor errors
are tolerated
• The obvious issue that arises in hard real-time systems is that it must
somehow guarantee that the system will meet the hard deadlines.
• The ability to meet hard deadlines comes from writing fast code
• To write some frequently called subroutine in assembly language.
• If tasks can be characterized, then analysis can be done to know whether
deadlines can be met or not
• A hard RT RTOS is one, which has predictable performance with no deadline miss, even in case of
sporadic tasks (sudden bursts of occurrence of events requiring attention).
• Automobile engine control system and anti lock brake are the examples of hard real time systems
• Disabling of all other interrupts of lower priority when running the hard real time tasks
• Preemption of higher priority task by lower priority tasks  Some critical code in assembly to
meet the real time constraint (deadline) fast
• Task running in kernel space, [This saves the time required to first check whether access is
outside the memory space allocated to the kernel functions.]
• EX: Application having hard real-time tasks is an anti-missile system.
• An anti-missile system must first detect all incoming missiles, properly position the anti-missile gun, and then fire to
destroy the incoming missile before the incoming missile can do any damage.
• All these tasks are hard real-time in nature and the anti-missile system would be considered to have failed, if any of its
tasks fails to complete before the corresponding deadlines.
• For hard real-time tasks in practical systems, the time bounds usually range from several micro
seconds to a few milli seconds.
• It may be noted that a hard real-time task does not need to be completed within the shortest
time possible, but it is merely required that the task must complete within the specified time
bound.
Saving memory:
 Embedded systems often have limited memory
 RTOS: each task needs memory space for its stack.
 Two methods for determining space requirements:
 The first method for determining how much stack space a task needs is to
examine your code. Trace the deepest functional calls possible.
 Add all the local variables, parameters and interrupt routine needs.
 Accurate but very difficult to carry out.
 The second method is experimental. Fill each stack with some
recognizable data pattern at startup, run the system for a period of time
 Easy but not so accurate. Might have missed the worst case.
Saving Memory Space
Program Memory:
 Limit the number of functions used
 Check the automatic inclusions by your linker: may consider writing own functions
 Include only needed functions in RTOS
 Consider using assembly language for large routines
 Make sure that you are not using two functions to do the same thing.
 Check that your development tools are not sabotaging you.
 Configure your RTOS to contain only those functions that you need.
 Look at the assembly language listings created by your cross-compiler to see if certain of your C
statements translate into huge numbers of instructions.
Data Memory:
 Consider using more static variables instead of stack variables
 On 8-bit processors, use char instead of int when possible
•ROM (code) and RAM(data) are separate.
• Packing data tightly and neatly usually consumes more code space.
•It is often necessary to save space to get everything to fit .
Saving power:
The primary method for preserving battery power is to turn off parts or all of the system
whenever possible.
Most embedded-system microprocessors have at least one power-saving mode; many have
several.
The modes have names such as sleep mode, low-power mode, idle mode, standby mode, and so
on.
A very common power-saving mode is one in which the microprocessor stops executing
instructions, stops any built-in peripherals, and stops its clock circuit.
This saves a lot of power, but the drawback typically is that the only way to start the
microprocessor up again is to reset it.
Static RAM uses very little power when the microprocessor isn't executing instructions
Another typical power-saving mode is one in which the microprocessor stops executing instructions
but the on-board peripherals continue to operate.
Another common method for saving power is to turn off the entire system and have the user turn it
back on when it is needed.
• To some extent, the ability to meet hard deadlines comes from writing fast
code.
• Writing fast code for real-time systems is not very different from writing fast
code for applications,
• however. Faced, for example, with writing a system of either kind that
searches fix data items frequently but adds and deletes them rarely,
Host and Target machines
• Host: –
A computer system on which all the
programming tools run – Where the
embedded software is developed,
compiled, tested, debugged, optimized,
and prior to its translation into target
device.
• Target: –
After writing the program, compiled,
assembled and linked, it is moved to
target – After development, the code is
cross-compiled, translated – cross-
assembled, linked into target
processor instruction set and located
into the target.
Linkers and Locators
The process of converting the source code representation of embedded
software into an executable binary image involves three distinct steps:

1.Each of the source files must be compiled or assembled into an object


file.
2.All of the object files that result from the first step must be linked
together to produce a single object file, called the relocatable
program.
3.Physical memory addresses must be assigned to the relative offsets
within the relocatable program in a process called relocation.
Linkers and Locators
Program Execution
• Translator
• A translator is a program that converts source code into object code.
• Generally, there are three types of translator: compilers. interpreters. assemblers.
• Linker
• A System Software that Combines two or more separate object programs and supplies the
information needed to allow references between them : Assemblers
• Relocator
• Relocation is the process of assigning load addresses for position-dependent code
and data of a program and adjusting the code and data to reflect the assigned
addresses.
• Loader
• A loader is a system program, which takes the object code of a program as input and
prepares it for execution.
Linker
• Tool that merges the object files produced by separate compilation or
assembly and creates an executable file.

• Tasks of Linker
• Searches the program to find library routines used by program, e.g.
printf(),sqrt(),strcat() and various other.

• Determines the memory locations that code from each module will occupy
and relocates its instructions by adjusting absolute references.

• It combines two or more separate object programs and supplies the


information needed to allow references between them .
• Computer programs typically comprise several parts or modules
• All these parts/modules need not be contained within a single
object file, and in such case refer to each other by means of
symbols
• An object file can contain three kinds of symbols:
• Publicly defined symbols, which allow it to be called by other modules ,
also called as public definition .
• Externally defined symbols(undefined symbols), which calls the other
modules where these symbols are defined, also called as external
reference.
• Local symbols, used internally within the object file to facilitate
relocation.
Types of linkers
• Two Types
– Dynamic Linker – Static Linker
Static Linker:
Static linkers takes input as collection of relocatable object files and command line
arguments and generate output as a fully linked executable object file that can be
loaded and run.
The advantage of static linking
self-contained, independent programs can be created.
In other words, the executable program will consist of one part (the .EXE file) that you need
to keep track of.
Disadvantages:
The behavior of executable files cannot be changed without relinking them.
External called programs cannot be shared, requiring that duplicate copies of programs be
loaded in memory if more than one calling program needs to access them.
Dynamic Linker:
Dynamic linker is the part of an OS that loads and links the shared libraries needed by
an executable when it is executed (at "run time"), by copying the content of libraries
from persistent storage to RAM, filling jump tables and relocating pointers.
The specific operating system and executable format determine how the dynamic linker
functions and how it is implemented.
Advantages
• Often-used libraries need to be stored in only one location, not duplicated in every single binary.
• If an error in a library function is corrected by replacing the library, all programs using it
dynamically will benefit from the correction after restarting them.
Programs that included this function by static linking would have to be re-linked first.
Disadvantages:
• Known on the Windows platform as "DLL Hell", an incompatible updated DLL will break
executables that depended on the behavior of the previous DLL.
• A program, together with the libraries it uses, might be certified (e.g. as to correctness,
documentation requirements, or performance) as a package, but not if components can be replaced.
Loader
It is a SYSTEM PROGRAM that brings an executable file residing on disk into memory and
starts it running.
• Steps:-

– Read executable file’s header to determine the size of


text and data segments.

– Create a new address space for the program.

– Copies instructions and data into address space.

– Copies arguments passed to the program on the stack.

– Initializes the machine registers including the stack pointer.

–Jumps to a startup routine that copies the program’s arguments from the stack to registers
and calls the program’s main routine.
Types of Loaders
 Compile/Assemble and Go loader
 Absolute Loader
 Relocating Loader
 Direct Linking loader
Assemble-and-go Loader
• Compilation, assembly, and link steps are not separated from program
execution all in single pass.

• The intermediate forms of the program are generally kept in RAM, and not
saved to the file system.

• Compile and go systems differ from interpreters, which either directly


execute source code or execute an intermediate representation.
Advantages
• The user need not be concerned with the separate steps of compilation,
assembling, linking, loading, and executing.

• Execution speed is generally much superior to interpreted systems.

• They are simple and easier to implement.


Disadvantages
• There is wastage in memory space due to the presence of
the assembler.

• The code must be reprocessed every time it is run.

• Systems with multiple modules, possibly in different


languages, cannot be handled naturally within this
framework.

• Compile-and-go systems were popular in academic


environments, where student programs were small,
compiled many times, usually executed quickly and, once
debugged, seldom needed to be re-executed.
Absolute
Loader
Absolute Program
• Advantage:
• Simple and efficient
• No linking or relocation

• Disadvantage:
• Difficult to use subroutine libraries.
• The need of programmer to state the actual address.
Algorithm for Absolute Loader
1. begin
2. read Header record
3. verify program name and length
4. read first Text record
5. while record type <> ’E’ do
6. begin
7. {if object code is in character form, convert into
8. internal representation}
9. move object code to specified location in memory
10. read next object program record
11. end
12. jump to address specified in End record
13. end
Bootstrap Loader
• Special Type of Absolute Loader.

• When a computer is first tuned on or restarted bootstrap


loader is executed.

• This bootstrap loads the first program to be run by


computer that is the OS.

• It loads the first address 0x80.


Relocation
• Execution of the object program using any
part of the available and sufficient memory.
• The object program is loaded into memory
wherever there is room for it.
• The actual starting address of the object
program is not known until load time.
Relocating Loader
• Efficient sharing of the machine with
larger memory and when several
independent programs are to be run
together.

• Support the use of subroutine libraries


efficiently.
Direct Linking Loader
• This type of loader is a relocating loader.
• The loader cannot have the direct access to the source
code.
• To place the object code 2 types of addresses can be
used:-

1. ABSOLUTE : In this the absolute path of object code is


known and the code is directly loaded in memory.

2. RELATIVE :In this the relative path is known and this


relative path is given by assembler.
Work Of Assembler in Direct Linking Loade
The assembler should give the following information to the
loader:

 The length of the object code segment.

 A list of external symbols (could be used by diff.


segment).

 List of External symbols(The segment itself is using)

Information about address constants

 Machine code translation of the source program

You might also like