Chapter 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Chapter-5 (Real Time Operating System)

 General purpose operating system and RTOS.

 RTOS Examples:-.
1. LynxOS.
2. OSE.
3. QNX.
4. RTLinux.
5. VxWorks.
6. Windows CE.

RTOS:- A real-time operating system (RTOS) is a program that schedules execution in a timely
manner, manages system resources, and provides a consistent foundation for developing
application code. An operating system is considered real-time if it invariably enables its
programs to perform tasks within specific time constraints, usually those expected by the user.

 Characteristics of RTOS:

1. Reliability
2. Consistency
3. Predictability
4. Performance
5. Scalability
6. Compactness
1. Reliability: A reliable system is one that is available (continues to provide service) and does
not fail. Embedded systems and hence RTOS used in such systems must be reliable.
2. Consistency: A key characteristic of an RTOS is the level of its consistency concerning the
amount of time it takes to accept and complete an application's task; the variability is 'jitter’.
A 'hard' real-time operating system has less jitter than a 'soft' real-time operating system.
3. Predictability: The RTOS used in this case needs to be predictable to a certain degree. The
term deterministic describes RTOSes with predictable behavior, in which the completion of
operating system calls occurs within known timeframes.
4. Performance: This requirement dictates that an embedded system must perform fast

5. Scalability: Because RTOSes can be used in a wide variety of embedded systems, they must
be able to scale up or down to meet application-specific requirements.
6. Compactness: In embedded systems, where hardware real estate is limited due to size and
costs, the RTOS clearly must be small and efficient. In these cases, the RTOS memory
footprint can be an important factor.

 Examples of RTOS:
 calculators, heart pacemakers
 Electric Geyser where water temperature is controlled in real time in the industry the
process parameters like temperature, flow, or pressure or status of a device (say a valve
open or close) are continuously monitored and instant actions are initiated
 Room Air Conditioner which adaptively controls the temperature of a room.
 Electric Power System which controls power quality parameters like Frequency, Peak
Voltage, Power Factor, e.t.c.
 Machine vision guided robotics
 Digital audio decoding, transport over a network and encoding using a simple off-theshelf
sound card (VoIP or internet radio applications).

 Functions of RTOS.
1. Task management (Inter Task Communication & Multi Tasking)
2. Synchronization and communication
3. Memory management
4. Time management
5. Interrupt and event handler
6. I/O management
7. Interrupt Handling & Resource allocation
8. Scheduling
 Resource allocation and Interrupt handling function of RTOS.

When an interrupt occurs, interrupt Service Routines (ISR) is run. Most interrupt routines in RTOS
Copy peripheral data into a buffer , Indicate to other code that data has arrived and Acknowledge
the interrupt (tell hardware) RTOS normally disable the interrupts while handling critical section and
enable after the critical section has been executed. Interrupt latency is a factor to look for, when
selecting a RTOS.

Interrupt latency = Maximum amount of time interrupts are disabled+ time to start execution of first
instruction of ISR. It is desirable that RTOS should have minimum interrupt latency.

ii) Resource allocation:

Sharing of resources by competing tasks as per their execution schedules is a function RTOS. This
means that tasks should have the required resources allocated to them whenever they are needed.
The Operating System allocates resources when a task need them. When the task terminates, the
resources are de-allocated, and allocated to other tasks that need them. Resources can be allocated
in Round Robin method or Priority based. Some resources are non-Pre emptible eg. Mutex.

In Round Robin, tasks are scheduled in FIFO manner. Fixed Time quantum is given to the tasks after
which it is pre-empted. Priority Scheduling, resources are allocated to processes according to
priorities.

 Scheduling
Scheduling algorithms of RTOS:
1. First in first out
2. Round-robin algorithm
3. Round robin with priority
4. Shortest job first
5. Non Preemptive multitasking
6. Preemptive multitasking
 Round Robin (RR)

• Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this
time has elapsed, the process is preempted and added to the end of the ready queue.

• If there are n processes in the ready queue (as a FIFO) and the time quantum is q, then each
process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more
than (n-1)q time units.

Example of RR with Time Quantum = 20

Process Burst Time

P1 53

P2 17

P3 68

P4 24

The Gantt chart is:


 Inter-task communication with reference to RTOS:-

Inter task communication:- Inter task communication involves sharing of data among tasks
through sharing of memory space, transmission of data, etc..

Some of the mechanisms available for executing Inter Task communications are :

a) Message queue
b) Pipes
c) Remote procedure call

Inter task communications is executed using following mechanisms

 Message queues - A message queue is an object used for inter task communication through
which task send or receive messages placed in a shared memory.
The queue may follow:
1) First In First Out (FIFO)
2) Last in First Out(LIFO) or
3) Priority (PRI) sequence.
Usually, a message queue comprises of an associated queue control block (QCB), name,
unique ID, memory buffers, queue length, maximum message length and one or more task
waiting lists. A message queue with a length of 1 is commonly known as a mailbox.
 Pipes – A pipe is an object that provide simple communication channel used for
unstructured data exchange among tasks. A pipe does not store multiple messages but
stream of bytes. Also, data flow from a pipe cannot be prioritized.

 Remote procedure call (RPC) - It permits distributed computing where task can invoke the
execution of another task on a remote computer.

 Multitasking communication with reference to RTOS:-

