Sunteți pe pagina 1din 50

Unit 2

Real Time Systems


Concepts
Foreground/Background Systems
A small system with low complexity are called
foreground/background system.
Various functions are used to perform desired
operation which is called background.
Interrupt service routines (ISR) handles
asynchronous events called foreground.
Foreground/Background Systems
ISR also called interrupt level and background
or functions are called task level.
ISR provides information to background
modules.
These information can execute by the
background modules.
Foreground/Background Systems
Background module require some time to
execute these information which is depends
the function or the loop execution time.

If a code change is made, the timing of loop is


affect.
Critical Section of Code
In this section of a code execution must not be
interrupted by interrupts.
In this section of a code interrupts are disabled
before the critical code is executed.
And interrupts are enable only when critical
code is finished.
Resources and Shared Resources
Resources
Any unit which used by task.
It can be any I/O device like printer, monitor,
keyboard, variable, structure or an array.
Shared resource
Used by more than one task.
Each task should gain special access to the shared
resource to prevent data corruption.
Multitasking
Multitasking
A process of scheduling and switching the CPU
between several task.
A CPU switches its attention between several
sequential task.
Maximizes the use of the CPU.
Easy to design and maintain application program.
Task/Thread
A real time system splits its work into several
program called task.
Each task executes its work for which it is
designed.
Each task is assigned a priority with its own set of
CPU registers and its own stack area.
Various type of states of task are:
Dormant state: Task that resides in memory but has not
made available to execute.
Ready state: Task can execute but its priority is less than
the currently running task.
Running state: Task has control of the CPU.
Task/Thread
Various type of states of task are:
Waiting state: A task requires the occurrence of the event
(Like waiting for I/O operation to complete or a shared
resource to be available, a timing pulse to occur).
ISR state: When an interrupt has occur and CPU is in the
process of service the interrupt.
Context Switch/Task Switch
Steps to switch the task
The current task context i.e. contents of CPU registers
saves in the current task context area.
The new task context restore from its storage area to the
CPU registers and then new task code resumes to start
execution.

The more the registers of the CPU has the higher


the time required to switch the task.
Kernel
Kernel is a software which manages CPU time
for tasks switching and communication between
tasks.
Task switching is the main function of Kernel.
The services provide by kernel require extra time
which is additional load on the system.
In a well design application kernel required 2 to
5% of CPU time.
Kernel
Kernel required additional space of system ROM and
each task requires its own stack space which occupy
RAM space.
Small systems generally not use real time kernel,
since they have very little RAM.
Various services of Kernel
Semaphore management
Mailboxes
Queues
Time delays
Scheduler
Scheduler are also called dispatcher.
This is a part of kernel and responsible for
determining which task runs next.
In priority kernel, each task assign a priority
according to their importance.
Control of the CPU is always given to the task
whose priority is highest and ready to run.
Non-Preemptive Kernel
There are two types of Kernel
Non-Preemptive Kernel
Preemptive Kernel
Non-preemptive scheduling is also called
cooperative multitasking as task cooperate
with each other to share the CPU.
ISR has highest priority, can interrupt the
current task and after completion of execution
returns to the interrupted task.
Non-Preemptive Kernel
The new higher priority task gains the control of the
CPU only when the current task gives up the CPU.
Low Priority Task

ISR

High Priority Task


Preemptive Kernel
Use when system responsiveness is important,
hence C/OS-II and all commercial kernels are
preemptive.
The higher priority task ready to run, the current
task is preempted/suspended and higher priority
task is immediately given to control of the CPU.
If an ISR makes a higher priority task ready, when
the ISR completes, the interrupt task suspended
and the new higher priority task is resumed.
Preemptive Kernel
Low Priority Task

ISR

High Priority Task


