0
0
FreeRTOSprogramming~30 mins

Task starvation and priority inversion in FreeRTOS - Mini Project: Build & Apply

Choose your learning style9 modes available
Task Starvation and Priority Inversion in FreeRTOS
📖 Scenario: You are working on a FreeRTOS-based embedded system where multiple tasks share a resource. You want to understand how task starvation and priority inversion can happen and how to detect them.
🎯 Goal: Build a simple FreeRTOS program with three tasks of different priorities sharing a mutex. Observe how priority inversion can cause task starvation and learn how to use priority inheritance to fix it.
📋 What You'll Learn
Create three tasks: HighPriorityTask, MediumPriorityTask, and LowPriorityTask
Use a mutex called xMutex to protect a shared resource
Demonstrate priority inversion by having LowPriorityTask hold the mutex while HighPriorityTask waits
Add priority inheritance to the mutex to prevent starvation
Print messages to the console to show task execution order
💡 Why This Matters
🌍 Real World
Embedded systems often have tasks with different priorities sharing resources. Understanding priority inversion helps avoid delays in critical tasks.
💼 Career
Embedded software engineers must manage task priorities and synchronization to ensure real-time system responsiveness and reliability.
Progress0 / 4 steps
1
Create three tasks and a mutex
Create a mutex called xMutex using xSemaphoreCreateMutex(). Then create three tasks named LowPriorityTask, MediumPriorityTask, and HighPriorityTask with priorities 1, 2, and 3 respectively. Use xTaskCreate() for each task. Do not start the scheduler yet.
FreeRTOS
Need a hint?

Use xSemaphoreCreateMutex() to create the mutex. Use xTaskCreate() to create each task with the correct priority.

2
Add mutex locking in LowPriorityTask
Modify LowPriorityTask to take the mutex xMutex using xSemaphoreTake() before printing "Low priority task running". Hold the mutex for 3000 milliseconds using vTaskDelay(), then release it with xSemaphoreGive(). Keep the other tasks unchanged.
FreeRTOS
Need a hint?

Use xSemaphoreTake(xMutex, portMAX_DELAY) to wait for the mutex. Print the message, delay for 3000 ms, then release the mutex with xSemaphoreGive(xMutex).

3
Make HighPriorityTask try to take the mutex and print messages
Modify HighPriorityTask to try taking the mutex xMutex with xSemaphoreTake() using portMAX_DELAY. When it gets the mutex, print "High priority task running", then release the mutex with xSemaphoreGive(). Add a short delay of 1000 milliseconds after releasing the mutex. Keep MediumPriorityTask unchanged.
FreeRTOS
Need a hint?

Use xSemaphoreTake(xMutex, portMAX_DELAY) to wait for the mutex. Print the message, release the mutex, then delay 1000 ms.

4
Enable priority inheritance and observe output
Modify the mutex creation to enable priority inheritance by using xSemaphoreCreateMutex() (already done). Run the program and print the messages from all tasks to observe how priority inversion is resolved. Add print statements in MediumPriorityTask to print "Medium priority task running" every 1000 milliseconds. Finally, print the messages from all tasks to the console.
FreeRTOS
Need a hint?

In MediumPriorityTask, add a print statement inside the infinite loop and delay 1000 ms each time.