Difference Between Mutex and Semaphore
Mutex vs semaphore compared — ownership, counting, and use cases — with examples and operating systems interview questions answered.
Expected Interview Answer
A mutex is a locking primitive owned by exactly the thread that acquires it and enforces mutual exclusion over a single resource, while a semaphore is a counter that can permit a configurable number of concurrent accessors and carries no notion of ownership.
A mutex has a binary locked/unlocked state, and only the thread that locked it is allowed to unlock it — most implementations will error or deadlock if another thread tries. A semaphore, by contrast, holds an integer count of available units: any thread can call wait or signal on it regardless of which thread last touched it, which makes semaphores suitable for signaling between threads or rationing access to N interchangeable resources (like a pool of database connections). Because a mutex has ownership, it naturally prevents accidental unlocking by the wrong thread, whereas a semaphore’s flexibility makes it powerful for producer-consumer patterns but easier to misuse if signal calls are mismatched. Choosing between them comes down to whether you need strict single-owner locking (mutex) or counting/signaling across threads (semaphore).
- Mutex: ownership prevents wrong-thread unlocking
- Semaphore: models a pool of N interchangeable resources
- Semaphore: usable for cross-thread signaling, not just locking
- Mutex: simpler reasoning for single-resource critical sections
AI Mentor Explanation
A mutex is like a single team locker room key that only the player who took it can return — a security guard refuses to accept it back from anyone else. A semaphore is like a rack of five practice bats shared by the whole squad: any player can take one (decrement) or return one (increment), and the coach does not track who returned which bat. The mutex enforces strict single-owner locking; the semaphore rations a pool without caring about ownership.
Step-by-Step Explanation
Step 1
Ownership
Mutex: only the locking thread may unlock it. Semaphore: any thread can wait or signal.
Step 2
Value range
Mutex: binary locked/unlocked. Semaphore: integer count, from 0 up to a configured maximum.
Step 3
Use case
Mutex: protecting a single critical section. Semaphore: rationing N interchangeable resources or signaling between threads.
Step 4
Misuse risk
Mutex: wrong-thread unlock is typically an error. Semaphore: mismatched wait/signal calls can leak or over-grant access.
What Interviewer Expects
- Ownership as the defining distinction between mutex and semaphore
- Binary vs counting value semantics
- A correct use-case example for each (locking vs pooling)
- Awareness that misuse of either causes concurrency bugs
Common Mistakes
- Saying mutex and semaphore are interchangeable in all cases
- Claiming a semaphore always has a value of 0 or 1
- Forgetting that a mutex has ownership and a semaphore does not
- Not being able to give a concrete example of a counting semaphore use
Best Answer (HR Friendly)
“A mutex is like a single lock that only the person who locked it can open again, protecting one resource at a time. A semaphore is more like a counter for a shared pool — any thread can take a slot or give one back, which makes it useful for rationing a fixed number of resources or signaling between threads.”
Code Example
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
sem_t pool; /* counting semaphore for a pool of 3 resources */
sem_init(&pool, 0, 3);
void critical_section(void) {
pthread_mutex_lock(&lock); /* only THIS thread may unlock it */
/* ... exclusive access to shared data ... */
pthread_mutex_unlock(&lock);
}
void use_pooled_resource(void) {
sem_wait(&pool); /* any thread may wait/signal */
/* ... up to 3 threads run here concurrently ... */
sem_post(&pool);
}Follow-up Questions
- What is a binary semaphore and how does it differ from a mutex?
- Can a semaphore be signaled by a thread that never called wait?
- What is priority inversion and how does a mutex relate to it?
- How would you implement a mutex using a semaphore?
MCQ Practice
1. What is the key distinguishing feature of a mutex?
A mutex is owned by the thread that locked it; that same thread must be the one to unlock it.
2. A counting semaphore is best suited for?
A counting semaphore tracks how many of a fixed pool of resources are free, allowing up to N concurrent holders.
3. Which of these can be signaled by any thread, regardless of who last waited on it?
Semaphores carry no ownership, so any thread may call wait or signal on them.
Flash Cards
Mutex vs semaphore in one line? — Mutex: owned lock for one resource. Semaphore: unowned counter for N resources.
Who can unlock a mutex? — Only the thread that originally locked it.
When would you pick a semaphore over a mutex? — When rationing a pool of N interchangeable resources or signaling across threads.
Does a semaphore track ownership? — No — any thread can call wait or signal on it.