ESP32 Task Starvation / Starve / Preemption / Preempt

ESP32 Developer – Priority
Arduino ESP32 FreeRTOS 2: How to use Task Parameter – Task Priorities – Task Handle – Idle Task Hook

There are 3 types of scheduling: Co-operative Scheduling and Prioritized Pre-emptive Scheduling with time slicing and without time slicing.

  • Prioritized Pre-emptive Scheduling with time slicing:
    task with higher priority will pre-empt the tasks that have lower priority. And tasks that have the same priority will run in turn every time tick interrupt occurs (RTOS has a timer interrupt to measure the time, every time the interrupt occurs RTOS will check that it is time to unblock or wake a task).
  • Prioritized Pre-emptive Scheduling without time slicing:
    task with higher priority will pre-empt the tasks that have lower priority. And tasks that have the same priority will not run in turn every time tick interrupt occurs (the task that is running, will continue running until it is pre-empted by high priority task).
  • Co-operative Scheduling:
    context switch occur when running task change from Running state to Blocked state or call taskYIELD(). The next running task is the task that has highest priority and is in Running state.

Watchdogs, Interrupt Watchdog Timer (IWDT), Task Watchdog Timer (TWDT)
Clarifications on freeRTOS
ESP-IDF, multicore & freeRTOS confusion

> Does freeRTOS uses a preemptive scheduler on ESP-idf?
Yes

> 1. Given
> core 0 runs a priority 10 task,
> core 1 runs a priority 12 task
> and a priority 11 task is awoken.
– If the priority 11 task is pinned to core 0 or has no core affinity, the priority 11 task will preempt on core 0.
– If the priority 11 task is pinned to core 1, core 1 will not preempt.

> 3. A high priority task that never calls vTaskDelay will eat
> all the resources and never let lower priorities run, right?
If the high priority task is pinned to a core, then it will starve the lower priority tasks on that core from cpu time. However if the high priority task as no core affinity, it can be possible that the task will bounce between the two cores, giving the lower priority task on each core a chance to run. However, writing a task function that never blocks is very poor application design. We guard against cpu starvation using the task watchdog timer.

Leave a Reply

Your email address will not be published. Required fields are marked *