Assignment

1719 Words4 Pages

Question:
In your own words, describe how binary semaphores and mutexes are implemented in FreeRTOS. (Do not include counting semaphores.) In the discussion go to the level of the FreeRTOS code relevant to semaphores (i.e. your description should include code snippets from the FreeRTOS semaphore code). Be sure to cover all relevant aspects of priority-related issues and how the code supports the context switching mechanism (without going into the code-level details of how the context switching mechanism is implemented). Include a section that relates your FreeRTOS description to content from SYSC 5701 lectures.

Solution:
Semaphore is a Greek word where sema means “sign or signal” and phore means “carrier” and is said to be used as a protected variable. There are basically four types of semaphores:
1. Binary Semaphores
2. Counting Semaphores
3. Mutex
4. Recursive Mutex

In FreeRTOS, the concept of semaphore has different meaning based on the context and also its level of usage. FreeRTOS implements semaphores as to synchronize the set of tasks with the other events in system and they are implemented based on the queue mechanism.
A macro that is being defined is responsible for creating a semaphore using the standing queuing mechanism. This queue length can be either 0 or 1 based on the type and context of the semaphore being used.
The concept of Binary Semaphore and Mutex are considered to be the two different levels of implementation of the Semaphores or may be considered as two different use cases viz. Mutual Exclusion and Signaling. There, the confusion still exists on the usage and the terminology of the Mutex and Semaphores. As said, the semaphore can take different meaning based on the context level and ...

... middle of paper ...

... vSemaphoreCreateBinary( xSemaphore );

if( xSemaphore != NULL )
{
if( xSemaphoreGive( xSemaphore ) != pdTRUE )
{
// We would expect this call to fail because we cannot give
// a semaphore without first "taking" it!
}

// Obtain the semaphore - don't block if the semaphore is not
// immediately available. if( xSemaphoreTake( xSemaphore, ( portTickType ) 0 ) )
{
// We now have the semaphore and can access the shared resource.

// ...

// We have finished accessing the shared resource so can free the semaphore. if( xSemaphoreGive( xSemaphore ) != pdTRUE )
{
// We would not expect this call to fail because we must have
// obtained the semaphore to get here.
}
}
}
}

More about Assignment

Open Document