Types of Semaphores
Here we will discuss the types of Semaphores as follows.
Type-1: General Semaphore
A semaphore whose integer component can take arbitrary non-negative values of S.L. these is called General Semaphore. They are kind of weak semaphores.
Type-2: Binary Semaphore
A semaphore whose integer component S.L. takes only the values 0 and 1 is called a binary semaphore. This is also known as “mutex” which stands for mutual exclusion.
Initialization
S <- (0, φ) or S <- (1, φ)
Wait for Operation: It remains Unchanged as above.
Signal Operation: It slightly changes as follows
Algorithm
Signal(S) : if (S.V.== 1) { // Undefined // Return } else if (S.L == empty) { S.V.= 1 } else { Let q be some process in S.L. S.L. = S.L. - { q } q.state = ready }
Type-3 : Strong Semaphore
In Strong semaphores, S.L. remains unchanged as in weak semaphores whereas S.V. is replaced by the queue. Because removal of arbitrary process in a weak semaphore it may lead to starvation whereas in this case, it remains free from starvation.
Initialization
S <- (0,empty)
Wait for Operation Algorithm
Wait(S) : if (S.V > 0) { S.V.= S.V - 1 } else { S.L.= S.L.U p p.state = blocked }
Signal Operation Algorithm
Signal(S) : if (S.L.== empty) { S.V.= S.V.+ 1 } else { q = head(S.L.) S.L. = S.L. - { q } q.state = ready }
Type-4 Busy- Wait for Semaphore
It does not have a component S.L. and Semaphore S is identified only by S.V. Busy-Wait Semaphore are appropriate in a multi-processor system where the waiting process has its own processor and is not waste CPU time that could be used for computation.
Wait for Operation Algorithm
Wait(S): await S>0 S = S - 1
Signal Operation Algorithm
Signal(S): S = S + 1
Advantages
- They do not allow more than one process to enter the critical section. In this way, mutual exclusion is achieved and thus they are extremely efficient than other techniques for synchronization.
- Due to busy waiting in semaphore, there is no wastage of process time and resources. This is because the processes are only allowed to enter the critical section after satisfying a certain condition.
- They are machine-independent as they run in the machine-independent code of the microkernel.
- They allow flexible management of resources.
Disadvantages
- There may be a situation of priority inversion where the processes having low priority get access to the critical section than the processes having higher priority.
- To avoid deadlocks, the wait() and signal() operations have to be executed in the correct order.
- Semaphore programming is complicated and there are chances of not achieving mutual exclusion.
Conclusion
In this section, we have discussed semaphores and their types, and also we have discussed their atomic operations. These are the basics of the Semaphore which are used for solving the problem of a critical section.
Semaphores and its types
Semaphores are compound data types with two fields one is a Non-negative integer S.V. and the second is a set of processes in a queue S.L. It is used to solve critical section problems, and by using two atomic operations, it will be solved. In this, wait and signal that is used for process synchronization.
Contact Us