In earlier tutorials, we saw how to create, use and then delete the tasks.
In this tutorial, we will see how to read the priority of task and also change it dynamically.


API Details

Here we will discuss some of the most frequently used APIs related to tasks.

xTaskCreate(): This interface is used to create a new Task, if the task is successfully created then it returns pdPass(1) or else errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY(-1). Check this link for more details.

vTaskDelay(): This function is used to delay/block the task for specified delay time(ticks). INCLUDE_vTaskDelay needs to be set to 1 in FreeRtosConfig.h file for using this function. Check this link for more details.

vTaskDelete():This function is used to delete as task. We need to pass the taskHandle of the task to be deleted.
To delete the own task we should pass NULL as the parameter. Please check this link for details.

vTaskPrioritySet(): This function is used to change/Set the priority of a task.
For this we need to pass the handle of the task and new priority to vTaskPrioritySet() function. Check this link for more details.

vTaskPriorityGet(): This function is used to get the priority of a task.
For this, we need to pass the handle of the task and it will return the task. Check this link for more details.

Example1

In this example, we will be changing the priority of Task1 from low(1) to medium(3).
This task will delete all the tasks which have priorities lower than it. The task with higher priorities will continue to run ahead of Task1, once their job is done they delete themselves. So we will be able to see the output of the tasks with priorities higher than Task1.

Output1

TaskPriorityChange2.png
0. Serial port is initialised and Task1 is created with priority 1.

  1. As we have only Task1 and Idle task, Task1 starts executing. Furter Task1 creates Task2 with priority 2. Now Task2 will preempt Task1 and starts running.
  2. Now Task2 will create Task4 with priority 4. Now Task2 will preempt Task1 and starts running.
  3. Now Task4 changes the priority of Task1 to 3 which is greater than Task2 but less than Task4. Hence Task4 still continues to run and enters waiting/blocked state.
  4. Now we have Task1(Prio:3), Task2(Prio:2), IDLE(Prio:0) ready to execute. Since Task1 has higher priority, it starts running and prints its priority as 3 and deletes all the task. The remaining part of Task2 and Task4 will never get executed.


Example2

In earlier example, we changed the priority of Task1 from 1 to 3, because of which the Tasks with priority greater than 3 were able to run.
In this example, we will change the priority of Task1 from low(1) to highest(5). This task will delete all the tasks and deletes itself after completing its job.

Output2

TaskPriorityChange.png
0. Serial port is initialised and Task1 is created with priority 1.

  1. As we have only Task1 and Idle task, Task1 starts executing. Furter Task1 creates Task2 with priority 2. Now Task2 will preempt Task1 and starts running.
  2. Now Task2 will create Task4 with priority 4. Now Task2 will preempt Task1 and starts running.
  3. Now Task4 changes the priority of Task1 to 4 which is highest of all. It will preempt Task4 and starts running.
  4. Now Task1 deletes all the task. The remaining part of Task2 and Task4 will never get executed.

Downloads

Download the complete project and libraries from here.

Have an opinion, suggestion , question or feedback about the article let it out here!