0
0
FreeRTOSprogramming~10 mins

Task starvation and priority inversion in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Task starvation and priority inversion
Start Scheduler
Multiple Tasks Ready
Check Priorities
Highest Priority Task Runs
Lower Priority Tasks Wait
If Resource Held by Low Priority Task
Priority Inversion Occurs
Priority Inheritance Boosts Low Priority Task
Low Priority Task Releases Resource
Normal Priorities Resume
Starvation Avoided or Occurs if No Inheritance
Scheduler Continues
The scheduler runs the highest priority task ready. If a low priority task holds a resource needed by a high priority task, priority inversion happens. Priority inheritance temporarily boosts the low priority task to avoid starvation.
Execution Sample
FreeRTOS
Task HighPriority:
  Wait(Resource)
  Run Critical Section
  Release(Resource)

Task LowPriority:
  Acquire(Resource)
  Run Long Task
  Release(Resource)
Two tasks with different priorities share a resource; the low priority task holds it while the high priority task waits, causing priority inversion.
Execution Table
StepTask RunningResource StatePriority ActionNotes
1LowPriorityResource Acquired by LowPriorityLowPriority runsLowPriority starts and acquires resource
2HighPriorityResource Held by LowPriorityHighPriority blocked, waitingHighPriority tries to acquire resource but blocks
3LowPriorityResource HeldPriority Inheritance: LowPriority boostedLowPriority priority boosted to HighPriority level
4LowPriorityResource HeldLowPriority runs fasterLowPriority continues and finishes critical section
5LowPriorityResource ReleasedPriority restored to LowLowPriority releases resource, priority back to normal
6HighPriorityResource FreeHighPriority runsHighPriority acquires resource and runs
7IdleResource FreeNo tasks readyAll tasks done or waiting, scheduler idle
💡 HighPriority task acquires resource after LowPriority releases it, ending priority inversion
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
LowPriority PriorityLowLowLowBoosted to HighBoosted to HighRestored to LowLowLow
HighPriority StateReadyReadyBlockedBlockedBlockedReadyRunningDone
Resource StateFreeHeld by LowPriorityHeld by LowPriorityHeld by LowPriorityHeld by LowPriorityFreeAcquired by HighPriorityFree
Key Moments - 3 Insights
Why does the HighPriority task wait even though it has higher priority?
Because the resource it needs is held by the LowPriority task, so HighPriority blocks until the resource is free (see execution_table step 2).
How does priority inheritance help avoid starvation?
It temporarily raises the LowPriority task's priority to match HighPriority, letting it finish faster and release the resource (see execution_table step 3 and 4).
What happens if priority inheritance is not used?
The LowPriority task runs at low priority and may be delayed by other tasks, causing HighPriority to wait indefinitely (starvation).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what happens to the LowPriority task's priority?
AIt is boosted to HighPriority level
BIt stays low
CIt becomes idle
DIt is blocked
💡 Hint
Check the 'Priority Action' column at step 3 in the execution_table
At which step does the HighPriority task stop being blocked?
AStep 2
BStep 4
CStep 6
DStep 1
💡 Hint
Look at the 'HighPriority State' in variable_tracker and 'Task Running' in execution_table
If the LowPriority task never releases the resource, what happens to the HighPriority task?
AIt runs normally
BIt starves and never runs
CIt lowers its priority
DIt releases the resource itself
💡 Hint
Consider the blocking state of HighPriority in execution_table step 2 and the concept of starvation
Concept Snapshot
Task starvation happens when low priority tasks block high priority ones.
Priority inversion occurs when a low priority task holds a resource needed by a high priority task.
Priority inheritance temporarily boosts the low priority task's priority.
This helps avoid starvation and lets the high priority task run sooner.
FreeRTOS uses this to manage shared resources safely.
Full Transcript
In FreeRTOS, tasks have priorities. The scheduler runs the highest priority task ready. When a low priority task holds a resource needed by a high priority task, the high priority task waits, causing priority inversion. Priority inheritance boosts the low priority task's priority temporarily so it can finish and release the resource faster. This avoids starvation of the high priority task. The execution trace shows the low priority task acquiring the resource, the high priority task blocking, the priority boost, and finally the resource release and high priority task running. Without priority inheritance, the high priority task could wait forever if the low priority task is delayed.