Consider a FreeRTOS system with three tasks: TaskHigh (priority 3), TaskMedium (priority 2), and TaskLow (priority 1). TaskLow holds a mutex that TaskHigh needs. TaskMedium runs continuously. What happens to TaskHigh's effective priority when it tries to take the mutex?
/* Pseudocode illustrating priority inheritance */ void TaskLow(void *params) { xSemaphoreTake(mutex, portMAX_DELAY); // Simulate long processing vTaskDelay(1000); xSemaphoreGive(mutex); vTaskDelete(NULL); } void TaskHigh(void *params) { xSemaphoreTake(mutex, portMAX_DELAY); // Critical section xSemaphoreGive(mutex); vTaskDelete(NULL); } void TaskMedium(void *params) { while(1) { // Runs continuously } } int main() { mutex = xSemaphoreCreateMutex(); xTaskCreate(TaskLow, "Low", 1000, NULL, 1, NULL); xTaskCreate(TaskMedium, "Medium", 1000, NULL, 2, NULL); xTaskCreate(TaskHigh, "High", 1000, NULL, 3, NULL); vTaskStartScheduler(); return 0; }
Think about how priority inheritance prevents priority inversion by temporarily boosting the priority of the mutex holder.
When TaskHigh tries to take the mutex held by TaskLow, TaskLow inherits TaskHigh's higher priority (3). This allows TaskLow to preempt TaskMedium (priority 2) and finish its critical section faster, preventing priority inversion.
Choose the statement that correctly explains how priority inheritance works in FreeRTOS.
Consider which task's priority changes and why during mutex contention.
In FreeRTOS, when a higher priority task waits for a mutex held by a lower priority task, the lower priority task temporarily inherits the higher priority to avoid priority inversion.
Examine the code snippet below. Despite using a mutex, a high priority task is blocked for a long time by a low priority task while a medium priority task runs. Identify the cause.
xSemaphoreHandle mutex;
void TaskLow(void *params) {
xSemaphoreTake(mutex, portMAX_DELAY);
vTaskDelay(5000); // Simulate long work
xSemaphoreGive(mutex);
vTaskDelete(NULL);
}
void TaskHigh(void *params) {
vTaskDelay(100); // Ensure TaskLow takes mutex first
xSemaphoreTake(mutex, portMAX_DELAY);
xSemaphoreGive(mutex);
vTaskDelete(NULL);
}
void TaskMedium(void *params) {
while(1) {
// Busy loop
}
}
int main() {
mutex = xSemaphoreCreateMutex();
xTaskCreate(TaskLow, "Low", 1000, NULL, 1, NULL);
xTaskCreate(TaskMedium, "Medium", 1000, NULL, 2, NULL);
xTaskCreate(TaskHigh, "High", 1000, NULL, 3, NULL);
vTaskStartScheduler();
return 0;
}Check how the mutex was created and if priority inheritance is enabled by default.
FreeRTOS mutexes created with xSemaphoreCreateMutex() enable priority inheritance. If a binary semaphore or a mutex created differently is used, priority inheritance may not occur, causing priority inversion.
Which option contains the syntax error that breaks priority inheritance behavior in this FreeRTOS mutex example?
SemaphoreHandle_t mutex;
void TaskA(void *params) {
xSemaphoreTake(mutex, portMAX_DELAY);
// Critical section
xSemaphoreGive(mutex);
vTaskDelete(NULL);
}
int main() {
mutex = xSemaphoreCreateMutex();
xTaskCreate(TaskA, "TaskA", 1000, NULL, 2, NULL);
vTaskStartScheduler();
return 0;
}Look carefully at the line with xSemaphoreTake and check punctuation.
The missing semicolon after xSemaphoreTake(mutex, portMAX_DELAY) causes a syntax error, preventing compilation and correct mutex behavior including priority inheritance.
In a FreeRTOS system, if multiple higher priority tasks are waiting on a mutex held by a lower priority task, how many of these tasks' priorities does the mutex holder inherit?
Consider how priority inheritance resolves priority inversion with multiple waiting tasks.
FreeRTOS priority inheritance raises the mutex holder's priority to the highest priority of any task waiting for the mutex. It does not combine or switch between multiple priorities.
