Complete the code to create a mutex in FreeRTOS.
SemaphoreHandle_t xMutex = xSemaphoreCreate[1]();In FreeRTOS, xSemaphoreCreateMutex() creates a mutex for resource locking.
Complete the code to take a mutex safely with a timeout.
if(xSemaphoreTake(xMutex, [1]) == pdTRUE) { /* critical section */ }
Using portMAX_DELAY waits indefinitely to take the mutex, preventing premature failure.
Fix the error in the code to prevent deadlock by releasing the mutex.
xSemaphoreTake(xMutex, portMAX_DELAY);
/* critical section */
[1](xMutex);After the critical section, xSemaphoreGive() releases the mutex to avoid deadlock.
Fill both blanks to create a deadlock prevention pattern by acquiring mutexes in order.
if(xSemaphoreTake(xMutex1, portMAX_DELAY) == pdTRUE) { if(xSemaphoreTake([1], portMAX_DELAY) == pdTRUE) { /* critical section */ [2](xMutex2); } xSemaphoreGive(xMutex1); }
Acquire xMutex2 after xMutex1 to keep order, then release xMutex2 to prevent deadlock.
Fill all three blanks to implement priority inheritance to prevent priority inversion.
vTaskPrioritySet([1], [2]); /* critical section protected by mutex */ vTaskPrioritySet([3], originalPriority);
Raise the priority of the task holding the mutex to higherPriority to prevent inversion, then restore it.
