Integer variables that help solve the critical section problem are known as semaphores. Wait and signal are the two atomic operations that help achieve process synchronization in a multiprocessing environment..

If an argument, let’s say argument A is positive, then the wait (sleep or down) operation decrements its value. And if argument A is negative or zero, then no operation occurs. Whereas, the signal (wake-up or up) operation increments the value of argument A.

P(A) is wait and V(A) is signal.

```P(A): if A >= 1 then A := A - 1
else <block and enqueue the process>;

V(A): if <some process is blocked on the queue>
then <unblock a process>
else A := A + 1;
```

### Properties of Semaphores

Following are the properties of semaphore:

• Simple and has a positive integer value.
• Can work with multiple processes.
• Different critical sections for different semaphores.
• An unique access semaphore for each critical section.
• Allows multiple processes into the critical section at a time, if required.

### Types of Semaphore

There are two types of semaphores:

1. Counting Semaphore

Counting semaphores are integer value semaphores. They have an unrestricted value domain and are used to coordinate resource access. During this coordination, the count of semaphores is equal to the number of available resources and if a resource is added or removed, then the count of semaphores changes accordingly.

2. Binary Semaphore

Binary semaphores are similar to the above, just that their value is restricted to 0 and 1. The wait and signal operation only works when the semaphore is 1 and 0 respectively. Binary semaphores are easier to implement.

### Counting Semaphore vs Binary Semaphore

 COUNTING SEMAPHORE BINARY SEMAPHORE No mutual exclusion Mutual exclusion is present Any integer value possible Two integer values, 0 and 1 More than one slots Only one slot Offers a set of processes Contains mutual exclusion mechanism

### Semaphore vs Mutex

 PARAMETER SEMAPHORE MUTEX Data Type Integer variable Object Mechanism Signaling mechanism Type of locking mechanism Types Binary and Counting No Type Operation Wait and signal modifies the semaphore’s value Locked and Unlocked operation Thread Multiple program threads Multiple program threads that aren’t simultaneous

Following are the advantages of semaphore:

• They allow one process into the critical section at a time and follow the principle of mutual exclusion. They are far more efficient than other methods of synchronization.
• Zero wastage of resources due to busy waiting in semaphore.
• The OS implements a semaphore in the machine-independent code of the microkernel. This makes them machine-independent.

### Disadvantages of Semaphore in Operating System

Following are the disadvantages of semaphores:

• It is necessary to implement the wait and signal operations in the correct order in order to prevent deadlocks.
• They are impractical for last-scale use. This is because it leads to loss of modularity as wait and signal operations don’t allow the creation of a structured layout for the system.
• Semaphores can cause a priority inversion where low priority processes may get the chance to access the critical section before the high priority processes.

### Summary

A semaphore is a variable that helps with critical section problems and process synchronisation. Wait and signal are the two operations that help modify the value of a semaphore. There are two types of semaphores, namely, counting and binary.