Semaphores
wait/signal with an animated waiting queue.
Operating Systems: Semaphores
1. Semaphore Usage & Core Concepts
A Semaphore S is an integer variable accessed only through two standard atomic operations: wait() (historically P) and signal() (historically V).
The integer value can range over an unrestricted domain. Used to control access to a resource consisting of a finite number of instances (e.g., resource pool of size N).
The integer value ranges only between 0 and 1. Behave similarly to mutex locks and are used to enforce mutual exclusion across shared critical sections.
Used to solve statement ordering problems. If statement S1 in Process P1 must execute before S2 in Process P2, initialize synch = 0 and use signal(synch) in P1 and wait(synch) in P2.
2. Semaphore Implementation (No Busy Waiting)
To avoid wasting CPU cycles in a spinlock loop, semaphores can suspend processes. When a process executes wait() and finds the semaphore value is non-positive, it blocks itself and enters a waiting queue linked to the semaphore.
// Struct definition of Semaphore typedef struct { int value; struct process *list; // Waiting queue } semaphore; // Non-busy waiting wait() void wait(semaphore *S) { S->value--; if (S->value < 0) { add process to S->list; block(); // Suspend execution } } // Non-busy waiting signal() void signal(semaphore *S) { S->value++; if (S->value <= 0) { remove process P from S->list; wakeup(P); // Move to Ready queue } }
Key Implementation Details:
- Negative Semaphore Values: If
S->valueis negative, its absolute magnitude |S→value| indicates the exact number of processes currently blocked in the waiting queuelist. block()Operation: Suspends the calling process and transfers execution to the OS CPU scheduler.wakeup(P)Operation: Removes a process from the waiting list and moves it to the OS ready queue.- Atomicity Requirement:
wait()andsignal()must execute atomically using hardware support (compare_and_swapor interrupt masking).
3. Deadlocks and Starvation
Improper ordering of wait() and signal() operations leads to classic OS concurrency hazards:
Occurs when two or more processes are waiting indefinitely for an event that can only be caused by one of the waiting processes.
// S and Q initialized to 1 P0: P1: wait(S); wait(Q); wait(Q); wait(S); ... ... signal(S); signal(Q); signal(Q); signal(S); // Deadlock if P0 executes wait(S) and P1 executes wait(Q)!
Occurs when a process waits indefinitely inside a semaphore's waiting queue if the queue is managed using a LIFO (Last-In-First-Out) order or an un-bounded priority scheme.
Solution: Use FIFO (First-In-First-Out) ordering for the semaphore queue to guarantee bounded waiting.
4. Priority Inversion & Priority Inheritance
Priority Inversion occurs in systems with priority-based scheduling when a higher-priority process is indirectly preempted or delayed by a lower-priority process.
Sequence of Events:
- Low Priority Process (L) acquires semaphore lock S.
- High Priority Process (H) becomes ready and requests S. H blocks, waiting for L to finish.
- Medium Priority Process (M) becomes ready. M does not require S, so it preempts L (since M > L priority).
- Result: M runs while H waits! H is indirectly delayed by M, reversing priority expectations.
Under Priority Inheritance, any process accessing a resource needed by a higher-priority process temporarily inherits the higher priority until it releases the resource.
Example: Process L temporarily inherits the priority of H while holding lock S. This prevents Medium Process M from preempting L. Once L releases S, its priority reverts to low, and H immediately executes.
5. Interactive Semaphore Simulator
Simulate a Counting Semaphore (capacity initialized to 2) with blocking queues. Watch processes claim resources or get blocked when capacity reaches 0.