mutex1 and mutex2Task1 that tries to take mutex1 then mutex2Task2 that tries to take mutex1 then mutex2 (same order as Task1)xSemaphoreTake and xSemaphoreGive to manage mutexesmutex1 and mutex2Task1 that tries to take mutex1 then mutex2Task2 that tries to take mutex1 then mutex2 (same order as Task1)xSemaphoreTake and xSemaphoreGive to manage mutexesmutex1 and mutex2 using xSemaphoreCreateMutex().Use xSemaphoreCreateMutex() to create each mutex and assign it to mutex1 and mutex2.
Task1 that tries to take mutex1 first, then mutex2. Use xSemaphoreTake with portMAX_DELAY to wait indefinitely. After taking both mutexes, give them back in reverse order using xSemaphoreGive. Use printf to print messages when each mutex is taken and given.Inside Task1, take mutex1 first, then mutex2. Print messages after each take and give. Finally, delete the task.
Task2 that also takes mutex1 first, then mutex2, following the same pattern as Task1. Use printf to show when mutexes are taken and given. Create Task2 in main() with the same priority as Task1.Make sure Task2 takes mutex1 then mutex2 in the same order as Task1. Print messages and delete the task at the end.
printf statements in main() before starting the scheduler to print "Starting scheduler". Run the program and observe the output showing that both tasks take and give mutexes without deadlock because they acquire mutexes in the same order.Print "Starting scheduler" before calling vTaskStartScheduler(). Run the program and check that both tasks take and give mutexes without deadlock.
What is a common method to prevent deadlocks in FreeRTOS when multiple tasks need several mutexes?
Choose the best practice.
Which of the following is the correct way to create a mutex in FreeRTOS to help prevent deadlocks?
?Consider this FreeRTOS code snippet where two tasks try to take two mutexes in different orders:
Task1:
xSemaphoreTake(mutexA, portMAX_DELAY);
xSemaphoreTake(mutexB, portMAX_DELAY);
Task2:
xSemaphoreTake(mutexB, portMAX_DELAY);
xSemaphoreTake(mutexA, portMAX_DELAY);What is the likely outcome?
Given this FreeRTOS code snippet, identify the problem that can cause deadlock:
void Task1(void *pvParameters) {
xSemaphoreTake(mutex1, portMAX_DELAY);
vTaskDelay(10);
xSemaphoreTake(mutex2, portMAX_DELAY);
// ...
xSemaphoreGive(mutex2);
xSemaphoreGive(mutex1);
}
void Task2(void *pvParameters) {
xSemaphoreTake(mutex2, portMAX_DELAY);
vTaskDelay(10);
xSemaphoreTake(mutex1, portMAX_DELAY);
// ...
xSemaphoreGive(mutex1);
xSemaphoreGive(mutex2);
}What is the best fix to prevent deadlock?
You have three tasks that need to access three shared resources protected by mutexes: mutexX, mutexY, and mutexZ. To prevent deadlocks, which strategy is best?
mutexX and mutexYmutexY and mutexZmutexX and mutexZChoose the best deadlock prevention pattern.
Pass the test and this pattern moves unknown