Chapter Three
Software Frameworks for Real-time
and Embedded Systems
1
Module Outline
✓ Real-time operating system: definitions, Characteristics, functionality,
structure and support for applications
✓ Features of a real time operation system
✓ General and specific microprocessors
✓ Inter process communication
✓ Real time task scheduling
✓ RTOS support for semaphores, queues, and events
✓ Dynamic allocation of tasks
✓ Scheduling: Cyclic scheduling, Priority-based scheduling
✓ Multi-tasking and Concurrency issue
✓ Fault- tolerance
✓ Synchronization techniques: Centralized clock synchronization, Distributed
clock synchronization
✓ Handling resource sharing and dependencies
2
Real Time Operating System (RTOS)
➢ RTOS?
1. A RTOS is an OS that guarantees a certain capability within a specified
time constraint.
2. 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
3
to unpredictable events and process multiple events concurrently
Introduction
➢ RTOS is key to many embedded systems today and, provides a software
platform upon which to build applications.
➢ Not all embedded systems are designed with an RTOS. Some embedded
systems with relatively simple hardware or a small amount of software
application code might not require an RTOS.
➢ Many embedded systems with moderate-to-large software applications
require some form of scheduling, and these systems require an RTOS
➢ Although, some functional similarities exist between RTOS and General
Purpose Operating System (GPOS), many differences occur as well. 4
Characteristics of a RTOS
❑ RTOS generally have the following characteristics:
1. Small footprint. Compared to general OS, RTOS are lightweight.
2. High performance. RTOS are typically fast and responsive.
3. Determinism. Repeating inputs end in the same output.
4. Safety and security: Safety-critical and security standards are typically the
highest priority, as RTOS are frequently used in critical systems.
5. Priority-based scheduling: Tasks that are assigned a high priority are
executed first followed by lower-priority jobs.
6. Timing information: RTOS are responsible for timing and providing
5
application programming interface
Cntd…
➢ Some core functional similarities between a typical RTOS and GPOS
❖ Software and hardware resource management,
❖ Some level of multitasking
❖ Provision of underlying OS services to applications, and
❖ Abstracting the hardware from the software application
➢ Some key functional differences that set RTOS apart from GPOS
❖ Better reliability in embedded application contexts
❖ The ability to scale up or down to meet application needs,
❖ Faster performance,
❖ Reduced memory requirements,
❖ Support for diskless embedded systems by allowing executables to
boot and run from ROM or RAM, and
❖ Better portability to different hardware platforms 6
Structure of RTOS
➢ Basic Structure is similar to regular OS but, in addition, it provides
mechanisms to allow real time scheduling of tasks.
➢ Though RTOSs may or may not increase the speed of execution, they can
provide much more precise and predictable timing characteristics than
general-purpose OS.
7
Cntd…
➢ The heart of a real-time OS (and the heart of every OS) 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 neglected out of memory, and the
scheduler will start running the child tasks. 8
Basic Kernel Services
➢ The "kernel" of a RTOS provides an "abstraction layer" that hides from
application software from the hardware.
➢ Details of the processor (or set of processors) upon which the application
software will run.
➢ The Kernel provides
▪ An interrupt handler
▪ Task scheduler
▪ Resource sharing flags and
▪ Memory management
9
Basic functions of RTOS
❑ The following are some of the basic function of RTOS
➢ Task Management
➢ Interrupt handling
➢ Memory management
➢ Exception handling
➢ Task synchronization
➢ Task scheduling
➢ Time management
1. Task Management
➢Tasks are implemented as threads in RTOS
➢Have timing constraints for tasks
➢Each task a triplet: (execution time, period, deadline)
➢Can be initiated any time during the period
Cntd…
2. Interrupt handling
3. Memory management 4. Exception handling
➢ When a task is created the RTOS
➢ Standard techniques:
simply returns an already
❖ System calls with error
initialized memory location.
➢ when a task dies, the RTOS returns code
the memory location to the pool ❖ Fault-tolerance 11
Applications of RTOS
Real-time systems are used in:
➢ Airlines reservation system.
➢ Air traffic control system.
➢ Systems that provide immediate updating.
➢ Defense application systems like RADAR.
➢ Networked Multimedia Systems
➢ Command Control Systems
➢ Internet Telephony
➢ Anti-lock Brake Systems
➢ Heart Pacemaker etc…
12
Features of RTOS
Here are important features of RTOS:
➢ Occupy very less memory
➢ Consume fewer resources
➢ Response times are highly predictable
➢ Unpredictable environment
➢ The Kernel saves the state of the interrupted task and then determines
which task it should run next.
➢ The Kernel restores the state of the task and passes control of the CPU
for that task.
13
General and specific microprocessors
➢ The system designer created to produce large numbers microprocessors for a
variety of applications.
➢ Advantages:
▪ Easy to design and use
▪ Design time is low
▪ Design cost is low
▪ Re programmability
➢ Disadvantages:
▪ Performance is not very good
▪ Size is high
▪ They consume much power
14
Specific purpose microprocessor
➢ An embedded designer creates a single-purpose processor by designing a
custom digital circuit for one program.
➢ Advantages:
▪ Performance is very good
▪ Small size (exact to fit one solution)
▪ Consume little power
e.g., Microcontroller (MCU)
➢ Disadvantages:
▪ not very easy to design and design time is thus high
▪ Design cost is thus higher
▪ Reprogramming is difficult
▪ Limited flexibility: not easy to make changes
▪ e.g., Application-specific integrated circuit (ASIC), application-specific
standard product (ASSP) 15
Context switching mechanisms
A context switch
➢ process switch or a task switch
➢ switching of the CPU from one process/task to another
➢ It is an essential feature of multitasking OS
How multitasking is working with single CPU?
➢ A multitasking OS
▪ CPU simultaneously execute multiple tasks
▪ it is an illusion of concurrency
▪ It is achieved by means of context switching
Task: decomposed application into small, schedulable, and sequential program units
governed by three time-critical properties;
Release time
Deadline
Execution time
16
Cntd…
❑ Each task may exist in following states
➢ Dormant: Task doesn’t require CPU time
➢ Ready: Task is ready to go active state, waiting processor time
➢ Active: Task is running
➢ Suspended: Task put on hold temporarily
➢ Pending: Task waiting for resource.
17
Cntd…
❑ What happened during switching ?
➢ Context of the to-be-suspended task will be saved
➢ Context of the to-be-executed task will be retrieved
❑ Task Control block(TCB):
➢ Task uses TCBs to remember its context.
➢ Accessible only by RTOS
➢ Information on TCB could be
Task_ID
Task_State
Task_Priority
Task_Stack_Pointer
Task_Prog _Counter
18
Inter-process communication
➢ Tasks usually need to communicate and synchronize with each other for
different reasons such as:
Accessing a shared resource
To signal the occurrence of events to each other
➢ RTOS provides inbuilt inter-task primitives , which are kernel objects that
facilitate these synchronization and communication
➢ Examples of such objects include:
Semaphores
Message queues
Signal and so on
19
Cntd…
❑ Semaphores is a kernel object that one or more tasks can acquire or release for :
Mutual exclusion
Signaling the occurrence of an event
Synchronizing activities among tasks
➢ Semaphores have
➢ An associated semaphore control block (SCB),
➢ A unique ID,
➢ A user-assigned value (binary or a count), and
➢ A task-waiting list.
➢ There are two types
Binary semaphores (0 or 1)
If value =0 => semaphore is not available
If value = 1 => semaphore available
Counting semaphores (>= 0)
20
uses a count to allow it to be acquired or released multiple times
Real time task scheduling
❑ The two major groups are run-time and pre-run-time
schedulers.
A. Run time Scheduler is referred to priority based schedulers where the decision
of which task will execute next is computed dynamically at run time.
B. Pre-run-time schedulers are those which computation work about the execution
line is done previous to run-time.
▪ These are also known as cyclic or clock driven schedulers.
A. Priority-based Scheduling is based in the idea that each task in the systems is
assigned a priority according to some decision base.
➢ Consistent with priority, the task will be assigned for execution while the system
is running.
➢ The main question here is, which factor do we use to assign the priorities?
❖ priority-based algorithms, dynamic-priority and static-priority scheduling. 21
Cntd…
B. Cyclic scheduling (also known as clock-driven, pre-run time)
➢ This schedule is computed off-line before the system begins to execute, and the
computation is based on the knowledge of the release times and resource
requirements of all the jobs for all times.
➢ It offers, general advantages for deterministic systems, in which release times and
job demands do not vary or vary slightly with time.
➢ But the problem is what happens with those systems in which the future work
load is relatively unpredictable?
❑ Scheduling policies: keeping record of the state of each task and allocates the
CPU to one of them
➢ Scheduler is responsible for time-sharing of CPU among tasks
22
Cntd…
➢ More information about the tasks are required
➢ Number of tasks
➢ Resource Requirements
➢ Execution time
➢ Deadlines
➢ Scheduling algorithms
❖ Clock Driven Scheduling
❖ Weighted Round Robin Scheduling
❖ Priority Scheduling
1. Clock Driven Scheduling
➢ All parameters about jobs (execution time/deadline) known in advance
➢ Schedule can be computed at some regular time instances
➢ Minimal runtime overhead
➢ Not suitable for many applications
23
Cntd…
2. Weighted Round Robin Scheduling
Jobs scheduled in FIFO manner
Time quantum given to jobs is proportional to it’s weight
Example use : High speed switching network : Job A can run only after Job B.
3. Priority Scheduling
Processor never left idle when there are ready tasks
Processor allocated to processes according to priorities
Priorities
Static - at design time
Dynamic - at runtime
Earliest Deadline First (EDF
Least Slack Time First (LSF): slack = relative deadline – execution left
Rate Monotonic Scheduling (RMS): is an optimal fixed-priority policy where
the higher the frequency (1/period) of a task, the higher is its priority.
Tasks priority inversely proportional to it’s period. 24
Message passing and shared memory communications
Message Passing is a form of communication used in inter process communication.
➢ Communication is made by the sending of messages to recipients.
➢ Each process should be able to name the other processes.
➢ The producer typically uses send() system call to send messages,
➢ and the consumer uses receive() system call to receive messages.
➢ synchronous or asynchronous,
➢ Could either be between processes running on a single machine,
➢ Could be done over the networked machine.
Message Queue: kernels provide an object called a message queue
➢ Used to place task send or received messages
➢ a buffer-like object through which tasks and ISRs send and receive messages to
25
communicate and synchronize with each others
Cntd…
➢ Message queues have
an associated message queue control block (QCB),
a name,
a unique ID,
memory buffers,
a message queue length,
a maximum message length,
Shared Memory communication
➢ Is an OS provided abstraction
➢ allows a memory region to be simultaneously accessed by multiple programs
➢ One process will create an area in RAM which other processes can access
➢ Since both processes can access the shared memory area like regular working
memory, this is a very fast way of communication
➢ It is less powerful, as for example the communicating processes must be running
26
on the same machine
Dynamic allocation of tasks
➢ Task in real time system, the system is subjected to real time. i.e, response
should be guaranteed within a specified time constraint or system should
meet the specified deadline.
➢ There are two types in real time systems: Periodic and dynamic tasks
➢ Periodic tasks: jobs are release in at a regular interval and repeats itself
after a fixed time interval.
➢ Dynamic Tasks: it is a sequential program that is invoked by the occurrence
of an event. Dynamically arriving tasks can be categorized on their critical
and knowledge about their occurrence times.
➢ Dynamic task allocation is a class of task allocation in which the task is a
dynamic process and may need to be continuously adjusted in response to
changes in the task environment.
❖ It is more efficient 27
Multi-tasking and Concurrency issue
➢ Multitasking is the capability of the OS of running several applications at
the same time.
➢ When CPU resources are over-committed, the OS schedules the different
active processes by performing a process switch at regular time intervals,
distributing the available CPU cycles according to a well-defined priority
policy.
➢ Multitasking is the natural execution model of single or multiprocessor
systems.
➢ Switching among different processes naturally induces an execution
overhead.
➢ For a system to be real-time, there must be a guaranteed time frame where
within a task must execute.
➢ In a multitasking RTOS the task scheduling, switching and execution
elements are key to the effectiveness of the OS in real-time requirements. 28
What is concurrency in real time systems?
➢ Concurrency is the execution of the multiple instructions at the same time.
➢ It happens in the operating system when there are several process threads
running in parallel.
➢ The running process threads always communicate with each other through shared
memory or message passing.
➢ Concurrency in sharing of resources result in problems like deadlocks and
resources starvation.
29
Fault tolerance
➢ Fault tolerance refers to the ability of a system (computer, network,
cloud cluster, etc.) to continue operating without interruption when one
or more of its components fail.
➢ Systems employ backup elements that take the place of the failed
component to ensure there's no loss in productivity for your business.
➢ Fault tolerance can be achieved by either hardware or software or time
redundancy.
➢ Safety-critical applications have strict time and cost constraints, which
means that not only faults have to be tolerated but also the constraints
should be satisfied.
30
Synchronization techniques: Centralized clock synchronization, Distributed clock
synchronization
❑ Distributed System is a collection of computers connected via the high
speed communication network.
➢ In the distributed system, the hardware and software components
communicate and coordinate their actions by message passing.
➢ Each node can share their resources with other nodes.
➢ So, there is need of proper allocation of resources to preserve the state of
resources and help coordinate between the several processes.
➢ To resolve such conflicts, synchronization is used.
❑ Synchronization in distributed systems is achieved via clocks.
➢ The physical clocks are used to adjust the time of nodes.
➢ Each node in the system can share its local time with other nodes in the
system.
31
Cntd…
❑ The time is set based on UTC (Universal Time Coordination).
❑ UTC is used as a reference time clock for the nodes in the system.
➢ There are 2 types of clock synchronization algorithms: Centralized
and Distributed.
A. Centralized clock synchronization
➢ It is the one in which a time server is used as a reference.
➢ The single time server propagates its time to the nodes and all the nodes
adjust the time accordingly.
➢ It is dependent on single time server so if that node fails, the whole
system will lose synchronization.
➢ E.g., of centralized are-Passive Time Server, Active Time Server etc.
32
Cntd…
B. Distributed clock synchronization
➢ It is the one in which there is no centralized time server present.
➢ The nodes adjust their time by using their local time and then, taking the
average of the differences of time with other nodes.
➢ Distributed algorithms overcome the issue of centralized algorithms
like the scalability and single point failure.
➢ Examples of Distributed algorithms are: Global Averaging Algorithm,
Localized Averaging Algorithm, NTP (Network time protocol) etc.
33
Handling resource sharing and dependencies: Priorities and reentrancy, Resource
sharing protocols
➢ Most the different task scheduling algorithms that the different tasks in a
system are all independent.
➢ However, that is rarely the case in real-life applications.
➢ Tasks often have explicit dependencies specified among themselves,
however implicit dependencies are more common.
➢ Tasks might become inter dependent for several reasons.
➢ A common form of dependency arises when one task needs the results of
another task to proceed with its computations.
➢ Resource sharing protocols (Home work)
34
End of Chapter Three
Any Question???
35