Unit content
Counting semaphores and permits
A counting semaphore represents a number of available permits for a shared resource or activity.
An acquire operation—often called wait, P, or down—takes one permit. If no permit is available, the caller waits. A release operation—often called post, V, or up—returns a permit and can wake a waiter.
If a service may run at most three expensive jobs simultaneously, initialize a semaphore to 3:
acquire(semaphore)
run expensive job
release(semaphore)
The first three callers can proceed; further callers wait until a permit is returned.
A semaphore differs conceptually from a mutex. A mutex protects ownership of a critical section so only one owner enters at a time. A counting semaphore represents capacity and can allow several participants simultaneously.
Semaphores can also coordinate producer-consumer systems by counting available items or free slots, but incorrect acquire/release accounting can leak permits or allow more activity than intended.
The semaphore count is therefore a synchronization representation of available capacity, not the resource itself.