Semaphore Introduction

A semaphore is a synchronization primitive used in operating systems to control access to shared resources by multiple processes or threads. It consists of an integer variable and a queue of waiting processes, providing a mechanism to solve critical section problems and prevent race conditions in concurrent systems.

Semaphores use two atomic operations: wait() (also called P() or down()) and signal() (also called V() or up()) to manage access to shared resources. The integer value represents the number of available resources, while the queue holds processes waiting for access.

How Semaphores Work

A semaphore S is initialized with a non-negative integer value and an empty queue of processes:

S ? (k, ?)  // where k ? 0 and ? is empty queue

Semaphore Operations

Wait Operation (P/down):

  • If semaphore value > 0, decrement it and allow process to continue

  • If semaphore value = 0, add process to waiting queue and block it

Signal Operation (V/up):

  • If waiting queue is empty, increment semaphore value

  • If waiting queue is not empty, remove one process and make it ready

Semaphore Operations Wait() Operation Is S.value > 0? Yes No S.value-- Continue Add to queue Block process Signal() Operation Is queue empty? Yes No S.value++ Remove from queue & wake

Types of Semaphores

Type Value Range Description
Binary Semaphore 0 or 1 Acts as a mutex for mutual exclusion
Counting Semaphore 0 to N Controls access to multiple instances of a resource
Strong Semaphore Any non-negative FIFO queue ordering prevents starvation
Weak Semaphore Any non-negative No specific queue ordering, may cause starvation

Example Producer-Consumer Problem

Consider a buffer with capacity 3, using semaphores for synchronization:

semaphore empty = 3;    // Available buffer slots
semaphore full = 0;     // Filled buffer slots  
semaphore mutex = 1;    // Mutual exclusion for buffer access

Producer() {
    wait(empty);        // Wait for empty slot
    wait(mutex);        // Enter critical section
    // Add item to buffer
    signal(mutex);      // Exit critical section
    signal(full);       // Signal item added
}

Consumer() {
    wait(full);         // Wait for filled slot
    wait(mutex);        // Enter critical section
    // Remove item from buffer
    signal(mutex);      // Exit critical section
    signal(empty);      // Signal slot available
}

Advantages

  • Flexible resource management Can handle multiple instances of resources

  • No busy waiting Blocked processes don't consume CPU cycles

  • Machine independent Portable synchronization mechanism

  • Prevents race conditions Ensures atomic access to critical sections

Disadvantages

  • Programming complexity Incorrect usage can lead to deadlocks

  • Priority inversion Low-priority processes may block high-priority ones

  • Proper sequencing required wait() and signal() must be called in correct order

Conclusion

Semaphores are essential synchronization tools that provide controlled access to shared resources in concurrent systems. While they offer flexible resource management and prevent race conditions, careful programming is required to avoid deadlocks and ensure correct operation.

Updated on: 2026-03-17T09:01:38+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements