Compare binary semaphores (mutex) versus counting semaphores (resource pools) — their use cases, behavior, and when to use each.
| Feature | Binary Semaphore | Counting Semaphore |
|---|---|---|
| Values | 0 or 1 only | 0 to N (N = resource count) |
| Use Case | Mutual exclusion (critical section) | Resource pool management |
| Initial Value | 1 (unlocked) | N (available resources) |
| Negative Value | Never negative | Negative = number of waiters |
| Example | File lock, mutex | Buffer slots, printer pool |
- Binary semaphore = mutex — ensures only one process enters critical section
- Counting semaphore = resource counter — tracks pool of identical resources
- Both use same wait()/signal() operations — the difference is initialization and interpretation
- Binary semaphores can be implemented with counting (just set N=1), but not vice versa
- Classic use: producer-consumer — uses counting (empty, full) + binary (mutex)
- Priority inversion affects both types — high-priority process blocked by lower-priority holder
Binary semaphores are a special case of counting semaphores (N=1). Use binary for mutual exclusion, counting for resource pools. The producer-consumer problem elegantly combines both: counting semaphores (empty, full) manage buffer slots, while a binary semaphore (mutex) protects the buffer data structure.
- Semaphore — the general concept
- Binary Semaphore — mutex use case
- Counting Semaphore — resource pool use case
- Producer-Consumer Problem — uses both types
- Mutex — binary semaphore synonym
- wait() Operation — used by both
- signal() Operation — used by both