ES Chap 3
ES Chap 3
1. Introduction of RTOS
2. Task and Task States,
3. Semaphore
4. Mutex
5. Mailboxes
6. Message Queue
7. Pipes
8. Signals
9. Timers
10.Memory Management
11.Priority Inversion Problem
If a higher priority task unblocks, is the current running task moved to the ready state right
away?
This will happen only if the RTOS is preemptive.
These are the common operations that an RTOS task can perform with a mutex:
• Create\Delete a mutex
• Get Ownership (acquire a lock on a shared resource)
• Release Ownership (release a lock on a shared resource)
RTOS Semaphores
• Consider two functions for dealing with the RTOS binary semaphores – TakeSemaphore and
ReleaseSemaphore.
• If a task has called TakeSemaphore to take the semaphore, any other task calling it will block
until the semaphore is released (ReleaseSemaphore).
By: Vikram Kulkarni
Semaphores and Shared Data(Cont.)
• With this new setup consider the scenario where the ‘levels task’ has just taken
the semaphore and is pre-empted by the higher priority ‘push button’ task.
• The RTOS will move the higher priority task to the running state.
• When this task tries to lock the semaphore, it will block.
• The OS will then run the task which has the semaphore (levels task) until it releases it.
• As soon as this happens, the RTOS will switch back to the higher priority task which is now
unblocked.
Multiple Semaphores
• RTOSs normally allow the users to have multiple semaphores that are distinctly identified by a
parameter and are independent of each other.
• This speeds task responses – a high priority task does not have to be blocked by a lower priority
one as long as it is using a different semaphore.
• It is the users responsibility to remember which semaphore protects which shared data – the OS
will not do that.
By: Vikram Kulkarni
Semaphores and Shared Data(Cont.)
Semaphores Problems
• Forgetting to take it – Semaphores only work if tasks actually remember to use them while
accessing shared data.
• Forgetting to release the semaphore – This will cause all tasks using the semaphore to be
eventually blocked forever.
• Holding it for too long – Higher priority tasks might loose their deadlines if some lower priority
task holds the semaphore for too long.
A problem that can happen is if a low priority task C has a semaphore and a higher priority task
B that does not use the semaphore pre-empts it in between.
Now suppose the highest priority task A comes along and is blocked on the semaphore. As B
has a higher priority than C it will run instead and might block A for long enough that it misses
its deadline.
This is called priority inversion. Some RTOSs temporarily give task A’s priority to C (and hence
prevent B from pre-empting C).
Important Points
• Most RTOS require queues to be initialized before being used. Some OSs also require the user to allocate
memory for them.
• If a task tries to write to a queue that is full, the RTOS generally returns an error, or in some cases it might
block the task until another task reads the queue. The code should deal with these errors.
• RTOSs include functions that read from queues, if they have data, and return an error if they don’t.
• Many RTOSs only allow a fixed amount of data that can be written in one call.
• A common method is to allow the user to write the number of bytes taken up by a void pointer in one
function call.
Mailboxes
• Mailboxes are similar to queues. They have variations in different RTOS as follows :-
• Most RTOS allow only one message in a mailbox though some might allow more.
• Some OS have unlimited message length mailboxes. The total messages in all the mailboxes however is limited and
this is distributed among individual mailboxes.
• Some RTOS allow priorities in mailboxes – higher priority mailboxes will be read first
Which to Use?
• Since queues, mailboxes and pipes vary in each RTOS, the user should read the corresponding documentation
and determine which would best suit his communication requirement.
Pitfalls
• Although the above 3 mechanisms simplify sharing data they can also introduce several bugs.
• User should ensure that tasks write to the correct mailboxes otherwise there might be errors.
• Most RTOSs have a delay function that delays or blocks a task for a certain time period.
• e.g. in the U.S. each of the tones representing a digit in a phone call must sound for 1/10th of a second followed by the same period of
silence between tones.
This can be done by utilizing the function taskDelay(100) that delays the task for 100ms
Questions
• How do I know that taskDelay takes milliseconds as its parameter?
You don’t. In VxWorks for e.g. taskDelay takes system ticks as the parameter
• How accurate are these delays?
They are accurate to the nearest tick. The RTOS sets up a hardware timer often called a heartbeat timer to
periodically interrupt and bases its timings on the interrupt.
• The buffer size returned depends on the pool. relbuf frees a buffer
• MultiTask! like typical RTOSs needs to be told where the memory is (init_mem_pool).
int init_mem_pool (
unsigned int PoolId,
void *p_vMemory,
unsigned int BufSize,
unsigned int BufCount,
unsigned int PoolType);
By: Vikram Kulkarni
Memory Management (contd.)
• PoolId – identifier
• p_vMemory – points to the pool memory block
• BufSize and BufCount – indicate size and count of buffer
• PoolType – indicates if buffers will be used by tasks or by interrupt
routines.
• Priority Ceiling All of the resources are assigned a priority that is equal to the highest priority of any
task that may attempt to claim them. This helps in avoiding priority inversion.
• Disabling Interrupts There are only two priorities in this case i.e. interrupts disabled and
preemptible. So priority inversion is impossible as there is no third option.
• Priority Inheritance This solution temporarily elevates the priority of the low priority task that is
executing to the highest priority task that needs the resource. This means that medium priority tasks
cannot intervene and lead to priority inversion.
• No blocking Priority inversion can be avoided by avoiding blocking as the low priority task blocks the
high priority task.
• Random boosting The priority of the ready tasks can be randomly boosted until they exit the critical
section
By: Vikram Kulkarni
Thanks