Line 1: Line 1:
 
[[category: Free RTOS with Arduino]]
 
[[category: Free RTOS with Arduino]]
In earlier tutorials, we saw how to create, delete, suspend and resume the tasks.<br>
+
In this tutorial, we will see how to create and use binary semaphore.<br>
In this tutorial, we will see how to read the Task info(state, Stack size, priority.<br>
+
Later we will be looking into priority inversion using the LPT,MPT and HPT.<br><br>
+
 
 +
==Teminologies==
 +
#<b>LPT:</b> Low Priority Task
 +
#<b>MPT:</b> Medium Priority Task
 +
#<b>HPT:</b> High Priority Task
 +
 
 +
 
 +
=Prerequisites=
 +
Please check [[Semaphores|this tutorial]] for detailed explanation on Semaphores.<br> <br>
  
 
=API Details=
 
=API Details=
Here we will discuss some of the most frequently used APIs related to tasks.
+
Below is the list of the API's used in this tutorial.
  
<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>
+
<b>1.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>
  
<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>2.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>vTaskSuspend():</b> This function is used to Suspend a task, the suspended remains in the same state util it is resumed.<br>
+
<b>3.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.<br>
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.
+
Please check [http://www.freertos.org/a00126.html this link] for details. <br>
  
<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>
+
<b>4.vSemaphoreCreateBinary():</b> This interface is used to create a binary semaphore. We need to pass a handle of type SemaphoreHandle_t to this function.<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.
+
Please check [http://www.freertos.org/a00121.html this link] for details. <br>
 +
 
 +
<b>5.xSemaphoreTake():</b> 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.<br>  
 +
Please check [http://www.freertos.org/a00122.html this link] for details. <br>
 +
 
 +
<b>6.xSemaphoreGive():</b> This interface is used to release a binary semaphore. We need to pass a semaphore handle to this function.<br>
 +
Please check [http://www.freertos.org/a00123.html this link] for details. <br>
 +
 
 +
<b>7.vSemaphoreDelete ():</b> This interface is used to delete a binary semaphore. We need to pass a semaphore handle to this function.<br>
 +
Please check [http://www.freertos.org/a00113.html#vSemaphoreDelete this link] for details. <br>
  
<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>
 
  
 
=Example1=
 
=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.  
+
<b> Normal Priority Inversion:</b>
<html><script src="https://gist.github.com/SaheblalBagwan/c696f165daba3e2826cd3b9ebf33785f.js"></script></html>
+
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.
 +
 
 +
<html><script src="https://gist.github.com/SaheblalBagwan/5bec8e6039889b7ad4875b4a2321e602.js"></script></html>
 +
 
 +
[[File:BinarySemaphorePriorityInversion.png]]
 +
 
 +
Here an HPT tasks waits for the LPT tasks as it is holding the resource(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.
 +
#LPT starts executing again and releases the semaphore.
 +
#Immediately the HPT comes out of the blocked state and starts executing.
 +
#It releases the semaphore and runs for some time and deletes itself.
 +
#Now control goes back to LPT which completes its job and deletes itself.
 +
#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.
 +
 
 +
=Example2=
 +
<b> Extended Priority Inversion:</b>
 +
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.
 +
<html><script src="https://gist.github.com/SaheblalBagwan/e18934bcd64b11307fabf8b0e0812f32.js"></script></html>
 +
 
 +
[[File:BinarySemaphoreExtendedPriorityInversion.png]]
 +
 
 +
#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.
 +
#LPT starts executing again and waits for an event(packet to be received).
 +
#Now the Idle task starts running. Now the HPT is starved because of LPT and Idle task.
 +
#LPT comes out of blocked state as the event(wait time/packet is received) has occurred. Now it releases the semaphore.
 +
#Immediately the HPT comes out of the blocked state and starts executing. It releases the semaphore and runs for some time and deletes itself.
 +
#Now control goes back to LPT which completes its job and deletes itself.
 +
#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.
 +
 
 +
=Example3=
 +
<b>Worst Case Priority Inversion:</b>
 +
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.  
 +
<html><script src="https://gist.github.com/SaheblalBagwan/2e068861803630f0c404d48886c83775.js"></script></html>
  
=Output1=
+
[[File:BinarySemaphoreWorstCasePriorityInversion.png]]
[[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.
+
  
 +
#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.
 +
#LPT starts executing again and creates an MPT tasks.
 +
#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.
 +
#LPT comes out of blocked state and releases the semaphore.
 +
#Immediately the HPT comes out of the blocked state and starts executing. It releases the semaphore and runs for some time and deletes itself.
 +
#Now control goes back to LPT which completes its job and deletes itself.
 +
#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.
  
=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!
 
Have an opinion, suggestion , question or feedback about the article let it out here!
 
{{DISQUS}}
 
{{DISQUS}}

Revision as of 17:47, 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.

/***************************************************************************************************
ExploreEmbedded Copyright Notice
****************************************************************************************************
* File: BinarySemaphorePriorityInversion
* Version: 16.0
* Author: ExploreEmbedded
* Website: http://www.exploreembedded.com/wiki
* Description: Program to demonstrate the Binary semaphore usage and priority inversion
This code has been developed and tested on ExploreEmbedded boards.
We strongly believe that the library works on any of development boards for respective controllers.
Check this link http://www.exploreembedded.com/wiki for awesome tutorials on 8051,PIC,AVR,ARM,Robotics,RTOS,IOT.
ExploreEmbedded invests substantial time and effort developing open source HW and SW tools, to support consider
buying the ExploreEmbedded boards.
The ExploreEmbedded libraries and examples are licensed under the terms of the new-bsd license(two-clause bsd license).
See also: http://www.opensource.org/licenses/bsd-license.php
EXPLOREEMBEDDED DISCLAIMS ANY KIND OF HARDWARE FAILURE RESULTING OUT OF USAGE OF LIBRARIES, DIRECTLY OR
INDIRECTLY. FILES MAY BE SUBJECT TO CHANGE WITHOUT PRIOR NOTICE. THE REVISION HISTORY CONTAINS THE INFORMATION
RELATED TO UPDATES.
Permission to use, copy, modify, and distribute this software and its documentation for any purpose
and without fee is hereby granted, provided that this copyright notices appear in all copies
and that both those copyright notices and this permission notice appear in supporting documentation.
**************************************************************************************************/
#include <Arduino_FreeRTOS.h>
TaskHandle_t LPT_TaskHandle;
TaskHandle_t HPT_TaskHandle;
SemaphoreHandle_t binSemaphore_A = NULL;
#define printMsg(taskhandle,str) {\
Serial.print(F("Priority "));\ // Print task priority
Serial.print(uxTaskPriorityGet(taskhandle));\
Serial.print(F(" : "));\
Serial.println(F(str));\ // Print user string
}
void setup()
{
Serial.begin(9600);
Serial.println(F("In Setup function, Creating Binary Semaphore"));
vSemaphoreCreateBinary(binSemaphore_A); /* Create binary semaphore */
if(binSemaphore_A != NULL)
{
Serial.println(F("Creating low priority task"));
xTaskCreate(LPT_Task, "LPT_Task", 100, NULL, 1, &LPT_TaskHandle);
}
else
{
Serial.println(F("Failed to create Semaphore"));
}
}
void loop()
{ // Hooked to Idle Task, will run when CPU is Idle
Serial.println(F("Loop function"));
delay(50);
}
/*LPT: Low priority task*/
void LPT_Task(void* pvParameters)
{
printMsg(LPT_TaskHandle,"LPT_Task Acquiring semaphore");
xSemaphoreTake(binSemaphore_A,portMAX_DELAY);
printMsg(LPT_TaskHandle,"LPT_Task Creating HPT");
xTaskCreate(HPT_Task, "HPT_Task", 100, NULL, 3, &HPT_TaskHandle);
printMsg(LPT_TaskHandle,"LPT_Task Releasing the semaphore");
xSemaphoreGive(binSemaphore_A);
printMsg(LPT_TaskHandle,"LPT_Task Finally Exiting");
vTaskDelete(LPT_TaskHandle);
}
/*HPT: High priority task*/
void HPT_Task(void* pvParameters)
{
printMsg(HPT_TaskHandle,"HPT_Task Trying to Acquire the semaphore");
xSemaphoreTake(binSemaphore_A,portMAX_DELAY);
printMsg(HPT_TaskHandle,"HPT_Task Acquired the semaphore");
printMsg(HPT_TaskHandle,"HPT_Task Releasing the semaphore");
xSemaphoreGive(binSemaphore_A);
printMsg(HPT_TaskHandle,"HPT_Task About to Exit");
vTaskDelete(HPT_TaskHandle);
}

BinarySemaphorePriorityInversion.png

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.

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.

/***************************************************************************************************
ExploreEmbedded Copyright Notice
****************************************************************************************************
* File: BinarySemaphoreExtendedPriorityInversion
* Version: 16.0
* Author: ExploreEmbedded
* Website: http://www.exploreembedded.com/wiki
* Description: Program to demonstrate the Binary semaphore usage and extended priority inversion
This code has been developed and tested on ExploreEmbedded boards.
We strongly believe that the library works on any of development boards for respective controllers.
Check this link http://www.exploreembedded.com/wiki for awesome tutorials on 8051,PIC,AVR,ARM,Robotics,RTOS,IOT.
ExploreEmbedded invests substantial time and effort developing open source HW and SW tools, to support consider
buying the ExploreEmbedded boards.
The ExploreEmbedded libraries and examples are licensed under the terms of the new-bsd license(two-clause bsd license).
See also: http://www.opensource.org/licenses/bsd-license.php
EXPLOREEMBEDDED DISCLAIMS ANY KIND OF HARDWARE FAILURE RESULTING OUT OF USAGE OF LIBRARIES, DIRECTLY OR
INDIRECTLY. FILES MAY BE SUBJECT TO CHANGE WITHOUT PRIOR NOTICE. THE REVISION HISTORY CONTAINS THE INFORMATION
RELATED TO UPDATES.
Permission to use, copy, modify, and distribute this software and its documentation for any purpose
and without fee is hereby granted, provided that this copyright notices appear in all copies
and that both those copyright notices and this permission notice appear in supporting documentation.
**************************************************************************************************/
#include <Arduino_FreeRTOS.h>
TaskHandle_t LPT_TaskHandle;
TaskHandle_t HPT_TaskHandle;
SemaphoreHandle_t binSemaphore_A = NULL;
#define printMsg(taskhandle,str) {\
Serial.print(F("Priority "));\ // Print task priority
Serial.print(uxTaskPriorityGet(taskhandle));\
Serial.print(F(" : "));\
Serial.println(F(str));\ // Print user string
}
void setup()
{
Serial.begin(9600);
Serial.println(F("In Setup function, Creating Binary Semaphore"));
vSemaphoreCreateBinary(binSemaphore_A); /* Create binary semaphore */
if(binSemaphore_A != NULL)
{
Serial.println(F("Creating low priority task"));
xTaskCreate(LPT_Task, "LPT_Task", 100, NULL, 1, &LPT_TaskHandle);
}
else
{
Serial.println(F("Failed to create Semaphore"));
}
}
void loop()
{ // Hooked to Idle Task, will run when CPU is Idle
Serial.println(F("Loop function"));
delay(50);
}
/*LPT: Low priority task*/
void LPT_Task(void* pvParameters)
{
printMsg(LPT_TaskHandle,"LPT_Task Acquiring semaphore");
xSemaphoreTake(binSemaphore_A,portMAX_DELAY);
printMsg(LPT_TaskHandle,"LPT_Task Creating HPT");
xTaskCreate(HPT_Task, "HPT_Task", 100, NULL, 3, &HPT_TaskHandle);
printMsg(LPT_TaskHandle,"LPT_Task going to wait for some time");
vTaskDelay(100/portTICK_PERIOD_MS);
printMsg(LPT_TaskHandle,"LPT_Task Back from wait state, Releasing Semaphore");
xSemaphoreGive(binSemaphore_A);
printMsg(LPT_TaskHandle,"LPT_Task Finally Exiting");
vTaskDelete(LPT_TaskHandle);
}
/*HPT: High priority task*/
void HPT_Task(void* pvParameters)
{
printMsg(HPT_TaskHandle,"HPT_Task Trying to Acquire the semaphore");
xSemaphoreTake(binSemaphore_A,portMAX_DELAY);
printMsg(HPT_TaskHandle,"HPT_Task Acquired the semaphore");
printMsg(HPT_TaskHandle,"HPT_Task Releasing the semaphore");
xSemaphoreGive(binSemaphore_A);
printMsg(HPT_TaskHandle,"HPT_Task About to Exit");
vTaskDelete(HPT_TaskHandle);
}

BinarySemaphoreExtendedPriorityInversion.png

  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.

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.

/***************************************************************************************************
ExploreEmbedded Copyright Notice
****************************************************************************************************
* File: BinarySemaphoreWorstCasePriorityInversion
* Version: 16.0
* Author: ExploreEmbedded
* Website: http://www.exploreembedded.com/wiki
* Description: Program to demonstrate the Binary semaphore usage and worst case priority inversion
This code has been developed and tested on ExploreEmbedded boards.
We strongly believe that the library works on any of development boards for respective controllers.
Check this link http://www.exploreembedded.com/wiki for awesome tutorials on 8051,PIC,AVR,ARM,Robotics,RTOS,IOT.
ExploreEmbedded invests substantial time and effort developing open source HW and SW tools, to support consider
buying the ExploreEmbedded boards.
The ExploreEmbedded libraries and examples are licensed under the terms of the new-bsd license(two-clause bsd license).
See also: http://www.opensource.org/licenses/bsd-license.php
EXPLOREEMBEDDED DISCLAIMS ANY KIND OF HARDWARE FAILURE RESULTING OUT OF USAGE OF LIBRARIES, DIRECTLY OR
INDIRECTLY. FILES MAY BE SUBJECT TO CHANGE WITHOUT PRIOR NOTICE. THE REVISION HISTORY CONTAINS THE INFORMATION
RELATED TO UPDATES.
Permission to use, copy, modify, and distribute this software and its documentation for any purpose
and without fee is hereby granted, provided that this copyright notices appear in all copies
and that both those copyright notices and this permission notice appear in supporting documentation.
**************************************************************************************************/
#include <Arduino_FreeRTOS.h>
TaskHandle_t LPT_TaskHandle;
TaskHandle_t HPT_TaskHandle;
TaskHandle_t MPT_TaskHandle;
SemaphoreHandle_t binSemaphore_A = NULL;
#define printMsg(taskhandle,str) {\
Serial.print(F("Priority "));\ // Print task priority
Serial.print(uxTaskPriorityGet(taskhandle));\
Serial.print(F(" : "));\
Serial.println(F(str));\ // Print user string
}
void setup()
{
Serial.begin(9600);
Serial.println(F("In Setup function, Creating Binary Semaphore"));
vSemaphoreCreateBinary(binSemaphore_A); /* Create binary semaphore */
if(binSemaphore_A != NULL)
{
Serial.println(F("Creating low priority task"));
xTaskCreate(LPT_Task, "LPT_Task", 100, NULL, 1, &LPT_TaskHandle);
}
else
{
Serial.println(F("Failed to create Semaphore"));
}
}
void loop()
{ // Hooked to Idle Task, will run when CPU is Idle
Serial.println(F("Loop function"));
delay(50);
}
/*LPT: Low priority task*/
void LPT_Task(void* pvParameters)
{
printMsg(LPT_TaskHandle,"LPT_Task Acquiring semaphore");
xSemaphoreTake(binSemaphore_A,portMAX_DELAY);
printMsg(LPT_TaskHandle,"LPT_Task Creating HPT");
xTaskCreate(HPT_Task, "HPT_Task", 100, NULL, 3, &HPT_TaskHandle);
printMsg(LPT_TaskHandle,"LPT_Task Creating MPT");
xTaskCreate(MPT_Task, "MPT_Task", 100, NULL, 2, &MPT_TaskHandle);
printMsg(LPT_TaskHandle,"LPT_Task Releasing the semaphore");
xSemaphoreGive(binSemaphore_A);
printMsg(LPT_TaskHandle,"LPT_Task Finally Exiting");
vTaskDelete(LPT_TaskHandle);
}
/*MPT: Medium priority task*/
void MPT_Task(void* pvParameters)
{
printMsg(MPT_TaskHandle,"MPT_Task Done and About to Exit");
vTaskDelete(MPT_TaskHandle);
}
/*HPT: High priority task*/
void HPT_Task(void* pvParameters)
{
printMsg(HPT_TaskHandle,"HPT_Task Trying to Acquire the semaphore");
xSemaphoreTake(binSemaphore_A,portMAX_DELAY);
printMsg(HPT_TaskHandle,"HPT_Task Acquired the semaphore");
printMsg(HPT_TaskHandle,"HPT_Task Releasing the semaphore");
xSemaphoreGive(binSemaphore_A);
printMsg(HPT_TaskHandle,"HPT_Task About to Exit");
vTaskDelete(HPT_TaskHandle);
}

BinarySemaphoreWorstCasePriorityInversion.png

  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.


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