Semaphores
It is a software-based solution to synchronization problems.
Semaphore proposed by Edsger Dijkstra is a technique to manage concurrent processes by passing a simple integer value, which is known as a semaphore.
Semaphores contain two atomic operations wait and signal that are used in process synchronization.
Wait:
The wait operation decrements the value of its argument S if it is positive. If S is negative or zero then no operation is performed.
wait(S)
{
while(S<=0)
S--;
}
Signal:
The signal operation increases the value of its argument S.
signal(S)
{
S++
}
Types of Semaphores:
Semaphores are of the following types:
Binary Semaphore:
- Mutex lock is another name for binary semaphore.
- It can have only two values 0 or 1.
- Its value is 1 by default.
- In binary semaphore 0 denotes busy and 1 denotes unoccupied.
Advantages:
- Only one process is allowed to enter the critical part of semaphores. They are far more efficient as compared to other synchronization methods.
- They are machine-independent.
Counting Semaphore:
- Counting semaphore is a synchronization mechanism that is used to restrict access to a resource with many copies.
- A counting semaphore's value can range from 0 to N; where N is the number of processes that are free to exit the crucial area
- Counting semaphores assures bounded wait since numerous instances of the process can use the shared resource at the same time.
- A process that enters the crucial section must wait for the other process to enter the critical section using such a semaphore, suggesting that no process will starve.