0
0
Operating Systemsknowledge~6 mins

Semaphores (counting and binary) in Operating Systems - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine many people trying to use a single bathroom or a limited number of parking spots. Without a way to control access, chaos and conflicts happen. Semaphores help manage access to shared resources so that multiple processes or threads don't interfere with each other.
Explanation
What is a Semaphore
A semaphore is a tool used in operating systems to control access to shared resources by multiple processes. It keeps a count that represents how many units of the resource are available or how many processes can enter a critical section. Processes must check the semaphore before proceeding to ensure safe access.
Semaphores act as counters that regulate how many processes can access a resource at the same time.
Counting Semaphore
A counting semaphore holds a value that can be any non-negative integer. It is used when there are multiple identical resources available, like several printers or parking spots. Each time a process uses a resource, the semaphore decreases; when a resource is freed, the semaphore increases.
Counting semaphores track multiple available resources by counting how many are free.
Binary Semaphore
A binary semaphore can only have two values: 0 or 1. It is like a simple lock that allows only one process to access a resource at a time. When the value is 1, the resource is free; when 0, it is taken. This is useful for protecting critical sections where only one process should enter.
Binary semaphores act like on/off switches to allow exclusive access to a resource.
How Semaphores Work
Processes use two main operations on semaphores: wait (also called P or down) and signal (also called V or up). The wait operation decreases the semaphore if it is positive or makes the process wait if zero. The signal operation increases the semaphore and may wake waiting processes. This coordination prevents conflicts.
Wait and signal operations on semaphores coordinate safe access to shared resources.
Real World Analogy

Imagine a parking lot with a limited number of spots. A counting semaphore is like a gatekeeper who keeps track of how many spots are free and lets cars in only if there is space. A binary semaphore is like a single bathroom key that only one person can hold at a time to enter.

What is a Semaphore → A gatekeeper controlling entry to a parking lot
Counting Semaphore → The gatekeeper counting how many parking spots are free
Binary Semaphore → A single bathroom key that only one person can hold
How Semaphores Work → Cars waiting for a spot and leaving when done, with the gatekeeper updating availability
Diagram
Diagram
┌───────────────────────────────┐
│          Semaphore             │
│ ┌───────────────┐             │
│ │ Counting      │             │
│ │ Semaphore     │             │
│ │ Value: n ≥ 0  │             │
│ └───────────────┘             │
│ ┌───────────────┐             │
│ │ Binary        │             │
│ │ Semaphore     │             │
│ │ Value: 0 or 1 │             │
│ └───────────────┘             │
│                               │
│ Wait (P) ↓                    │
│ Signal (V) ↑                  │
└───────────────────────────────┘
Diagram showing the two types of semaphores with their value ranges and the wait/signal operations.
Key Facts
SemaphoreA synchronization tool that controls access to shared resources by multiple processes.
Counting SemaphoreA semaphore with a value that can be zero or more, representing multiple available resources.
Binary SemaphoreA semaphore with only two values, 0 or 1, used for exclusive access.
Wait (P) OperationDecreases the semaphore value or blocks the process if the value is zero.
Signal (V) OperationIncreases the semaphore value and may wake a waiting process.
Common Confusions
Thinking binary semaphores are only for locking files or devices.
Thinking binary semaphores are only for locking files or devices. Binary semaphores are general-purpose locks used to protect any critical section, not just files or devices.
Believing counting semaphores can have negative values.
Believing counting semaphores can have negative values. Semaphore values never go negative; if zero, processes wait until the value increases.
Confusing semaphores with mutexes as the same thing.
Confusing semaphores with mutexes as the same thing. Mutexes are a special type of binary semaphore with ownership and priority rules, while semaphores are more general.
Summary
Semaphores help manage access to shared resources by multiple processes to avoid conflicts.
Counting semaphores track how many identical resources are free, while binary semaphores allow exclusive access.
Processes use wait and signal operations on semaphores to coordinate safe resource usage.