In real-time operating systems, multitasking is achieved by dividing each application into useful tasks. But not all tasks execute throughout the execution of the program. Some tasks are more important and execute more frequently than others. Therefore, in RTOS, some tasks can be deleted during program execution when there is no need to call them in the near future or when we do not need them anymore in the program. Hence, in this tutorial, we will learn how to delete tasks using FreeRTOS API with Arduino.

Before preceding further with the tutorial, you should read to know how to use FreeRTOS with Arduino. You must read this getting started tutorial:

FreeRTOS Task deleting API

vTaskDelete() FreeRTOS API function is used to delete tasks. With the help of this API function, any task can delete itself and can also delete other tasks by passing reference by a handler to vTaskDelete() function.

Note: Before using vTaskDelete(), you should make changes to FreeRTOSConfig.h by setting INCLUDE_vTaskDelete = 1. Otherwise, vTaskDelete() API do not work.

To set INCLUDE_vTaskDelete = 1, go to the libraries folder of Arduino IDE. After that, open Arduino FreeRTOS library folder>>scr

Arduino Serial Monitor Output

This diagram shows the output of Arduino Serial monitor when program executed.

At T1, task1 being a highest priority task starts to execute. It turns on LED1( D4) and makes LED2 low. Moreover, it also prints “Task1 running” on Arduino Serial monitor. After that, it create Task2 with priority 2 which is higher than task2. Hence, Task2 becomes the highest priority and blocked Task2.

At T2, Task2 enters the running state and start its execution. It does nothing except deleting itself and displaying “Task2 is runnig and about to delete itself” on Serial monitor of Arduino. It also inverses the state of GPIO pins D4 and D5.

At T3, Task1 calls vTaskDelay(), allowing the idle task to run until the delay time expires, and the whole sequence repeats.

Video Demo

Commonly Asked Questions

Why we need to delete Task?

In RTOS, we divide a complete program into smaller mini programs that are knows as tasks. But some tasks are more important than other and critical tasks execute more frequency than non-important tasks. Therefore, sometimes we delete unnecessary tasks to free memory and create them again when require again.

What Happens when we delete the task?

When we delete task through FreeRTOS task deletion API, task does not exist and can not be called until created again. Moreover, FreeRTOS idle task free the memory that has been allocated to the task when it created.

Does RTOS Kernel automatically delete the resources?

Only memory allocated to a task by the kernel itself will be freed automatically when the task is deleted. Any memory or other resource that the implementation of the task allocated must be freed explicitly [FreeRTOS].