Bird
Raised Fist0
FreeRTOSprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does task starvation mean in FreeRTOS?
easy
A. Some tasks never get CPU time because others run too much
B. All tasks get equal CPU time regardless of priority
C. Tasks run in a random order without priority
D. Tasks share CPU time only when a mutex is free

Solution

  1. Step 1: Understand task scheduling in FreeRTOS

    FreeRTOS schedules tasks based on priority, so higher priority tasks run more often.
  2. Step 2: Define task starvation

    Task starvation happens when low priority tasks never get CPU time because higher priority tasks keep running.
  3. Final Answer:

    Some tasks never get CPU time because others run too much -> Option A
  4. Quick Check:

    Task starvation = some tasks starve for CPU [OK]
Hint: Starvation means some tasks get no CPU time [OK]
Common Mistakes:
  • Confusing starvation with equal CPU sharing
  • Thinking tasks run randomly
  • Assuming mutex controls CPU time directly
2. Which FreeRTOS API is used to create a mutex that supports priority inheritance?
easy
A. xQueueCreate()
B. xSemaphoreCreateMutex()
C. vTaskDelay()
D. xTaskCreate()

Solution

  1. Step 1: Identify mutex creation functions

    FreeRTOS uses xSemaphoreCreateMutex() to create mutexes with priority inheritance.
  2. Step 2: Differentiate other APIs

    xQueueCreate() creates queues, vTaskDelay() delays tasks, and xTaskCreate() creates tasks, not mutexes.
  3. Final Answer:

    xSemaphoreCreateMutex() -> Option B
  4. Quick Check:

    Mutex with priority inheritance = xSemaphoreCreateMutex() [OK]
Hint: Mutex with priority inheritance uses xSemaphoreCreateMutex() [OK]
Common Mistakes:
  • Choosing xQueueCreate() for mutex
  • Confusing task creation with mutex creation
  • Using delay functions instead of mutex APIs
3. Given three tasks with priorities High (3), Medium (2), and Low (1). Low task holds a mutex needed by High task. What happens without priority inheritance?
medium
A. Medium task is blocked until High task finishes
B. Low task immediately releases the mutex
C. High task waits indefinitely while Medium task runs
D. High task preempts Low task and runs

Solution

  1. Step 1: Understand priority inversion scenario

    Low priority task holds mutex needed by High priority task, so High task blocks waiting.
  2. Step 2: Effect without priority inheritance

    Medium priority task runs, preventing Low task from running and releasing mutex, causing High task to wait indefinitely.
  3. Final Answer:

    High task waits indefinitely while Medium task runs -> Option C
  4. Quick Check:

    Priority inversion without inheritance = High task blocked [OK]
Hint: Without inheritance, medium task blocks low task, blocking high task [OK]
Common Mistakes:
  • Assuming low task releases mutex immediately
  • Thinking high task preempts low task despite mutex
  • Believing medium task is blocked
4. Identify the problem in this FreeRTOS code snippet causing priority inversion:
void lowTask(void *pvParameters) {
  xSemaphoreTake(mutex, portMAX_DELAY);
  // long processing
  xSemaphoreGive(mutex);
  vTaskDelete(NULL);
}

void highTask(void *pvParameters) {
  xSemaphoreTake(mutex, portMAX_DELAY);
  // quick processing
  xSemaphoreGive(mutex);
  vTaskDelete(NULL);
}
medium
A. Mutex created without priority inheritance support
B. High task does not take the mutex
C. Low task never releases the mutex
D. Tasks have the same priority

Solution

  1. Step 1: Analyze mutex usage in code

    Both tasks take and give the mutex correctly, so no misuse in code logic.
  2. Step 2: Check for priority inversion cause

    If mutex was created without priority inheritance, low task holding mutex blocks high task causing priority inversion.
  3. Final Answer:

    Mutex created without priority inheritance support -> Option A
  4. Quick Check:

    Priority inversion fix needs mutex with priority inheritance [OK]
Hint: Check if mutex supports priority inheritance to fix inversion [OK]
Common Mistakes:
  • Assuming high task does not take mutex
  • Thinking low task never releases mutex
  • Believing tasks have same priority
5. How can you prevent task starvation and priority inversion in a FreeRTOS system with multiple tasks sharing resources?
hard
A. Use busy-wait loops in low priority tasks to keep CPU busy
B. Assign all tasks the same priority to avoid conflicts
C. Avoid using any synchronization primitives like mutexes
D. Use mutexes with priority inheritance and design balanced task priorities

Solution

  1. Step 1: Understand causes of starvation and inversion

    Starvation happens when low priority tasks never run; inversion when low priority blocks high priority.
  2. Step 2: Apply solutions in FreeRTOS

    Using mutexes with priority inheritance prevents inversion; balanced priorities avoid starvation.
  3. Final Answer:

    Use mutexes with priority inheritance and design balanced task priorities -> Option D
  4. Quick Check:

    Priority inheritance + balanced priorities prevent issues [OK]
Hint: Combine priority inheritance mutexes with balanced priorities [OK]
Common Mistakes:
  • Setting all tasks to same priority causing inefficiency
  • Avoiding mutexes causing race conditions
  • Using busy-wait wastes CPU and worsens starvation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard