A Dutch computer scientist and mathematician, Dijkstra was one of the most influential figures in the development of modern computer science. In addition to introducing semaphores and the wait and signal primitives for concurrent programming, he is famous for the shortest-path algorithm named after him: Dijkstra’s algorithm.
To manage access to the shared resource R, processes invoke the two operations on the integer variable S:
wait(S) (also called the acquisition operation): checks the state of S. If S < 1, the process blocks or waits. If S >= 1, the process decrements the variable (S--) and enters the critical section.
signal(S) (also called the release operation): increments the semaphore variable (S++) to release the resource or unblock any waiting processes.
To protect a shared resource and ensure that only one process at a time can access it, every process that intends to use it must strictly follow a standard protocol composed of three sequential phases:
Resource request (wait): the process invokes the wait(SR) primitive to check the semaphore state and acquire access.
Resource use: the process enters its critical section and performs the necessary operations on the shared resource R.
Resource release (signal): at the end of the operations, the process invokes signal(SR) to restore the free state and allow waiting processes to proceed.
In early theoretical implementations, wait was executed through a continuous loop (for example, while (S < 1) { }), wasting CPU cycles (busy waiting).
In modern operating systems, waiting is handled in a passive manner:
A waiting queue Qs is associated with the semaphore.
If a process executes wait(S) and finds S < 1, the operating system suspends it, moving it from the Running state to the Waiting state and adding its descriptor to the queue Qs.
When another process executes signal(S), the system removes one process from Qs and moves it to the Ready state, avoiding wasted computing resources.
To understand how a semaphore works in depth, we must analyze what happens internally during wait(S). Acquiring a resource with wait requires two distinct actions in sequence:
Check the semaphore state (verify whether S >= 1).
Update the semaphore (decrement it or suspend the process if S < 1).
To avoid errors, these two operations must be performed atomically (indivisibly). An atomic operation is a block of code that, once started by the processor, must execute to completion without interruption or the possibility of another process interleaving within it.
If the check and subsequent update were not atomic, serious synchronization failures could occur. Imagine this sequence with two concurrent processes (P1 and P2) and a semaphore S initialized to 1:
Process P1 performs the check and finds the semaphore free (S = 1). However, before it can change it to 0 (busy), the operating system suspends P1 and assigns the CPU to P2.
Process P2 also performs the check, likewise finds the semaphore free (S = 1), and proceeds to occupy it.
When P1 resumes execution, believing the resource is free, it occupies it too.
The result is disastrous: both processes access the shared resource simultaneously, completely violating the principle of mutual exclusion.
In future practical Java concurrent-programming exercises, we will see how the language provides native tools (such as the synchronized keyword or wait/notify blocks) that automatically handle this atomicity at the system level, protecting critical sections and preventing hidden conflicts among threads.