Unit V
Contents
Running
Running means that the
microprocessor is executing the
instructions that make up this task.
Unless yours is a multiprocessor
system, there is only one
microprocessor, and hence only one
task that is in the running state at
any given time.
Ready
Ready means that some other task
is in the running state but that this
task has things that it could do if the
microprocessor becomes available.
Any number of tasks can be in this
state.
Blocked
Blocked means that this task
hasnt got anything to do right now,
even if the microprocessor becomes
available. Tasks get into this state
because they are waiting for some
external event. Any number of tasks
can be in this state as well.
RMA
In RMA, the priority is proportional to
the frequency of execution.
If (i)th task has an execution period of
Ti, and Ei is its execution time, then
(Ei/Ti) gives the percentage of the CPU
time required for execution of the (i)th
task
In RMA, the scheduling test
indicates how much CPU time is
actually utilized by the tasks.
Create a task
Delete a task
Suspend a task
Resume a task
Change priority of a task
Query a task
ISR
Interrupts cause the microprocessor in the
embedded system to suspend doing whatever
it is doing and to execute some different code
instead, code that will respond to whatever
event caused the interrupt.
Interrupt service routine is also called as
interrupt handler or sometimes interrupt
routine.
ISR acts like a sub-routine that is called
whenever the hardware asserts the interrupt
request signal.
Continued..
Disabling the interrupts
Non maskable interrupts
Non maskable interrupts cannot be used for
shared data
Because of this, the non maskable interrupts
are most commonly used for event that are
completely beyond the normal range of the
ordinary processing.
For example, to allow system to react to a
power failure etc.
Continued..
What happens, if two interrupts
happen at the same time?
Can an interrupt request signal
interrupt another interrupt routine?
Continued..
Interrupt Latency time : The max time
interrupts are disabled + time to start the
execution of the first instruction in the ISR
Interrupt Response time: Time between the
receipt of the interrupt signal and starting
the code that handles the interrupt
Interrupt Recovery time: Time required for
the CPU to return to the interrupted code/
highest priority task
Interrupt
Recovery
time
Interrupt
request
Interrupt
latency
Interrupt
response time
In preemptive kernel
Response time = interrupt latency +
time to save CPU registers context.
Recovery time = time to check whether
highest priority task is ready + time to
restore CPU context of the highest
priority task + time to execute the
return instruction
In non-preemptive kernel, Recovery
time = time to restore the CPU contents
+ time to execute the return instruction
Semaphores
Semaphore is a h/w or a s/w tag variable or a
tool whose value indicates the status of a
common source.
Its purpose is to lock the resource being used.
A process which needs the resource will check
the semaphore for determining the status of
the resource followed by the decision or
proceeding.
In multitasking OS, the activities are
synchronized using the semaphore techniques.
Continued..
Semaphore is a kernel object that is
used for both resource
synchronization and task
synchronization.
Resource synchronization mutual
exclusion
Task synchronization serialization
Resource synchronization
Display
Task 1
Task 2
Task synchronization
Voice
single
A
D
C
Task to read
the Data
Task to write
the Data
Memory
D
A
C
Display Semaphore
Display
Semaphore
3
2
Task 1
Task 2
Types of semaphores
Binary semaphore
in which tasks can call two RTOS
functions, TakeSemaphore and
ReleaseSemaphore,
Counting
Counting semaphore
Pool of buffers
Buffer 1
Task1
Buffer 10
Task n
INITIALIZE
WAIT
PEND), and
SIGNAL
Semaphore management
function calls
Create a semaphore
Delete a semaphore
Acquire a semaphore
Release a semaphore
Query a semaphore
Multiple Semaphores
What is the advantages of having multiple
semaphores instead of a single one that protects
all ?
Semaphores as a Signaling
Device
Semaphores can also be used as a simple way
of communication between tasks, or between
an interrupt routine and its associated task.
Semaphore Problems
Potential problems
Semaphore Variants
Counting semaphores semaphores that can
be taken multiple times.
Resource semaphores useful for the shareddata problem.
Mutex semaphore automatically deal with
the priority inversion problem.
If several tasks are waiting for a semaphore
when it is released, different RTOS may vary in
the decision as to which task gets to run.
Task that has been waiting longest.
Highest-priority task that is waiting for the
semaphore
Deadlock
A deadlock, also called a deadly
embrace, is a situation in which two
tasks are each unknowingly waiting for
resources held by the other.
Assume task T1 has exclusive access to
resource R1 and task T2 has exclusive
access to resource R2. If T1 needs
exclusive access to R2 and T2 needs
exclusive access to R1, neither task can
continue. They are deadlocked.
Deadlock (cont)
The simplest way to avoid a deadlock is
for the tasks to
Acquire all resources before proceeding,
Acquire the resources in the same order, and
Release the resources in the reverse order.
Most RTOS allow you to specify a timeout when
acquiring a semaphore. This features allows a
deadlock to be broken.
Mutexes
Mutex stands for mutual exclusion
It is used for resource synchronization and
task synchronization
It can be achieved through the following
mechanisms
1. Disabling the interrupts
2. Disabling the scheduler
3. By test-and-set operations
4. Semaphores
Mutex management
function calls
Create a mutex
Delete a mutex
Acquire a mutex
Release a mutex
Query a mutex
Wait on mutex
Continued..
1. It will be an owner.
Disadvantages.
Affect the response times of all the interrupt
routines.
Disadvantages.
Take up microprocessor time.
Will not work for interrupt routine.
Summary
Task is nothing but an event or a
process running on an operating
system.
Scheduler is the heart of the kernel.
Rate monotonic analysis will be a good
start for designing a real time system.
Shared data problems can be solved
by semaphores and mutexes.