semaphore
also: sem, counting semaphore, binary semaphore
A synchronization primitive that controls access to a shared resource by maintaining a counter; processes can wait for or signal the semaphore to coordinate execution.
A semaphore is a kernel-managed counter used to synchronize access between multiple processes or threads. It has two main operations: wait() (also called P or down) decrements the counter and blocks if it's zero, while signal() (also called V or up) increments the counter and wakes waiting processes.
A binary semaphore has values 0 or 1 and acts like a lock—only one process can hold it. A counting semaphore allows multiple processes (up to its initial value) to access a resource simultaneously, useful for managing pools of identical resources like database connections.
Example: A web server with 5 worker threads might use a counting semaphore initialized to 5. Each incoming request waits on the semaphore (decrementing it), processes the request, then signals (incrementing it) when done. If all 5 are busy, new requests block until a worker finishes.