0
0
FreeRTOSprogramming~20 mins

Task starvation and priority inversion in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Priority Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this FreeRTOS task priority example?

Consider three tasks with priorities: High (3), Medium (2), and Low (1). The Low priority task holds a mutex needed by the High priority task. The Medium priority task runs continuously without blocking. What happens to the High priority task?

FreeRTOS
/* Pseudocode for FreeRTOS tasks */
void LowPriorityTask(void *pvParameters) {
    xSemaphoreTake(mutex, portMAX_DELAY);
    // Simulate long processing
    vTaskDelay(pdMS_TO_TICKS(1000));
    xSemaphoreGive(mutex);
    vTaskDelete(NULL);
}

void MediumPriorityTask(void *pvParameters) {
    while(1) {
        // Runs continuously without blocking
    }
}

void HighPriorityTask(void *pvParameters) {
    xSemaphoreTake(mutex, portMAX_DELAY);
    // Critical section
    xSemaphoreGive(mutex);
    vTaskDelete(NULL);
}
AMedium priority task is preempted by High priority task and Low priority task runs last.
BHigh priority task runs immediately after Low priority task releases the mutex.
CHigh priority task is blocked indefinitely due to priority inversion caused by Medium priority task starving Low priority task.
DLow priority task preempts Medium and High priority tasks causing deadlock.
Attempts:
2 left
💡 Hint

Think about how Medium priority task running continuously affects Low priority task holding the mutex.

🧠 Conceptual
intermediate
1:30remaining
Which mechanism helps prevent priority inversion in FreeRTOS?

FreeRTOS provides a feature to avoid priority inversion when a low priority task holds a resource needed by a higher priority task. What is this feature called?

ATime slicing
BRound-robin scheduling
CPreemptive scheduling
DPriority inheritance
Attempts:
2 left
💡 Hint

It temporarily raises the priority of the task holding the resource.

🔧 Debug
advanced
2:30remaining
Identify the cause of task starvation in this FreeRTOS code snippet

Given the following FreeRTOS task setup, why does the Low priority task never get CPU time?

FreeRTOS
xTaskCreate(HighPriorityTask, "High", 1000, NULL, 3, NULL);
xTaskCreate(MediumPriorityTask, "Medium", 1000, NULL, 2, NULL);
xTaskCreate(LowPriorityTask, "Low", 1000, NULL, 1, NULL);

void MediumPriorityTask(void *pvParameters) {
    while(1) {
        // Busy loop without any delay or blocking
    }
}

void LowPriorityTask(void *pvParameters) {
    while(1) {
        // Should run but never does
        vTaskDelay(pdMS_TO_TICKS(100));
    }
}
AMedium priority task never yields CPU because it has no blocking or delay, starving Low priority task.
BLow priority task has too small stack size causing it to crash silently.
CHigh priority task preempts both Medium and Low tasks causing starvation.
DMutex held by Low priority task blocks Medium priority task.
Attempts:
2 left
💡 Hint

Look at the Medium priority task's loop behavior.

Predict Output
advanced
1:30remaining
What is the output of this FreeRTOS priority inheritance example?

Consider a Low priority task holding a mutex and a High priority task waiting for it. Priority inheritance is enabled. What priority does the Low priority task temporarily get?

FreeRTOS
/* Assume priorities: Low=1, Medium=2, High=3 */
// Low priority task takes mutex
xSemaphoreTake(mutex, portMAX_DELAY);
// High priority task tries to take mutex and blocks
xSemaphoreTake(mutex, portMAX_DELAY);
// Priority inheritance raises Low priority task's priority
// What is Low priority task's temporary priority?
A2 (same as Medium priority task)
B3 (same as High priority task)
C1 (original Low priority)
D0 (idle priority)
Attempts:
2 left
💡 Hint

Priority inheritance raises the priority to the highest waiting task's priority.

🚀 Application
expert
3:00remaining
How to fix priority inversion in a FreeRTOS system with three tasks?

You have three tasks: High, Medium, and Low priority. The Low priority task holds a mutex needed by the High priority task. The Medium priority task runs continuously and starves the Low priority task. Which approach fixes the priority inversion and starvation?

AEnable priority inheritance on the mutex and add blocking calls in Medium priority task to allow Low priority task to run.
BIncrease Low priority task priority to be higher than Medium priority task permanently.
CRemove the mutex and use global variables without protection.
DMake High priority task yield to Medium priority task manually.
Attempts:
2 left
💡 Hint

Think about both priority inversion and starvation causes.