UNIT-II RTOS Embedded Systems
UNIT-II RTOS Embedded Systems
(Or)
“The ability of the operating system to provide a required level of service in a bounded
response time.”
In real life situations like controlling traffic signal or a nuclear reactor or an aircraft.
Arealtimesystemisasystem/thatensurestheexacttime
requirementsforajob.
Real Time Operating Systems
Real Time Operating Systems
Soft Real Time Systems
In a soft real-time system, tasks are completed as fast as possible without having to
be completed within a specified timeframe.
In a soft real-time system, it is considered undesirable, but not catastrophic, if deadlines
are occasionally missed.
Also known as “best effort” systems.
Most modern operating systems can serve as the base for a soft real time systems.
One in which deadlines are mostly met.
Soft real time means that only the precedence and sequence for the task operations are
defined, interrupt latencies and context switching latencies are small but there can be
few deviations between expected latencies of the tasks and observed time constraints and
a few deadline misses are accepted.
Soft Real Time Systems
A task (also called a thread) is a program on a computer which can be executed and run.
A task is an independent thread of execution that can compete with other concurrent tasks
for processor execution time.
A task is schedulable.
The design process for a real-time application involves splitting the work to be done into
tasks which are responsible for a portion of the problem.
Each task is assigned a priority, its own set of CPU registers, and its own stack area.
Task States
DORMANT
READY
RUNNING
DELAYED
PENDING
BLOCKED
INTERRUPTED
Task States
Task States
DORMANT-The DORMANT state corresponds to a task that resides in memory but has not
been made available to the multitasking kernel.
READY-A task is READY when it can be executed but its priority is less than that of the
task currently being run.
In this state, the task actively competes with all other ready tasks for the processor’s
execution time.
The kernel’s scheduler uses the priority of each task to determine which task to move to the
running state.
Task States
RUNNING-A task is RUNNING when it has control of the CPU and it's currently being
executed.
On a single-processor system, only one task can run at a time.
•When a task is preempted by a higher priority task, it moves to the ready state.
•It also can move to the blocked state.
–Making a call that requests an unavailable resource
–Making a call that requests to wait for an event to occur
–Making a call to delay the task for some duration
Task States
WAITING-A task is WAITING when it requires the occurrence of an event (waiting for
an I/O operation to complete, a shared resource to be available, a timing pulse to occur,
time to expire, etc.).
BLOCKEDCPU starvation occurs when higher priority tasks use all of the CPU
execution time and lower priority tasks do not get to run.
The cases when blocking conditions are met.
A semaphore token for which a task is waiting is released.
A message, on which the task is waiting, arrives in a message queue.
A time delay imposed on the task expires.
ISR(Interrupt Service Routine)-A task is in ISR state when an interrupt has occurred and
the CPU is in the process of servicing the interrupt.
Task States
Scheduling
Scheduling Cont…
Task Scheduling
Schedulers are parts of the kernel responsible for determining which task runs next
Most real-time kernels use priority-based scheduling
• Each task is assigned a priority based on its importance
• The priority is application-specific
The scheduling can be handled automatically.
Many kernels also provide a set of API calls that allows developers to control the state changes.
Manual scheduling
Non Real -time systems usually use Non-preemptive Scheduling
• Once a task starts executing, it completes its full execution
Most RTOS perform priority-based preemptive task scheduling.
Basic rules for priority based preemptive task scheduling
• The Highest Priority Task that is Ready to Run, will be the Task that Must be Running.
Why Scheduling is necessary for RTOS?
More information about the tasks are known
• Number of tasks.
• Resource Requirements.
• Execution time.
• Deadlines.
Being a more deterministic system better scheduling algorithms can be devised.
Scheduling Algorithms
Depending upon the requirement of ES, the scheduling algorithm needs to be choosen.
• First-In First-Out
• Round-Robin Algorithm
• Round Robin With Priority
• Shortest Job First
• Non-Preemptive Multi-tasking
• Preemptive Multi-tasking
First-In First-Out
Queue
• The task which ready to run are kept in a queue and CPU serves the task on first come first serve
basis.
Round-Robin Algorithm
Task-1 Task-2 Task-3 Task-1 Task-2 Task-3
Time
• The kernel assign a certain amount of time for each task waiting in the Queue.
• The time slice allocated to each task is called Quantum.
Non-Preemptive Multi-Tasking Algorithms
Low-priority task Interrupt Service High-priority task Low-priority task
running Routine running running
Time
ISR makes high priority task High priority task release the
Ready-to-run CPU
This is also called as cooperative multitasking as the tasks have to cooperate with one
another to share CPU time.
Time
ISR makes high priority task Low priority task release the
Ready-to-run CPU
Preemptive Multi-Tasking Algorithms
Time
ISR makes high priority task Low priority task release the
Ready-to-run CPU
The high priority tasks is always executed by the CPU, by preempting the lower priority
task.
All real time OS implement this scheduling algorithm.
Rate Monotonic Analysis
It is used to calculate the percentage of CPU time utilized by the tasks and to assign
priorities to tasks.
Priorities are proportional to the frequency of execution.
There are two types of priority assignments,
• Static priority assignment
• Dynamic priority assignment
Static priority assignment: A task will be assigned a priority at the time of creating the
task and it remains the same.
Dynamic priority assignment: the priority of the task can be changed during the
execution time.
It is not very easy to assign priorities to task, with the help of Rate Monotonic analysis
priorities of the tasks has been assign.
Task Management Function Calls
Create a Task
Delete a Task
Suspend a Task
Resume a Task
Change priority of a task
Query a Task.
Semaphores Cont…
Let us consider, two tasks wants to access a display. Display is a shared resource, in order
to control the access a semaphores is created.
If task 1 wants to access the printer, it acquires the semaphore, uses the printer and then
releases the semaphore.
If both tasks want to access a resource simultaneously, the kernel has to give the semaphore
only to one of the task.
This allocation may be based on the priority of the task or FIFO basis.
If a number of tasks have to access the same resource then the tasks are kept in a queue and
each task can acquire the semaphore one by one.
Resource Synchronization
When multiple tasks are running, two or more tasks may need to share the same resource.
In order to access a shared resource, there should be a mechanism so that there is
discipline.
That mechanism is known as Resource Synchronization.
Display Unit
Task 1 Task 2
Voltage
Signal A D
Task to write the Task to read the
D A
Data data
C C
Memory
Semaphore
1 3
4
2
Task 1 Task 2
1 2 3 4
Task 1 Task 2
Task 1 Uses Task 1 releases Task 2 Uses Task 2 releases
Acquires Acquires
Display Semaphore Display Semaphore
Semaphore Semaphore
Buffer 1 Buffer 10
0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Task Task
Task 1 Task 2