Embedded system are generally specific but need to perform many task for same application let us
consider example of grinding control machine A simple microcontroller program can only do one
thing at a time. However, because it can do things very fast (millions of operations per second), it
can be made to switch between tasks so fast that it gives an illusion of doing several things
concurrently. The question is, how do you program it so it will divide its attention between multiple
tasks, Round robin and round robin with interrupts are ways to achieves the multitasking Imagine
now a program with 25 steps in the main function plus 5 sub-functions (heating water, updating the
display, watching 3 push buttons). Suppose some of those sub-functions also contain a number of
steps with delays. Very, very quickly the complexity of the program becomes quite impossible to
untangle.

Pre-emptive multitasking :
 In pre-emptive multitasking the highest priority task always given the CPU time .
 If lower priority task is presently running and higher priority task is in ready to run state then
the running task is pre-empted and the higher priority task is executed .
 The Kernel restore the state of current task and pass control of the CPU to the higher
priority task.
 Again after some time the high priority task may releases the CPU and the low priority task
executed.
 It is used in commercial embedded systems.
 Semaphores:
 It is a system of sending message by using flags. Multiple concurrent threads of execution
with an application must be able to synchronize their execution & co-ordinate mutually
exclusive access to shared resources.
 To fulfill this requirement RTOS kernel provides a semaphore object that one or more
threads of execution can acquire or release for the purpose of synchronization or mutual
exclusion. Semaphore is like a key that allows a test to carry out some operation or to access
a resource. When task acquires the semaphore, it receives an access to the resource.
 The number of times the semaphore can be acquired by the task is determined by the
resource count of a semaphore.
 A resource count is decremented by one, when a task acquires the semaphore and its
resource count is incremented by one when a task releases the semaphore.
 A kernel supports many different types of semaphores :
 Binary: Binary semaphores are used for both mutual exclusion and synchronization
purposes. A binary semaphore is used to control sharing a single resource between tasks.
 Counting: it is a semaphore that increments when an IPC is given by a task. It decrements
when a waiting task unblocks and starts running.
 Mutex or Mutually Exclusion semaphore: In this a mutex key is used to lock the shared
resource, if it is acquired by the task, so that any other task cannot acquire until it is
released.

The main difference is that a semaphore can be “waited for” and “signaled” by any task, while only
the task that has taken a mutex is allowed to release it.
Example of using semaphores for Synchronization:

Assume two concurrent process P1 and P2 having statements S1 and S2.

We want in any case S1 should execute first. this can be achieved easily by initialize Sem=0;

In process P1

// Execute whatever you want to do

// before executing P2 S1;

signal(Sem);

in process P2

wait(Sem); S2;

 Watchdog timer
A watchdog timer is a piece of hardware that can be used to automatically detect software
anomalies and reset the processor if any occur. Generally speaking, a watchdog timer is based on a
counter that counts down from some initial value to zero. The embedded software selects the
counter's initial value and periodically restarts it. If the counter ever reaches zero before the
software restarts it, the software is presumed to be malfunctioning and the processor's reset signal
is asserted. The processor (and the embedded software it's running) will be restarted as if a human
operator had cycled the power.
Figure 1 shows a typical arrangement. As shown, the watchdog timer is a chip external to the
processor. However, it could also be included within the same chip as the CPU. This is done in many
microcontrollers. In either case, the output from the watchdog timer is tied directly to the
processor's reset signal.
 Deadlock

A deadlock is a situation where in two or more competing actions are each waiting for the other to
finish, and thus neither ever does.

Reason of occurance:-

 Assume thread/process T1 has exclusive access to resource R1.


 Thread/ process T2 has exclusive access to resource R2.
 If T1 needs exclusive access to R2 and T2 needs exclusive access to R1,
Neither thread can continue. They are deadlocked.

The simplest example is that of two tasks: 1 and 2. Each task requires two mutexes: A and B. If Task 1
takes mutex A and waits for mutex B while Task 2 takes mutex B and waits for mutex A, then each
task is waiting for the other to release the mutex.

These tasks may run without problems for a long time, but eventually one task may be preempted in
between the wait calls, and the other task will run. In this case, Task 1 needs mutex B to be released
by Task 2, while Task 2 needs mutex A to be released by Task 1. Neither of these events will ever
happen.

Causes of Deadlock:-

 Mutual exclusion: only one process at a time can use a resource


 Hold and wait: A process holding at least one resource is waiting to acquire additional
resources held by other resources
 No preemption: A resource can be released only voluntarily by the process holding it after
that process has completed the task
 Circular wait: A set of processes- P1 to Pn …P1 waiting for the resource held by P2, P2
waiting for resource held by P3 etc.
 Methods for Handling Deadlocks
 Ensure that the system will never enter a deadlock state.
 Allow the system to enter a deadlock state and then recover.
 Ignore the problem and pretend that deadlocks never occur in the system

 Deadlock Prevention:-

■ Mutual Exclusion – not required for sharable resources; must hold for nonsharable resources.

■ Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any
other resources.

● Require process to request and be allocated all its resources before it begins execution, or
allow process to request resources only when the process has none.

● Low resource utilization; starvation possible.

■ No Preemption –

● If a process that is holding some resources requests another resource that cannot be
immediately allocated to it, then all resources currently being held are released.

● Preempted resources are added to the list of resources for which the process is waiting.

● Process will be restarted only when it can regain its old resources, as well as the new ones
that it is requesting.

■ Circular Wait – impose a total ordering of all resource types, and require that each process
requests resources in an increasing order of enumeration.

 Deadlock Avoidance:-

■ Simplest and most useful model requires that each process declare the maximum number of
resources of each type that it may need.

■ The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure


that there can never be a circular-wait condition.

■ Resource-allocation state is defined by the number of available and allocated resources, and the
maximum demands of the processes.

 Deadlock Detection

■ Allow system to enter deadlock state

■ Detection algorithm

■ Recovery scheme

You might also like