Consider the following FreeRTOS code snippet. What will be the value of mutexCreated after execution?
SemaphoreHandle_t mutex;
BaseType_t mutexCreated;
void setup() {
mutex = xSemaphoreCreateMutex();
if (mutex != NULL) {
mutexCreated = pdTRUE;
} else {
mutexCreated = pdFALSE;
}
}Remember that xSemaphoreCreateMutex() returns a handle or NULL on failure.
The function xSemaphoreCreateMutex() returns a handle to the mutex if successful, otherwise NULL. Checking if the handle is not NULL confirms creation success.
Choose the best description of what xSemaphoreCreateMutex() does in FreeRTOS.
Think about what a mutex is used for in multitasking systems.
xSemaphoreCreateMutex() creates a mutex semaphore, which is a special type of binary semaphore used to ensure only one task accesses a resource at a time.
What error will this code produce when compiled or run?
SemaphoreHandle_t mutex;
void init() {
xSemaphoreCreateMutex();
}
void task() {
if (xSemaphoreTake(mutex, portMAX_DELAY) == pdTRUE) {
// critical section
xSemaphoreGive(mutex);
}
}Check if the mutex handle is properly initialized before use.
The code calls xSemaphoreCreateMutex() but does not assign its return value to mutex. This leaves mutex uninitialized, causing xSemaphoreTake() to operate on an invalid handle.
Choose the code snippet that correctly creates a mutex and verifies it was created successfully.
Remember the return type and how to check for success.
xSemaphoreCreateMutex() returns a handle or NULL. The correct check is mutex != NULL for success.
In FreeRTOS, a mutex created by xSemaphoreCreateMutex() is implemented internally as a queue. How many items does this queue hold?
Think about how a mutex represents a single resource that can be taken or given.
A mutex in FreeRTOS is implemented as a queue with a length of one, holding a single token that indicates if the mutex is available or taken.
