m (Explorer moved page Semaphores to Read Task Info : vTaskList())
 
(21 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[category: Free RTOS with Arduino]]
 
[[category: Free RTOS with Arduino]]
In this tutorial, we will be discussing a Semaphore and its types ie. Binary, Mutex, Counting.<br>
+
In earlier tutorials, we saw how to create, delete, suspend and resume the tasks.<br>
Later we will see each semaphore in detail with its pros and cons.
+
In this tutorial, we will see how to read the Task info(state, Stack size, priority.<br>
Also, we will be looking into priority inversion and priority inheritance.<br><br>
+
  
=What is a Semaphore?=
+
[[File:0ReadTaskInfo.png]]
Semaphore is a technique for synchronizing two/more task competing for the same resources. When a task wants to use a resource, it requests for the semaphore and will be allocated if the semaphore is available. If the semaphore is not available then the requesting task will go to blocked state till the semaphore becomes free.
+
  
Consider a situation where there are two persons who want to share a bike.
+
=API Details=
At one time only one person can use the bike. The one who has the bike key will get the chance to use it. And when this person gives the key to the 2nd person, then the 2nd person can use the bike.
+
Here we will discuss some of the most frequently used APIs related to tasks.
  
Semaphore is just like this <b>Key</b> and the bike is the shared resource.
+
<b>xTaskCreate()</b>: 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 [http://www.freertos.org/a00125.html this link] for more details.<br>
Whenever a task wants access to the shared resource, it must acquire the semaphore first.
+
 
The task should release the semaphore after it is done with the shared resource.  
+
<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>
Until this time all other tasks have to wait if they need access to shared resource as semaphore is not available.
+
 
Even if the task trying to acquire the semaphore is of higher priority than the task acquiring the semaphore,
+
<b>vTaskSuspend():</b> This function is used to Suspend a task, the suspended remains in the same state util it is resumed.<br>
it will be in the wait state until the semaphore is released by the lower priority 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.
 +
 
 +
<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. Check [http://www.freertos.org/a00131.html this link] for more details.
 +
 
 +
<b>vTaskList():</b> This function is used to read the task details(name, state, priority, num). We need to pass a string pointer(buffer) to which it copies the above task details. The buffer size should be minimum 40 bytes per task. Check [http://www.freertos.org/a00021.html#vTaskList this link] for more details.
 
<br><br>
 
<br><br>
  
=Types of Semaphores=
+
=Example1=
There are 3-types of semaphores namely Binary, Counting and Mutex semaphore.
+
In this example, we will be creating 3-tasks which run for some time and enter the blocked state allowing other tasks to run. We will include INT0 falling edge interrupt to read and display the current task list with all the task info. Every time interrupt is generated it will take the snapshot of tasks(name, state, priority, num) and sends on Serial port.  
*<b>Binary Semaphore</b>: Binary semaphore is used when there is only one shared resource.
+
<html><script src="https://gist.github.com/SaheblalBagwan/c696f165daba3e2826cd3b9ebf33785f.js"></script></html>
Binary semaphore exists in two states ie.Acquired(Take), Released(Give).
+
 
Binary semaphores have no ownership  and can be released by any task or ISR regardless of who performed the last take operation. Because of this binary semaphores are often used to synchronize tasks with external events implemented as ISRs, for example waiting for a packet from a network or waiting for a button is pressed.
+
=Output1=
 +
[[FILE:VTaskList().png]]
 +
<br>
 +
All the tasks will be running for some time and enter the blocked state allowing other tasks to run. Task3 suspends and resumes the Task1 alternatively.
 +
Whenever INT0 interrupt is generated, ISR will take the snapshot of all the task and sends it on the serial port.
  
Because there is no ownership concept a binary semaphore object can be created to be either in the “taken” or “not taken” state initially.
 
  
Cons:
 
#Priority Inversion : HPT needs to wait for the LPT as LPT is holding the resource required for HPT. In between if MPT task comes then LPT will be in blocked state thereby delaying HPT.
 
#Does not support recursion : If a task tries to take the semaphore twice then it gets blocked and will not come out of that state till someone releases that semaphore.
 
#No ownership : Anyone can release or delete the semaphore because of which the dependent tasks will always be in the blocked state.
 
  
*<b>Counnting Semaphore</b>: To handle more than one shared resource of the same type, counting semaphore is used.
+
=Downloads=
 +
Download the complete project and libraries from [https://github.com/ExploreEmbedded/Arduino_FreeRTOS/archive/master.zip here].<br><br>
  
*<b>Mutex Semaphore</b>: To avoid extended priority inversion, mutex can be used.
+
Have an opinion, suggestion , question or feedback about the article let it out here!
 +
{{DISQUS}}

Latest revision as of 17:54, 14 July 2016

In earlier tutorials, we saw how to create, delete, suspend and resume the tasks.
In this tutorial, we will see how to read the Task info(state, Stack size, priority.

0ReadTaskInfo.png


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.

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.

vTaskList(): This function is used to read the task details(name, state, priority, num). We need to pass a string pointer(buffer) to which it copies the above task details. The buffer size should be minimum 40 bytes per task. Check this link for more details.

Example1

In this example, we will be creating 3-tasks which run for some time and enter the blocked state allowing other tasks to run. We will include INT0 falling edge interrupt to read and display the current task list with all the task info. Every time interrupt is generated it will take the snapshot of tasks(name, state, priority, num) and sends on Serial port.

Output1

VTaskList().png
All the tasks will be running for some time and enter the blocked state allowing other tasks to run. Task3 suspends and resumes the Task1 alternatively. Whenever INT0 interrupt is generated, ISR will take the snapshot of all the task and sends it on the serial port.


Downloads

Download the complete project and libraries from here.

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