The value of a semaphore variable in binary semaphores is either 0 or 1.
The value of the semaphore variable is initially set to 1, but if a process requests a resource, the wait() method is invoked, and the value of this semaphore is changed from 1 to 0.
When the process has finished using the resource, the signal() method is invoked, and the value of this semaphore variable is raised to 1.
If the value of this semaphore variable is 0 at a given point in time, and another process wants to access the same resource, it must wait for the prior process to release the resource.
Process synchronization can be performed in this manner.
Where are Binary Semaphores Used?
The mutual exclusion was not used in counting semaphore because we had a group of processes that needed to run in the crucial region at the same time.
Binary Semaphore, on the other hand, rigorously enforces mutual exclusion.
Instead of having many slots in the crucial part, we can only have one process in the critical portion here.
There are just two possible values for the semaphore: 0 or 1.
Features of Binary Semaphore :
A Binary Semaphore is one with an integer value between 0 and 1.
It’s nothing more than a lock with two possible values: 0 and 1.
0 denotes being busy, while 1 denotes being unoccupied.
The rationale behind a binary semaphore is that it only enables one process to enter the crucial area at a time, thus allowing it to access the resource that is shared.
The value 0 indicates that a process or thread is in the critical section, i.e. it is accessing a shared resource, and that the other processes or threads should wait for it to finish.
The critical region, on the other hand, is free because no process is accessing the resource that is shared.
It ensures reciprocal exclusion by ensuring that no two processes are in the crucial section at the same time.
It cannot ensure bounded waiting because it is only a variable that retains an integer value.
It’s possible that a process will never get a chance to enter the critical section, causing it to starve.
We don’t want it to happen.
To know the difference between Counting Semaphore & Binary Semaphore :