Binary Semaphore in Operating System
Last Updated :
27 Feb, 2023
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.
Advantages:
- Using semaphores only one process can enter into a critical section. This process is also known as a mutual exclusion principle.
- Mostly semaphores are machine independent which means that irrespective of the platform they can execute anytime and anywhere.
Disadvantages:
- A lot of waits (P) and signal(V) operations can lead to performance degradation and make execution slow.
- 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:
- In the binary semaphore, it can take only integer values either 0 or 1.
- Here 1 stands for up-operation(V) and 0 stands for down-operation (P).
- 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.
- 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:
- This is a mechanism that can be used to ensure the synchronization of operations.
- You can implement a semaphore using the file descriptor to perform test operations and interrupts.
- This is a low-level synchronization mechanism.
Similar Reads
Semaphores Solutions in Operating System A Semaphore can be described as an object that consists of a counter, a waiting list of processes, Signal and Wait functions. The most basic use of semaphore is to initialize it to 1. When a thread want to enter a critical section, it calls down and enter the section. When another thread tries to do
3 min read
Process in Operating System A process is a program in execution. For example, when we write a program in C or C++ and compile it, the compiler creates binary code. The original code and binary code are both programs. When we actually run the binary code, it becomes a process. A process is an 'active' entity instead of a progra
3 min read
Fork System Call in Operating System In many operating systems, the fork system call is an essential operation. The fork system call allows the creation of a new process. When a process calls the fork(), it duplicates itself, resulting in two processes running at the same time. The new process that is created is called a child process.
5 min read
Operating System Services An operating system is software that acts as an intermediary between the user and computer hardware. It is a program with the help of which we are able to run various applications. It is the one program that is running all the time. Every computer must have an operating system to smoothly execute ot
6 min read
Critical Regions in Operating System In an operating system, a critical region refers to a section of code or a data structure that must be accessed exclusively by one method or thread at a time. Critical regions are utilized to prevent concurrent entry to shared sources, along with variables, information structures, or devices, that a
3 min read