A semaphore is a technique used to manage concurrent processes. Basically, the semaphore is a variable that is shared between threads, and it’s used to solve problems and to achieve synchronization in the multiprocessing environment.
Regarding the semaphores, there are two different types, a binary semaphore, and a counting semaphore. Binary semaphore can only have two values, 1 or 0, and its initial value is always 1. On the other hand, the counting semaphore can have a range of values so it can be used to control access to resources with multiple instances. Bear in mind that the value of the semaphore can’t be a negative integer value.
Semaphores do have some disadvantages, and in order to avoid issues, semaphores need to keep track of wait and signal commands. In addition, both wait and signal operations need to be executed in the correct order to avoid issues.