A semaphore is a synchronization primitive that uses an internal counter to control access for multiple agents to a finite set of shared resources or a critical section of code. Invented by Edsger Dijkstra, it operates on two atomic operations: wait() (or P) to acquire a permit, decrementing the counter, and signal() (or V) to release a permit, incrementing the counter. If the counter is zero, a calling agent must wait until a permit is released. This mechanism is more flexible than a simple mutex, as it can allow more than one agent simultaneous access.
