# ESP32 Task Starvation / Starve / Preemption / Preempt

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.

ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
entry 0x400806ac

Serial done!
RS485 done!

99 GT2
E (11904) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (11904) task_wdt:  - IDLE0 (CPU 0)