Difference between revisions of "RTOS Basics : Semaphore"
(Created page with "category : RTOS Tutorial ~~~~ ---- ==What is a Semaphore ?== Consider a situation when there are two person who wants to share a bike. At one time only one person can...") |
m |
||
Line 13: | Line 13: | ||
The task should release the semaphore after it is done with the shared resource. | The task should release the semaphore after it is done with the shared resource. | ||
Till this time all other tasks have to wait if they need access to shared resource as semaphore is not available. | Till 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 higher priority | + | Even if the task trying to acquire the semaphore is of higher priority than the task acquiring the semaphore, |
it will be in wait state until semaphore is released by the lower priority task. | it will be in wait state until semaphore is released by the lower priority task. | ||
− | |||
==Use of Semaphore== | ==Use of Semaphore== |
Revision as of 15:31, 8 April 2015
Amruta (talk) 15:30, 8 April 2015 (IST)
What is a Semaphore ?
Consider a situation when there are two person who wants to share a bike. 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 2nd person, only then 2nd person can use the bike.
Semaphore is just like this Key and the bike is the shared resource. 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. Till 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, it will be in wait state until semaphore is released by the lower priority task.
Use of Semaphore
- Managing Shared Resource
- Task Synchronization
- Apart from managing shared resource, task synchronization can also be performed with the help of a semaphore.
In this case semaphore will be like a flag not key.
- Unilateral Rendezvous
- Bilateral Rendezvous
Types of semaphore
- Binary Semaphore
- Binary semaphore is used when there is only one shared resource.
- Counting Semaphore
- To handle more then one shared resource of same type, counting semaphore is used.
- Mutual Exclusion Semaphore or Mutex
- To avoid extended priority inversion, mutexes can be used. You can check Mutex Working here.
Operations on Semaphore
Basically, there are 3 operations related to the semaphore:
- Create
- Acquire
- Release
{{{DISOUS}}}