100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is a Semaphore?

Learn what a semaphore is — wait and signal, counting vs binary, and semaphore vs mutex — with examples and operating systems interview questions.

mediumQ4 of 224 in Operating Systems Est. time: 6 minsLast updated:
Open Code Lab

Expected Interview Answer

A semaphore is a synchronization primitive — an integer counter with atomic wait (P/down) and signal (V/up) operations — used to control access to shared resources by multiple processes or threads.

A counting semaphore holds a value representing the number of available units of a resource: wait decrements it and blocks if the count is zero, while signal increments it and may wake a waiting thread. A binary semaphore is restricted to 0 and 1, so it behaves like a lock for mutual exclusion, though unlike a mutex it has no notion of ownership. Because the counter operations are atomic, semaphores solve classic problems such as producer-consumer and reader-writer while preventing race conditions — but careless ordering of waits can itself cause deadlock.

  • Controls concurrent access to a limited pool of resources
  • Counting semaphores allow N simultaneous holders
  • Binary semaphores provide simple mutual exclusion
  • Atomic operations prevent race conditions on shared state

AI Mentor Explanation

A semaphore is like a fixed number of practice nets at a training ground: the count starts at the number of free nets, each batter waits and takes one (decrementing the count) and signals when done (incrementing it). If all nets are taken, the next batter waits until one is freed. A binary version is a single net acting as a simple in-use flag — exactly how a counting semaphore rations a limited resource among many.

Step-by-Step Explanation

  1. Step 1

    Initialize the counter

    Set the semaphore to the number of available resource units (1 for a binary semaphore).

  2. Step 2

    wait / P / down

    Decrement the counter; if it drops below zero the caller blocks until a unit is free.

  3. Step 3

    signal / V / up

    Increment the counter; if threads are waiting, one is woken to proceed.

  4. Step 4

    Atomicity

    wait and signal execute atomically, so concurrent updates never corrupt the count.

What Interviewer Expects

  • Semaphore as an atomic counter with wait/signal
  • Difference between counting and binary semaphores
  • That a binary semaphore lacks ownership, unlike a mutex
  • A use case such as producer-consumer or resource pooling

Common Mistakes

  • Treating a semaphore and a mutex as identical
  • Forgetting that wait/signal must be atomic
  • Thinking a semaphore always means mutual exclusion (ignoring counting semaphores)
  • Ordering waits so that the semaphores themselves cause deadlock

Best Answer (HR Friendly)

A semaphore is a counter that controls how many tasks can use a shared resource at once. Tasks decrement it to take a slot and increment it to release one, waiting when none are free. A binary version acts like a simple lock, while a counting version lets a fixed number of tasks share the resource together.

Code Example

Counting semaphore limiting concurrent access to 3
import threading

# allow at most 3 threads into the pool at once
pool = threading.Semaphore(3)

def use_resource(name):
    pool.acquire()          # wait / P: decrement, block if count is 0
    try:
        # ... at most 3 threads run this section concurrently ...
        pass
    finally:
        pool.release()      # signal / V: increment, wake a waiter

Follow-up Questions

  • What is the difference between a semaphore and a mutex?
  • How do semaphores solve the producer-consumer problem?
  • What is the difference between a binary and a counting semaphore?
  • Can semaphores cause deadlock, and how?

MCQ Practice

1. A binary semaphore can take which values?

A binary semaphore is restricted to 0 and 1, making it behave like a lock for mutual exclusion.

2. The wait (P) operation on a semaphore does what?

wait decrements the count and blocks the caller if no resource unit is available.

3. A key difference between a semaphore and a mutex is that a mutex has?

A mutex is owned by the thread that locked it and only that thread unlocks it; a semaphore has no ownership.

Flash Cards

What is a semaphore?An atomic integer counter with wait (P) and signal (V) operations that controls access to shared resources.

Counting vs binary semaphore?Counting allows N concurrent holders; binary is limited to 0/1 and acts like a lock.

Semaphore vs mutex?A mutex has ownership (only the locker unlocks); a semaphore does not and can count many units.

What do wait and signal do?wait decrements (blocks if 0); signal increments and wakes a waiting thread.

1 / 4

Continue Learning