Semaphores in Operating Systems | OS Tutorials YASH PAL, June 2, 2026June 2, 2026 To solve the critical section problem in an operating system, we have some hardware-based approaches that we use to achieve mutual exclusion in an operating system, but these approaches are complicated for application programmers to use while developing real programs for an operating system. To overcome this difficulty, we use a synchronization tool called a semaphore.Table of Contents Semaphore in an Operating SystemUsage of SemaphoreBinary SemaphoreSemaphore PropertiesDrawback of SemaphoreSemaphore in an Operating SystemA semaphore “S” is an integer variable that, apart from initialization, is accessed only through two standard atomic operations:Wait()Signal()The wait() operation was originally termed P, and signal() was originally called V.Definition of Wait (P):Decrements the value of the semaphore variable “S” as soon as it becomes positive.Wait (s){ While (S <= 0); //no-op S--; }Definition of Signal (V):Increments the value of the semaphore variable “S” by 1.Signal(s){ S++; }Modification of the integer variable “S” in both WAIT and SIGNAL operations is indivisible.Indivisible:It means when one process modifies the value of the semaphore variable “S”, no other process can simultaneously modify that same semaphore value.In addition, in the case of wait(S), the testing of the integer value of S (S <= 0), as well as its possible modification (S–), must be executed without interruption.Usage of SemaphoreOperating systems often distinguish between counting and binary semaphores. The value of a counting semaphore can range over an unrestricted domain.Binary SemaphoreA binary semaphore is a semaphore with an integer value that can range only between 0 and 1. In principle, it should be easier to implement the binary semaphore.In this, a queue is used to hold processes waiting on the semaphore. The process that has been blocked the longest is released from the queue.Semaphores are not provided by hardware. But they have several attractive properties.Semaphore PropertiesMachine-independent – no need for code at the assembly level as in test-and-set.Simple.Works with any number of processes.Can have different semaphores for different critical sections.A process can acquire multiple needed resources by executing multiple downs.Can allow just one process into the critical region using a simple binary semaphore, or more than one if desired using counting semaphores. (NB: Unix supports arrays of counting semaphores – more on that next week)For both semaphores and binary semaphores, a queue is used to hold processes waiting on the semaphore. FIFO policy is used to remove the processes from the queue.The process that has been blocked the longest is released from the queue first is called a strong semaphore. A semaphore that does not specify the order in which processes are removed from the queue is called a weak semaphore.Drawback of SemaphoreThey are essentially shared global variables.Access to semaphores can come from anywhere in a program.There is no control or guarantee of proper usage.There is no linguistic connection between the semaphore and the data to which the semaphore controls access.They serve two purposes: mutual exclusion and scheduling constraints. engineering subjects Operating System Operating System