Task
A task is an independent thread of execution that
can compete with other concurrent tasks for
processor execution time.
A task is schedulable.
A task is defined by its distinct set of parameters
and supporting data structures.
Specifically, upon creation, each task has an
associated name
a unique ID
a priority (if part of a pre-emptive scheduling plan)
a task control block (TCB)
a stack and
a task routine
Together these components are known as task
objects
When the kernel first starts, it creates its own set of
system tasks and allocates the appropriate priority for
each from a set of reserved priority levels.
initialization or startup task initializes the
system and creates and starts system tasks,
idle task uses up processor idle cycles when
no other activity is present,
logging task logs system messages,
exception-handling task handles exceptions,
and
debug agent task allows debugging with a
host debugger.
Task States
ready state-the task is ready to run but cannot because a higher priority task is
executing.·
blocked state-the task has requested a resource that is not available, has requested
to wait until some event occurs, or has delayed itself for some duration.
running state-the task is the highest priority task and is running.
Some other states are
Suspended, Pended, and Delayed.
Pended and delayed are actually sub-states of
the blocked state.
A pended task is waiting for a resource that it
needs to be freed; a delayed task is waiting
for a timing delay to end.
The suspended state exists for debugging
purposes.
SEMAPHORES
Multiple concurrent threads of execution
within an application must be able to
Synchronize their execution
Coordinate mutually exclusive access to
shared resources
RTOS provides a semaphore object and
associated semaphore management services
Semaphore Definition
A kernel object that one or more threads of
execution can acquire or release for the
purpose of synchronization or mutual
exclusion
When a semaphore is first created, kernel
assign
A semaphore control block (SCB)
A unique ID
A value (binary or a count)
A task-waiting list
A semaphore, associated parameters and
supporting data structures
A semaphore is a key that allows a task to
carry out some operation or to access a
resource
A single semaphore can be acquired a finite
number of times
Acquire a semaphore is like acquiring the
duplicate of a key
If the token counter reach zero
o The semaphore has no token left
o A requesting task cannot acquire the
semaphore and may block if it chooses to
wait
Task-waiting list track of all tasks blocked
while waiting on an unavailable semaphore
* FIFO: first-in-first-out order
* Highest-priority first order
When an unavailable semaphore becomes
available
Kernel allow the first task in the task-
waiting list to acquire it
Semaphore - Types
* Binary semaphores
* Counting semaphores
* Mutual-exclusion (mutex)
semaphores
Binary Semaphore
Have a value either 0 or 1
0 : Semaphore is considered unavailable
1 : Semaphore is considered available
When a binary semaphore is first created:
# It can be initialized to either availiable
or unavailable (0 or 1 respectively)
When created as global resources
# They are shared among all tasks
# Any task would release a binary semaphore
even if the task did not initially acquire it
State Diagram of a Binary Semaphore
Counting Semaphores
Use a counter to allow it to be acquired or released multiple
times
The semaphore count assigned when it first created
denotes the number of semaphores tokens it has initially
If initial count is 0 the counting semaphore is created in
unavailable state.
If initial count is greater than 0 the counting semaphore is
created in available state.
When all tokens are gone semaphore moves from available
to unavailable state.
If a token is released by any task semaphore is moved to
available state.
Are global resources that can be used by any task that need
them.
The State Diagram of a Counting
Semaphore
Bounded and unbounded counts:
Bounded count:
Count in which initial count set for counting
semaphore, determine when the semaphore
was first created acts as the maximum count
for the semaphore.
Unbounded count:
Allow counting semaphore to count beyond
the initial count to the maximum value that
can be held by the counter’s data type
Mutual Exclusion (Mutex) Semaphores
A special binary semaphore that supports
* Ownership
* Recursive access
* Task deletion safety
* One or more protocols avoiding problems
inherent to mutual exclusions
The states of a mutex
Locked and unlocked
A mutex is initially created in the unlocked state
The State Diagram of a Mutual
Exclusion ( Mutex ) Semaphore Mutex
Mutex Ownership
Ownership of a mutexis gained when a task first locks the mutex
When a task owns the mutex
--> No other task can lock or unlock that
mutex
-- > Contrast to the binary semaphore that can be
released by any task
Implementation
* A lock countkeep track of
--Two states of a mutex
0 for unlocked and 1 for locked
And the number of timesit has been recursively locked
Lock count > 1
Implementation: a mutexmight maintain two counts
A binary value to track its state
A separate lock countto track the number of
times it has been acquired in the lock state
Implementation: a mutexmight maintain two counts
A binary valueto track its state
A separate lock countto track the number of times
it has been acquired in the lock state by the task
owns it
by the task owns it
Initializing Semaphores
The function SemCreate is used to initialize
the semaphore.
Semaphore must be initialized before
attempting to do any operation on it.
Reentrancy and Semaphore
In data sharing, the resources are protected
by semaphores.
The second task that calls the semaphore for
the second time will be blocked.
We can make the function reentrant.
The atomicity of operation of the function
will be protected by the semaphore.
Multiple Semaphore
RTOS allow us to have as many semaphores as we like.
Each call to the RTOS must identify the semaphore on
which to operate.
The semaphores are all independent of one another.
If one task take semaphore A another task can take
semaphore B without blocking.
Similarly if one task is waiting for semaphore
C that task will still be blocked even if some other task
releases semaphore D.