Difference between revisions of "Changing Priority of Tasks"
Line 28: | Line 28: | ||
=Output1= | =Output1= | ||
− | [[FILE: | + | [[FILE:TaskPriorityChange2.png]] |
<br> | <br> | ||
0. Serial port is initialised and Task1 is created with priority 1. | 0. Serial port is initialised and Task1 is created with priority 1. | ||
Line 45: | Line 45: | ||
=Output2= | =Output2= | ||
− | [[FILE: | + | [[FILE:TaskPriorityChange.png]] |
<br> | <br> | ||
0. Serial port is initialised and Task1 is created with priority 1. | 0. Serial port is initialised and Task1 is created with priority 1. |
Revision as of 18:12, 30 June 2016
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.
Contents
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 parameter. Please check this link for detials.
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
0. Serial port is initialised and Task1 is created with priority 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.
- Now Task2 will create Task4 with priority 4. Now Task2 will preempt Task1 and starts running.
- 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.
- 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
0. Serial port is initialised and Task1 is created with priority 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.
- Now Task2 will create Task4 with priority 4. Now Task2 will preempt Task1 and starts running.
- Now Task4 changes the priority of Task1 to 4 which is highest of all. It will preemp Task4 and starts running.
- Now Task1 deletes all the task. The remaining part of Task2 and Task4 will never get executed.