Round-Robin Scheduling
This is a time slice execution process for the
equal priority tasks.
Each time run for a predetermined time, called
quantum.
The kernel gives control to the next task in line of
The current task has no work to do during its time
slice.
The task completes before the end of its time slice.
The time slice end.
Round-Robin Scheduling
C/OS-II does not support round-robin
scheduling.
Each task must have a unique priority in
application.
Task Priorities
Each task has its own priority.
Higher priority for important task and vise
versa.
Static Priority
The priority of task does not change during the
application execution.
A fixed priority is assign to each task at compile
time.
Timing constraints of all task are known.
Task Priorities
Dynamic Priority
Task priority are not fixed.
During runtime priority of a task can change.
In a real time kernel this feature is disable to avoid
priority inversion.
C/OS-II provides this feature.
Priority Inversion
The priority of a task virtually reduced.
This problem occur in real time kernel.

1 2 3 4 5 6 7
Task 1 (H)

Task 2 (M)

Task 3 (L)
Priority Inversion
1. Task 1 and Task 2 are both waiting for an
event to occur and Task 3 is executing.
2. Task 3 acquires semaphore.
-- Semaphore needs before it can access resources.
3. An event of Task 1 is occurs, thus kernel
suspends Task 3and start execution of Task-1.
4. Task-1 want to access same resource.
1 2 3 4 5 6 7
Priority Inversion
-- Since the resource acquired by Task-3,
Task-1 is placed in a list of waiting task.
-- Task-3 resumes and continues execution.
5. Task-2 suspend Task-3 since its priority is
high than Task-3.
6. After execution of Task-2, kernel hand over
the control of CPU back to Task-3.
1 2 3 4 5 6 7
Priority Inversion
-- Task-3 finishes the execution with
resource and then releases the semaphore.
7. At this point Task-1 has the Semaphore
and can access the shared resource and
completes its execution.
The priority of Task-1 has virtually reduces
to that of Task-3.
1 2 3 4 5 6 7
Assigning Task Priorities
According to the importance of the task,
priorities are assign to them.
Some task are critical task and some task are
non critical.
High priority can assign to critical task and
low priority can assign to non-critical task.
Assigning Task Priorities
Rate Monotonic Scheduling is used to assign
task priorities based on how often task execute.
RMS assign high priority according to the rate
of execution of the task.
Semaphores
Semaphore is a key that the code acquires in order
to continue execution.
If the semaphore is already in use, the requesting
task is suspended until the semaphore is release
by its owner.
Semaphore is used to
Control access to a shared resource.
Signal the occurrence of an event.
Allow two tasks to synchronize their activities.
Semaphores
Two types of semaphore:
Binary semaphore
Take only two values 0 and 1

Counting semaphore
Allows value between 0 and 255 for the 8-bit
semaphore mechanism.
Allows value between 0 and 65535 for 16-bit
semaphore mechanism.
Semaphores
Three operations on semaphore:
Initialize
The semaphore get initial value when semaphore is
initialized.
Wait
A task desiring the semaphore performs wait operation.
If the semaphore is available, the semaphore value is
decremented by one and the task continues execution.
If the semaphore value is zero, the task performing a
wait.
Semaphores
Signal
A task releases a semaphore by performing a signal
operation.
If no task waiting for semaphore, the semaphore value is
simply incremented.
If any task waiting for semaphore and ready to run, then
the key is given to the task.
Mutual Exclusion
Mutual exclusion means, each task should gain
special access to the shared resource to prevent
data corruption.
The most common method to obtain special
access of shared resources are:
Disabling interrupts
Test and set
Disabling scheduling
Using Semaphores
Mutual Exclusion
Mutual Exclusion using disabling and enabling
interrupts
This the easiest and fastest way to gain exclusive access to
a shared resource.

The interrupt should not disable for long time, since it


affect the response of the system.
This is called interrupt latency.
This method is useful only for changing or copying few
variables.
Mutual Exclusion
Mutual Exclusion using Test and Set (TAS)
To get the access of the resource, function must
check the global variable is equal to 0.
If it is equal to 0, function has access to the
resource.
To disable the access of the resource, the value of
the global variable is set to 1.
This is called Test and Set (TAS) operation.
During TAS operation the interrupts must disable.
Mutual Exclusion
Mutual Exclusion using Test and Set (TAS)
Mutual Exclusion
Mutual Exclusion using disabling and enabling
scheduler:
In this method scheduling can disable by locking
scheduler.
In this case two or more task can share data without
the possibility of contention.
No need to disable interrupts.
If an interrupt occurs in critical section, ISR will
immediately executed.
At the end of ISR, the kernel always return to the
interrupt task even if higher priority task has been
made ready to run by ISR.
Mutual Exclusion
Mutual Exclusion using disabling and enabling
scheduler:
The scheduler should not lock, because it defeat
the purpose of having a kernel in the first place.
Deadlocks
A situation in which two task are unknowingly
waiting for resources held by each other.

