A semaphore is simply an integer variable that is shared between multiple threads.
We can also say A semaphore is a data structure with an initialization operation and two usage operations. The data structure consists of a counter and a queue for picking up blocked processes. The concept comes from Dijkstra and was presented in 1965. Semaphores serve as a solution to the critical section problem. This is to avoid race conditions. It is a part of code that gets access to a resource. Semaphore is used to figure out the problem of mutual exclusion, race condition, and process synchronization.
There are two types of semaphores
- Binary Semaphore: Binary semaphores have only 0 and 1 values. It is also named as mutex lock. To implement the solution of critical section algorithms we used binary semaphore.
- Counting Semaphore: The value of counting semaphore reach over an unrestricted domain. To control access to a resource that has several instances we used counting semaphore.
Limitations of Semaphores
- Priority inversion is the biggest limitation of semaphores.
- Consider a process as attempting to awaken another process that is already in a wake-up state. Thus, deadlock may block endlessly.
- To wait and to signal the semaphore, the OS has to keep track of entire calls.