Line 1: Line 1:
 
[[category: Free RTOS with Arduino]]
 
[[category: Free RTOS with Arduino]]
In this tutorial, we will see how to create and use binary semaphore. Later we will be looking into priority inversion using the LPT,MPT and HPT.<br><br>
+
In this tutorial, we will see how to create and use binary semaphore.<br>
 +
Later we will be looking into priority inversion using the LPT,MPT and HPT.<br><br>
  
 
==Teminologies==
 
==Teminologies==
Line 35: Line 36:
  
 
=Example1: Normal Priority Inversion =
 
=Example1: Normal Priority Inversion =
 +
In this example, we will be creating an LPT. LPT will acquire a semaphore and creates an HPT.
 +
HPT will preempt LPT as starts running. HPT tries to acquire a semaphore used by LPT and goes to blocked state resulting in priority inversion. Now LPT starts running and releases the semaphore and immediately HPT comes out of block state and starts running.
 +
 +
 +
 
Here an HPT tasks waits for the LPT tasks as it is holding the resource(semaphore).
 
Here an HPT tasks waits for the LPT tasks as it is holding the resource(semaphore).
 
#LPT starts running and acquires the semaphore.
 
#LPT starts running and acquires the semaphore.
Line 48: Line 54:
  
 
=Example2: Extended Priority Inversion =
 
=Example2: Extended Priority Inversion =
Here an HPT tasks waits for the LPT tasks as it is holding the resource(semaphore). Further, the LPT waits for the event/data to be received and the CPU executes the IDLE tasks. This makes the HPT wait for more time.  
+
In this example,  HPT waits for the LPT as it is holding the resource(semaphore). Further, the LPT waits for the event/data to be received and the CPU executes the IDLE tasks. This makes the HPT wait for more time.  
 +
 
 
#LPT starts running and acquires the semaphore.
 
#LPT starts running and acquires the semaphore.
 
#Now HPT is created and it preempts LPT and starts running. It makes the request to acquire the semaphore. Since the semaphore is already with LPT, HPT goes to blocked state.
 
#Now HPT is created and it preempts LPT and starts running. It makes the request to acquire the semaphore. Since the semaphore is already with LPT, HPT goes to blocked state.
Line 63: Line 70:
  
 
=Example3: Worst Case Priority Inversion=
 
=Example3: Worst Case Priority Inversion=
Here an HPT tasks waits for the LPT tasks as it is holding the resource(semaphore). In between an MPT starts executing thereby blocking the LPT and HPT. This makes the HPT wait for MPT and LPT.  
+
In this example,  HPT waits for the LPT as it is holding the resource(semaphore). In between an MPT starts executing thereby blocking the LPT and HPT. This makes the HPT wait for MPT and LPT.  
 +
 
 
#LPT starts running and acquires the semaphore.
 
#LPT starts running and acquires the semaphore.
 
#Now HPT is created and it preempts LPT and starts running. It makes the request to acquire the semaphore. Since the semaphore is already with LPT, HPT goes to blocked state.
 
#Now HPT is created and it preempts LPT and starts running. It makes the request to acquire the semaphore. Since the semaphore is already with LPT, HPT goes to blocked state.

Revision as of 14:05, 14 July 2016

In this tutorial, we will see how to create and use binary semaphore.
Later we will be looking into priority inversion using the LPT,MPT and HPT.

Teminologies

  1. LPT: Low Priority Task
  2. MPT: Medium Priority Task
  3. HPT: High Priority Task


Prerequisites

Please check this tutorial for detailed explanation on Semaphores.

API Details

Below is the list of the API's used in this tutorial.

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

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

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

4.vSemaphoreCreateBinary(): This interface is used to create a binary semaphore. We need to pass a handle of type SemaphoreHandle_t to this function.
Please check this link for details.

5.xSemaphoreTake(): This interface is used to acquire a binary semaphore. We need to pass a semaphore handle and the amount of time to wait for semaphore to become available.
Please check this link for details.

6.xSemaphoreGive(): This interface is used to release a binary semaphore. We need to pass a semaphore handle to this function.
Please check this link for details.

7.vSemaphoreDelete (): This interface is used to delete a binary semaphore. We need to pass a semaphore handle to this function.
Please check this link for details.


Example1: Normal Priority Inversion

In this example, we will be creating an LPT. LPT will acquire a semaphore and creates an HPT. HPT will preempt LPT as starts running. HPT tries to acquire a semaphore used by LPT and goes to blocked state resulting in priority inversion. Now LPT starts running and releases the semaphore and immediately HPT comes out of block state and starts running.


Here an HPT tasks waits for the LPT tasks as it is holding the resource(semaphore).

  1. LPT starts running and acquires the semaphore.
  2. Now HPT is created and it preempts LPT and starts running. It makes the request to acquire the semaphore. Since the semaphore is already with LPT, HPT goes to blocked state.
  3. LPT starts executing again and releases the semaphore.
  4. Immediately the HPT comes out of the blocked state and starts executing.
  5. It releases the semaphore and runs for some time and deletes itself.
  6. Now control goes back to LPT which completes its job and deletes itself.
  7. Finally the scheduler is left out with the idle task and it keeps running.

In this scenario, the HPT task waits for LPT from 2-4 which is the priority inversion period. PriorityInversion.png

Example2: Extended Priority Inversion

In this example, HPT waits for the LPT as it is holding the resource(semaphore). Further, the LPT waits for the event/data to be received and the CPU executes the IDLE tasks. This makes the HPT wait for more time.

  1. LPT starts running and acquires the semaphore.
  2. Now HPT is created and it preempts LPT and starts running. It makes the request to acquire the semaphore. Since the semaphore is already with LPT, HPT goes to blocked state.
  3. LPT starts executing again and waits for an event(packet to be received).
  4. Now the Idle task starts running. Now the HPT is starved because of LPT and Idle task.
  5. LPT comes out of blocked state as the event(wait time/packet is received) has occurred. Now it releases the semaphore.
  6. Immediately the HPT comes out of the blocked state and starts executing. It releases the semaphore and runs for some time and deletes itself.
  7. Now control goes back to LPT which completes its job and deletes itself.
  8. Finally the scheduler is left out with the idle task and it keeps running.

In this scenario, the HPT task waits for LPT and Idle task from 2-5 which is the extended priority inversion period. ExtendedPriorityInversion.png


Example3: Worst Case Priority Inversion

In this example, HPT waits for the LPT as it is holding the resource(semaphore). In between an MPT starts executing thereby blocking the LPT and HPT. This makes the HPT wait for MPT and LPT.

  1. LPT starts running and acquires the semaphore.
  2. Now HPT is created and it preempts LPT and starts running. It makes the request to acquire the semaphore. Since the semaphore is already with LPT, HPT goes to blocked state.
  3. LPT starts executing again and creates an MPT tasks.
  4. MPT preempts the LPT and starts running. At this point, the HPT is starved because of LPT and MPT. MPT completes its job and deletes itself.
  5. LPT comes out of blocked state and releases the semaphore.
  6. Immediately the HPT comes out of the blocked state and starts executing. It releases the semaphore and runs for some time and deletes itself.
  7. Now control goes back to LPT which completes its job and deletes itself.
  8. Finally the scheduler is left out with the idle task and it keeps running.

In this scenario, the HPT task waits for LPT and MPT from 2-6 which is the extended priority inversion period. 0WorstCasePriorityInversion.png


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