Line 32: Line 32:
 
#Now Task2 will create Task4 with priority 4. 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 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) reasy to execeute. Since Task1 has higher it starts running and prints its priority as 3 and delets all the task. The remaining part of Task2, Task4 will never get executed.
+
#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.

Revision as of 15:18, 28 June 2016

In earlier tutorials, we saw how to create and use and 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 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.

Example

Output

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 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.