Open In App

Binary Semaphore in Operating System

Improve
Improve
Like Article
Like
Save
Share
Report

Semaphores are one of the easiest and best process synchronization mechanisms founded by Dijkstra in the mid-’90s. Binary Semaphore provides mutual synchronization between the processes in an operating system. It has an integer range of values from 0 to 1. 

Basically, Binary Semaphores have two operations namely wait(P) and signal(V) operations. Both operations are atomic. Semaphore(s) can be initialized to zero or one. Here atomic means that the variable on which read, modify, and update happens at the same time/moment with no pre-emption.

Semaphores basically are used to implement critical sections in code. Critical sections are regions where only one process can execute at once. There are some advantages and disadvantages of binary semaphores as well.

Binary Semaphore in Operating System

 

Advantages: 

  1. Using semaphores only one process can enter into a critical section. This process is also known as a mutual exclusion principle.
  2. Mostly semaphores are machine independent which means that irrespective of the platform they can execute anytime and anywhere.

Disadvantages:

  1. A lot of waits (P) and signal(V) operations can lead to performance degradation and make execution slow.
  2. It will not always satisfies the mutual exclusion, progress and bounded wait.

Binary Semaphores are different from Counting semaphores in terms of values because binary semaphores can take only 0 or 1. At the same time, counting semaphores can take from  - \infty to + \infty      . The binary semaphores are different from counting semaphores because counting semaphore allows more than one process to enter into critical sections simultaneously.

Let’s see the programming implementation of binary semaphores in an operating system.

Code:

/*package whatever*/

public class BinarySemaphoreSample
{  
//initialising lock variable
private boolean lock = false; 
BinarySemaphore(int start)
{  
lock = (start == 0);  
}  

//error Handling
public synchronized void waitForNotify() 
throws InterruptedException   
{
while (lock)   
{
wait();  
}
lock = true;  
}
public synchronized void notifyToWakeup()  
{  
if (lock)   
{  
notify();  
}  
lock = false;  //lock variable
}  
}  

Features of Binary Semaphores:

  1. In the binary semaphore, it can take only integer values either 0 or 1.
  2. Here 1 stands for up-operation(V) and 0 stands for down-operation (P).
  3. The major reason behind introducing binary semaphores is that it allows only one process to enter into a critical section if they are sharing resources.
  4. 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.

Uses of Binary Semaphores:

  1. This is a mechanism that can be used to ensure the synchronization of operations.
  2. You can implement a semaphore using the file descriptor to perform test operations and interrupts.
  3. This is a low-level synchronization mechanism.

Last Updated : 27 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads