Difference between revisions of "Binary Semaphore 05: No-Ownership"
m (Explorer moved page Binary Semaphore 0E: to Binary Semaphore 05: No-Ownership) |
|||
Line 1: | Line 1: | ||
[[category: Free RTOS with Arduino]] | [[category: Free RTOS with Arduino]] | ||
− | In this tutorial, we will see | + | In this tutorial, we will see one of the demerits of binary semaphore ie. No-Ownership of semaphores.<br> |
− | + | Here we will see what happens if a task releases the semaphore locked by another task.<br><br> | |
==Teminologies== | ==Teminologies== | ||
Line 10: | Line 10: | ||
=Prerequisites= | =Prerequisites= | ||
− | Please check [[ | + | Please check [[Semaphore|this tutorial]] for detailed explanation on Semaphores.<br> <br> |
=API Details= | =API Details= | ||
Line 35: | Line 35: | ||
− | = | + | =Example= |
− | + | ||
In this example, we will be creating an LPT. LPT will acquire a semaphore and creates an HPT. | 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 | + | 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 creates MPT, as MPT has higher priority it preempts LPT and starts running. |
+ | At this point the semaphore is locked by LPT and HPT is waiting for it. Now MPT releases the semaphore locked by LPT. | ||
− | <html><script src="https://gist.github.com/SaheblalBagwan/ | + | <html><script src="https://gist.github.com/SaheblalBagwan/dbcd698228010f3a95c4927f18c365e8.js"></script></html> |
− | [[File: | + | [[File:BinarySemaphoreNonRecursiveLock.png]] |
− | |||
#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. | ||
− | #LPT starts executing again and | + | #LPT starts executing again and tries to take the same semaphore. Since the semaphore is not available, it gets blocked. |
− | + | #Finally the scheduler is left out with the idle task and it keeps running. The LPT and HPT will in blocked state until someone releases the semaphore. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | #Finally the scheduler is left out with the idle task and it keeps running. | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
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 16:54, 15 July 2016
In this tutorial, we will see one of the demerits of binary semaphore ie. No-Ownership of semaphores.
Here we will see what happens if a task releases the semaphore locked by another task.
Teminologies
- LPT: Low Priority Task
- MPT: Medium Priority Task
- 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.
Example
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 creates MPT, as MPT has higher priority it preempts LPT and starts running. At this point the semaphore is locked by LPT and HPT is waiting for it. Now MPT releases the semaphore locked by LPT.
- 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 tries to take the same semaphore. Since the semaphore is not available, it gets blocked.
- Finally the scheduler is left out with the idle task and it keeps running. The LPT and HPT will in blocked state until someone releases the semaphore.
Have an opinion, suggestion , question or feedback about the article let it out here!