What is Semaphore in OS?
The semaphore in OS (operating system) is initialized to the number of concurrent threads that you want to enter the section before they block.
Define Semaphore in OS
Logically, semaphore meaning in os says it is an integer variable that, apart from the initialization of the semaphore, can be only accessed through the mutually exclusive two atomic operations that are:
- Wait()
- Signal().
Wait(S) or P(S)
If the semaphore value for a certain process is smaller than 0, the process goes into a block state or is added to the waiting queue. If the semaphore value for a certain process is greater than 0, then the semaphore value is decremented. The structure of the atomic operation wait() is as below:
wait(S)
{
while(S≤ 0);
S = S – 1;
}
Signal(S) or V(S)
The atomic operation signal(S) or V(S) increments the semaphore value. The structure of the atomic operation signal() is as below:
signal(S)
{
S = S + 1;
}
Types of Semaphore in OS
A semaphore is essentially a non-negative integer that acts as a signal to address the critical section problem. It is a concept in operating systems that allows concurrent processes to be synchronized. The semaphore in OS(operating system) is categorized into two different types that are as follows:
- Counting semaphore
- Binary semaphore or mutexes
Which type of semaphore do we implement in os practical?
Binary
What is Counting Semaphore in OS?
The counting semaphore in OS has two elements: an integer value and an associated waiting list (mostly a queue). The value of the counting semaphore in OS (operating system) may be positive or negative.
The operation of counting semaphore in OS(operating system) includes:
- Wait (also referred to as Down operation, P operation).
- Signal (also referred to as Up operation, V operation, or Release operation).
What is Binary Semaphore in OS?
A binary semaphore in OS has two elements: the integer values can be 0 or 1, and an associated waiting list (mostly a queue). The waiting list(queue) of binary semaphores in OS indicates the blocked processes when trying to enter the critical section. The wait operation is executed when a process tries to enter the critical section. Some of the possible cases with the value of binary semaphore are as follows.
Case I: Binary semaphore value = 1
Suppose the binary semaphore value is 1. The value of the binary semaphore is set to 0 if any process is allowed to enter the critical section.
Case II: Binary semaphore value = 0
Suppose the binary semaphore value is 0. No process is allowed to enter into the critical section of the code and is blocked and kept in the waiting queue. The signal operation is executed when a process takes an exit from the critical section.
Important Points for Semaphore in OS (Operating System)
Every semaphore variable will have its own suspended list. Let us check the important points for semaphore in OS given below.
- Down and Up operations are atomic that is, no preemption can occur in between, and thus, interrupts are disabled.
- If more than one process is on the suspended list, then whenever we perform one up operation, one process will wake up from the suspended list, which will be based on the FIFO order.
- If two or more processes are on the suspended list and there is no other process to wake up these processes, then those processes are said to be involved in the deadlock.
- When the processes are on the suspended list, they remain in the ready state but will not participate in the scheduling.
Advantages of Semaphore in OS
Semaphores are implemented in the microkernel's machine-independent code. As a result, they are machine-agnostic. Let us check some more advantages of semaphore in OS.
- Only one process is allowed to enter the critical part thanks to semaphores. They closely adhere to the mutual exclusion principle and are far more efficient than other synchronization methods.
- Because processor time is not wasted unnecessarily checking if a condition is met to allow a process to access the vital region, there is no resource waste due to busy waiting in semaphores.
Comments
write a comment