Semaphores act as primitive objects which perform synchronizations of several sorts. Using semaphores, critical sections which execute in mutual exclusion can be built. In turn these allow new atomic actions to be defined, which can then perform any required form of synchronization. A semaphore, S, is an object with two components, one an integer value , the other a queue, waiters, of threads waiting on this semaphore. The semaphore constructor sets waiters empty and value to a supplied non-negative integer.
All semaphore operations are atomic.
Suppose S.value is initially 1. At most one such block of code can be in execution at any one time. Suppose C.value is initially 0. Semaphores have their dangers. Several higher-level facilities have been supplied to make synchronization safer.