{"id":9690,"date":"2019-06-08T15:10:13","date_gmt":"2019-06-08T15:10:13","guid":{"rendered":"http:\/\/blog.bachi.net\/?p=9690"},"modified":"2020-08-20T17:12:38","modified_gmt":"2020-08-20T17:12:38","slug":"esp32-task-starvation","status":"publish","type":"post","link":"https:\/\/blog.bachi.net\/?p=9690","title":{"rendered":"ESP32 Task Starvation \/ Starve \/ Preemption \/ Preempt"},"content":{"rendered":"<p><a href=\"https:\/\/esp32developer.com\/programming-in-c-c\/tasks\/priority\">ESP32 Developer &#8211; Priority<\/a><br \/>\n<a href=\"http:\/\/www.iotsharing.com\/2017\/06\/arduino-esp32-freertos-how-to-use-task-param-task-priority-task-handle.html\">Arduino ESP32 FreeRTOS 2: How to use Task Parameter &#8211; Task Priorities &#8211; Task Handle &#8211; Idle Task Hook<\/a><\/p>\n<p>There are 3 types of scheduling: Co-operative Scheduling and Prioritized Pre-emptive Scheduling with time slicing and without time slicing.<\/p>\n<ul>\n<li><strong>Prioritized Pre-emptive Scheduling with time slicing:<\/strong><br \/> 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).<\/li>\n<li><strong>Prioritized Pre-emptive Scheduling without time slicing:<\/strong><br \/> 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).<\/li>\n<li><strong>Co-operative Scheduling:<\/strong><br \/> 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.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/docs.espressif.com\/projects\/esp-idf\/en\/latest\/api-reference\/system\/wdts.html\">Watchdogs<\/a>, Interrupt Watchdog Timer (IWDT), Task Watchdog Timer (TWDT)<br \/>\n<a href=\"https:\/\/www.esp32.com\/viewtopic.php?t=7775\">Clarifications on freeRTOS<\/a><br \/>\n<a href=\"https:\/\/www.esp32.com\/viewtopic.php?t=1293\">ESP-IDF, multicore &#038; freeRTOS confusion<\/a><\/p>\n<p>> Does freeRTOS uses a preemptive scheduler on ESP-idf?<br \/>\nYes<\/p>\n<p>> 1. Given<br \/>\n> core 0 runs a priority 10 task,<br \/>\n> core 1 runs a priority 12 task<br \/>\n> and a priority 11 task is awoken.<br \/>\n&#8211; If the priority 11 task is pinned to core 0 or has no core affinity, the priority 11 task will preempt on core 0.<br \/>\n&#8211; If the priority 11 task is pinned to core 1, core 1 will not preempt.<\/p>\n<p>> 3. A high priority task that never calls vTaskDelay will eat<br \/>\n> all the resources and never let lower priorities run, right?<br \/>\nIf 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.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nets Jun  8 2016 00:22:57\r\n\r\nrst:0xc (SW_CPU_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)\r\nconfigsip: 0, SPIWP:0xee\r\nclk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00\r\nmode:DIO, clock div:2\r\nload:0x3fff0018,len:4\r\nload:0x3fff001c,len:1044\r\nload:0x40078000,len:8896\r\nload:0x40080400,len:5828\r\nentry 0x400806ac\r\n\r\nSerial done!\r\nRS485 done!\r\n\r\n99 GT2\r\nE (11904) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:\r\nE (11904) task_wdt:  - IDLE0 (CPU 0)\r\nE (11904) task_wdt: Tasks currently running:\r\nE (11904) task_wdt: CPU 0: taskTempCurrent\r\nE (11904) task_wdt: CPU 1: IDLE1\r\nE (11904) task_wdt: Aborting.\r\nabort() was called at PC 0x400e63cb on core 0\r\n\r\nBacktrace:\r\n0x4008ba20:0x3ffbe170 0x4008bc51:0x3ffbe190 0x400e63cb:0x3ffbe1b0\r\n0x40084775:0x3ffbe1d0 0x4000bfed:0x3ffb45d0 0x4008952d:0x3ffb45e0\r\n0x400885af:0x3ffb4600 0x400e457f:0x3ffb4620 0x400e33a1:0x3ffb4640\r\n0x400d17bd:0x3ffb4660 0x400d18a8:0x3ffb4680 0x400d1a12:0x3ffb46c0\r\n0x40088661:0x3ffb46f0\r\n\r\nRebooting...\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ESP32 Developer &#8211; Priority Arduino ESP32 FreeRTOS 2: How to use Task Parameter &#8211; Task Priorities &#8211; Task Handle &#8211; 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-9690","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/9690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9690"}],"version-history":[{"count":11,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/9690\/revisions"}],"predecessor-version":[{"id":11299,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/9690\/revisions\/11299"}],"wp:attachment":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}