Example:
Process-1 requests the printer, gets it
Process-2 requests the tape unit, gets it Process-1 and
Process-2 are
Process-1 requests the tape unit, waits
deadlocked
Process-2 requests the printer, waits
Deadlocks
Simplest way to avoid deadlock is for the task
to:
Acquire all resources before proceeding.
Acquire the resources in the same order.
Release the resources in the reverse order.

Deadlocks generally occur in large


multitasking system and not in embedded
system.
Clock Tick
A special interrupt occur periodically.
The time period between two interrupts is application
specific.
The clock tick interrupt allows the kernel to delay task
for an integral number of clock ticks.
These interrupts provides timeouts when tasks are
waiting for an event to occur.
All kernel allows task to be delayed for a certain
number of clock ticks.
The resolution of delayed task is 1 clock tick, however,
this is not mean that its accuracy is 1 clock tick.
Delaying a task for 1 tick

Fig. shows a situation where higher priority task and


ISRs executes prior to the task.
The task attempts to delay for 20mS but because of its
priority, actually executes at varying intervals.
This will thus cause the execution of the task to jitter.
Delaying a task for 1 tick

Fig. shows all higher priority and ISR tasks are slightly
less than one tick.
The task delays itself just before a clock tick.
The task will execute again almost immediately.
If designer need to delay a task for at least 1 clock tick,
designer must specify one extra tick.
Delaying a task for 1 tick

Fig. shows all higher priority and ISR tasks are extend
beyond one clock tick.
The task that tries to delay for 1 tick will actually execute
2 tick later.
In this case task missed its deadline. This might not be
acceptable in all cases
Memory requirement
For foreground/Background system, the amount
of memory required is depends on the application
software.
A multitasking kernel requires extra code space.
The space is depends on the features provided by
kernel and the n-bit CPU.
Features:
Scheduling, Text switching, Semaphore management,
delays and timeout etc.
The total code space is thus given by:
Application Code Size + Kernel Code Size
Memory requirement
If each task runs independently, then each task
needs its own stack area.
The stack size depends on
Task and ISR requirements
Local variable, function call, etc.
Maximum interrupt nesting: saved registers, local
storage in ISR.
[Application code requirements + Data space (i.e.
RAM) needed by the kernel + SUM(task stacks +
MAX(ISR nesting))]
Memory requirement
Some kernel supports a separate stack for
interrupts, then total RAM required is given
by:
[Application code requirements + Data space needed
by the kernel (i.e. RAM) + SUM(task stacks) +
MAX(ISR nesting)]
Advantages of Real-Time Kernels
RTOS, allows real-time applications to be easily
designed and expanded.
Functions can be added without requiring major
changes to the software.
RTOS simplifies the design process by splitting
the application code into separate tasks.
With a preemptive RTOS, all time-critical events
are handled as quickly and as efficiently as
possible.
RTOS allow you to make better use of your
resources by providing you with precious services
such as semaphores, mailboxes, queues, time
delays, timeouts, etc.
Disadvantages of Real-Time Kernels
Using a real-time kernel application, can afford
the extra requirements: extra cost of the kernel,
more ROM/RAM, and 2 to 4 percent additional
CPU overhead.
The cost of an RTOS varies from $50 to well over
$30,000.
The RTOS vendor may also require royalties on a
per-target-system basis.
This is like buying a chip from the RTOS vendor
that you include with each unit sold.
The RTOS vendors call this silicon software.
Assignment - 2
Q.1. Explain the following.
i. Different states of task.
ii. Context switching.

Q.2. Explain Mutual Exclusion.

Q.3. What is Deadlock? When it occurs?


References
Jean J. Labrosse, MicroC/OS-II, The Real-
Time Kernel, 2nd edition, CMP Books.

S-ar putea să vă placă și