A semaphore is a synchronization tool used in operating systems to manage concurrent processes and prevent issues like race conditions. It helps control access to shared resources in a multithreaded or multiprocess environment.
There are two main types:
Binary Semaphore (0 or 1): Works like a lock—only one process can access the resource at a time.
Counting Semaphore: Allows a set number of processes to access a resource simultaneously.
Semaphores use two atomic operations:
wait (P): Decreases the count; if it becomes negative, the process is blocked.
signal (V): Increases the count; if there are waiting processes, one is unblocked.
In simple terms, semaphores ensure safe and orderly execution of processes when they compete for limited system resources.