0
0
FreeRTOSprogramming~7 mins

Task starvation and priority inversion in FreeRTOS

Choose your learning style9 modes available
Introduction

Task starvation and priority inversion happen when some tasks can't run because others keep using the CPU or resources. Understanding these helps make your program fair and fast.

When you have tasks with different importance levels sharing resources.
When a low priority task blocks a high priority task from running.
When you want to avoid some tasks never getting CPU time.
When debugging why a high priority task is delayed unexpectedly.
Syntax
FreeRTOS
/* No direct syntax, but concepts relate to task priorities and resource locking in FreeRTOS */

// Example: Creating tasks with priorities
xTaskCreate(TaskFunction, "Task1", stackSize, NULL, priority, &taskHandle);

Task starvation happens when low priority tasks never get CPU because higher priority tasks run all the time.

Priority inversion happens when a low priority task holds a resource needed by a high priority task, blocking it.

Examples
Two tasks created with different priorities. High priority task can starve the low priority one.
FreeRTOS
xTaskCreate(TaskLowPriority, "LowTask", 1000, NULL, 1, NULL);
xTaskCreate(TaskHighPriority, "HighTask", 1000, NULL, 3, NULL);
Mutex with priority inheritance helps prevent priority inversion by temporarily boosting low priority task's priority.
FreeRTOS
/* Using mutex with priority inheritance to avoid priority inversion */
SemaphoreHandle_t mutex = xSemaphoreCreateMutex();

// Low priority task takes mutex
xSemaphoreTake(mutex, portMAX_DELAY);
// High priority task waits for mutex
xSemaphoreTake(mutex, portMAX_DELAY);
Sample Program

This program shows a low priority task holding a mutex and a high priority task waiting for it. FreeRTOS priority inheritance helps avoid priority inversion.

FreeRTOS
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include <stdio.h>

SemaphoreHandle_t mutex;

void LowPriorityTask(void *pvParameters) {
    while(1) {
        xSemaphoreTake(mutex, portMAX_DELAY);
        printf("Low priority task running and holding mutex\n");
        vTaskDelay(pdMS_TO_TICKS(2000));
        xSemaphoreGive(mutex);
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

void HighPriorityTask(void *pvParameters) {
    while(1) {
        printf("High priority task trying to take mutex\n");
        xSemaphoreTake(mutex, portMAX_DELAY);
        printf("High priority task got mutex and running\n");
        xSemaphoreGive(mutex);
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

int main(void) {
    mutex = xSemaphoreCreateMutex();
    xTaskCreate(LowPriorityTask, "LowTask", 1000, NULL, 1, NULL);
    xTaskCreate(HighPriorityTask, "HighTask", 1000, NULL, 3, NULL);
    vTaskStartScheduler();
    return 0;
}
OutputSuccess
Important Notes

Always use mutexes with priority inheritance in FreeRTOS to avoid priority inversion.

Task starvation can happen if high priority tasks never block or yield.

Design your tasks and priorities carefully to keep the system responsive.

Summary

Task starvation means some tasks never get CPU time because others run too much.

Priority inversion happens when a low priority task blocks a high priority one by holding a resource.

FreeRTOS mutexes with priority inheritance help fix priority inversion problems.