Semaphore:-
Semaphores are two-field data types, one of which is a non-negative type of integer S.V and the other is a set of processes in a queue S.L. It is used to address critical section problems by using two atomic operations, wait and signal, to synchronize processes in this.
Semaphores refer to the integer variables that are primarily used to solve the critical section problem via combining two of the atomic procedures, wait and signal, for the process synchronization.
The definitions of signal and wait are given below:
Wait:-
It decrements the value of its A argument in case it is positive. In case S is zero or negative, then no operation would be performed.
wait(A)
{
while (A<=0);
A–;
}
Signal:-
This operation increments the actual value of its argument A.
signal(A)
{
A++;
}
Pros of Semaphore:-
- Only one process is allowed to enter the critical part because of semaphores. They adhere closely to the mutual exclusion principle. Also, they are far more efficient as compared to the other synchronization methods.
- Since the processor time is not wasted in checking whether a condition is met so as to allow a process to access its critical section, there is ultimately no resource wastage due to busy waiting in the semaphores.
- Semaphores are implemented in the microkernel’s machine-independent code. They are, therefore, machine-independent.
- Due to how complex semaphores are, the wait and signal actions must be implemented in the proper order to avoid deadlocks.
- Semaphores are very impractical for their usage at the final scale since they reduce modularity. This mainly occurs because the wait, as well as the signal procedures, bar the system from forming an organized layout.
- Semaphores can cause a priority inversion, with low-priority processes getting access to the important portion first and high-priority processes getting access afterward.