Difference between revisions of "Task Suspend and Resume"
(Created page with "category: Free RTOS with Arduino In earlier tutorials, we saw how to create and use and delete the tasks.<br> In this tutorial, we will see how to Suspend and Resume the t...") |
|||
(18 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[category: Free RTOS with Arduino]] | [[category: Free RTOS with Arduino]] | ||
− | In earlier tutorials, we saw how to create | + | In earlier tutorials, we saw how to create, use and delete the tasks.<br> |
In this tutorial, we will see how to Suspend and Resume the tasks.<br> | In this tutorial, we will see how to Suspend and Resume the tasks.<br> | ||
Line 11: | Line 11: | ||
<b>vTaskDelay()</b>: 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 [http://www.freertos.org/a00127.html this link] for more details.<br> | <b>vTaskDelay()</b>: 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 [http://www.freertos.org/a00127.html this link] for more details.<br> | ||
− | <b>vTaskDelete():</b>This function is used to delete as task. We need to pass the taskHandle of the task to be deleted.<br> To delete the own task we should pass NULL as parameter. | + | <b>vTaskDelete():</b>This function is used to delete as task. We need to pass the taskHandle of the task to be deleted.<br> |
− | Please check [http://www.freertos.org/a00126.html this link] for | + | To delete the own task we should pass NULL as the parameter. Please check [http://www.freertos.org/a00126.html this link] for more details. <br> |
<b>vTaskPrioritySet():</b> This function is used to change/Set the priority of a task.<br> | <b>vTaskPrioritySet():</b> This function is used to change/Set the priority of a task.<br> | ||
− | For this we need to pass the handle of the task and new priority to vTaskPrioritySet() function. | + | For this we need to pass the handle of the task and new priority to vTaskPrioritySet() function. Check [http://www.freertos.org/a00129.html this link] for more details. |
− | Check [http://www.freertos.org/a00129.html this link] for more details. | + | |
<b>vTaskPriorityGet():</b> This function is used to get the priority of a task.<br> | <b>vTaskPriorityGet():</b> This function is used to get the priority of a task.<br> | ||
− | For this we need to pass the handle of the task and it will return the task. | + | For this, we need to pass the handle of the task and it will return the task. Check [http://www.freertos.org/a00128.html this link] for more details. |
− | Check [http://www.freertos.org/a00128.html this link] for more details. | + | |
<b>vTaskSuspend():</b> This function is used to Suspend a task, the suspended remains in the same state util it is resumed.<br> | <b>vTaskSuspend():</b> This function is used to Suspend a task, the suspended remains in the same state util it is resumed.<br> | ||
− | For this we need to pass the handle of the tasks that needs to be suspended. Passing NULL will suspend own task. | + | For this, we need to pass the handle of the tasks that needs to be suspended. Passing NULL will suspend own task. Check [http://www.freertos.org/a00130.html this link] for more details. |
− | Check [http://www.freertos.org/a00130.html this link] for more details. | + | |
− | <b>vTaskResume():</b> This function is used to resume a suspended task. If the Resumed task has higher priority than the running task then it will | + | <b>vTaskResume():</b> This function is used to resume a suspended task. If the Resumed task has higher priority than the running task then it will preempt the running task or else stays in ready state<br> |
− | For this we need to pass the handle of the task to be resumed. | + | For this, we need to pass the handle of the task to be resumed. Check [http://www.freertos.org/a00131.html this link] for more details.<br><br> |
− | Check [http://www.freertos.org/a00131.html this link] for more details. | + | |
=Example1= | =Example1= | ||
− | <html> <script src="https://gist.github.com/SaheblalBagwan/ | + | In this example, we will be creating 4-Tasks with priorities(1-4). Task4 will suspend all the tasks except Task1.<br> |
+ | As Task2-Task4 will be in suspended state, Task1 will get the chance to run. Now Task1 will resume one task at a time and every time it resumes a task it will be preempted as the resumed task will have higher priority.<br> | ||
+ | The resumed task will run for some time and deletes itself. Because of this, Task1 will get alternative chance to Run.<br> | ||
+ | It resumes all the tasks one after another and finally deletes itself. | ||
+ | |||
+ | <html><script src="https://gist.github.com/SaheblalBagwan/89ad71f71dbb9799bd6db604fa0d18d5.js"></script></html> | ||
=Output1= | =Output1= | ||
− | [[FILE: | + | [[FILE:TaskSuspendAndResume.png]] |
<br> | <br> | ||
− | 0. Serial port is initialised and | + | 0. Serial port is initialised and 4-Tasks are created with priorities 1-4. |
− | # | + | #Task4 starts executing as it has the highest priority and suspends Task2,Task3 and itself. Now Task2, Task3 and Task4 are in suspended state and will not run until they are resumed. |
+ | #At this point Task1 and IDLE are available for the scheduler, So Task1 starts running. It resumes Task2 and immediately Task2 starts running as it has higher priority compared Task1. | ||
+ | #Task2 will run for some time and deletes itself. | ||
+ | #Again we are left with Task1 and IDLE, So Task1 starts running. It resumes Task3 and immediately Task3 starts running as it has higher priority compared to Task1. | ||
+ | #Task3 runs for some time and deletes itself. | ||
+ | #Again we are left with Task1 and IDLE, So Task1 starts running. It resumes Task4 and immediately Task4 starts running as it has higher priority compared to Task1. | ||
+ | #Task4 runs for some time and deletes itself. | ||
+ | #Now we are left with Task1 and Idle, so Task1 completes it job and deletes itself. | ||
+ | #Finally we are left with IDLE task which continues to run. <br><br> | ||
+ | |||
+ | =Example2= | ||
+ | This example will be similar to above one except that the Task1 priority will be changed to highest.<br> | ||
+ | Task1 starts resuming the tasks(2-4) one by one, as it has the highest priority it will continue to run even after resuming other tasks and finally deletes itself.<br> | ||
+ | After this, all the resumed task will start running one after the other depending on their priorities.<br> | ||
+ | Compare the order of task execution in both the output images for better understanding. | ||
+ | <html><script src="https://gist.github.com/SaheblalBagwan/60d8a7c113f1f429908d2ca8771afd51.js"></script></html> | ||
+ | |||
+ | =Output2= | ||
+ | [[FILE:TaskSuspendAndResume2.png]] | ||
+ | <br> | ||
+ | 0. Serial port is initialised and 4-Tasks are created with priorities 1-4. | ||
+ | #Task4 starts executing as it has the highest priority and suspends Task2,Task3 and itself. Now Task2, Task3 and Task4 are in suspended state and will not run until they are resumed. | ||
+ | #At this point Task1 and IDLE are available for the scheduler, So Task1 starts running. Now the Task1 changes its priority to 5 which is highest of all. It resumes Task2, Task3 and Tas4 and continues to run as it has the highest priority and deletes itself at the end. Now we have Task2, Task3, Task1 and Idle. All will run one after other depending on priorities. | ||
+ | #Now Task4 starts running and deletes itself. | ||
+ | #Task3 runs and deletes itself. | ||
+ | #Task2 runs and deletes itself. | ||
+ | #Finally IDLE task will run continuously. | ||
+ | |||
+ | =Downloads= | ||
+ | Download the complete project and libraries from [https://github.com/ExploreEmbedded/Arduino_FreeRTOS/archive/master.zip here].<br><br> | ||
+ | |||
+ | Have an opinion, suggestion , question or feedback about the article let it out here! | ||
+ | {{DISQUS}} |
Latest revision as of 15:57, 4 July 2016
In earlier tutorials, we saw how to create, use and delete the tasks.
In this tutorial, we will see how to Suspend and Resume the tasks.
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 more 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.
vTaskSuspend(): This function is used to Suspend a task, the suspended remains in the same state util it is resumed.
For this, we need to pass the handle of the tasks that needs to be suspended. Passing NULL will suspend own task. Check this link for more details.
vTaskResume(): This function is used to resume a suspended task. If the Resumed task has higher priority than the running task then it will preempt the running task or else stays in ready state
For this, we need to pass the handle of the task to be resumed. Check this link for more details.
Example1
In this example, we will be creating 4-Tasks with priorities(1-4). Task4 will suspend all the tasks except Task1.
As Task2-Task4 will be in suspended state, Task1 will get the chance to run. Now Task1 will resume one task at a time and every time it resumes a task it will be preempted as the resumed task will have higher priority.
The resumed task will run for some time and deletes itself. Because of this, Task1 will get alternative chance to Run.
It resumes all the tasks one after another and finally deletes itself.
Output1
0. Serial port is initialised and 4-Tasks are created with priorities 1-4.
- Task4 starts executing as it has the highest priority and suspends Task2,Task3 and itself. Now Task2, Task3 and Task4 are in suspended state and will not run until they are resumed.
- At this point Task1 and IDLE are available for the scheduler, So Task1 starts running. It resumes Task2 and immediately Task2 starts running as it has higher priority compared Task1.
- Task2 will run for some time and deletes itself.
- Again we are left with Task1 and IDLE, So Task1 starts running. It resumes Task3 and immediately Task3 starts running as it has higher priority compared to Task1.
- Task3 runs for some time and deletes itself.
- Again we are left with Task1 and IDLE, So Task1 starts running. It resumes Task4 and immediately Task4 starts running as it has higher priority compared to Task1.
- Task4 runs for some time and deletes itself.
- Now we are left with Task1 and Idle, so Task1 completes it job and deletes itself.
- Finally we are left with IDLE task which continues to run.
Example2
This example will be similar to above one except that the Task1 priority will be changed to highest.
Task1 starts resuming the tasks(2-4) one by one, as it has the highest priority it will continue to run even after resuming other tasks and finally deletes itself.
After this, all the resumed task will start running one after the other depending on their priorities.
Compare the order of task execution in both the output images for better understanding.
Output2
0. Serial port is initialised and 4-Tasks are created with priorities 1-4.
- Task4 starts executing as it has the highest priority and suspends Task2,Task3 and itself. Now Task2, Task3 and Task4 are in suspended state and will not run until they are resumed.
- At this point Task1 and IDLE are available for the scheduler, So Task1 starts running. Now the Task1 changes its priority to 5 which is highest of all. It resumes Task2, Task3 and Tas4 and continues to run as it has the highest priority and deletes itself at the end. Now we have Task2, Task3, Task1 and Idle. All will run one after other depending on priorities.
- Now Task4 starts running and deletes itself.
- Task3 runs and deletes itself.
- Task2 runs and deletes itself.
- Finally IDLE task will run continuously.
Downloads
Download the complete project and libraries from here.
Have an opinion, suggestion , question or feedback about the article let it out here!