Some resources have multiple identical instances (e.g., 3 printers, 5 buffer slots). Binary semaphore can’t handle this — we need to track how many instances are available.
A counting semaphore can take values 0 to N, where N is the number of available resources in the pool.
- Initialize semaphore to N (number of available resources)
- Process requests resource:
wait(S)→ S— - If S becomes negative, process blocks (no resources available)
- Process releases:
signal(S)→ S++, wake up a blocked process
- Value range: 0 to N (N = resource count)
- Used for resource management (buffer slots, printer pool)
- Negative value = number of blocked processes
- Classic use: producer-consumer bounded buffer
- Built from: Semaphore, Producer-Consumer Problem
- Builds into: Reader-Writer Problem
- Related: Binary Semaphore
- Contrasts with: Mutex (mutex is just binary semaphore)
- Initializing to wrong N causes resource leaks or errors
- Must ensure signal() is called exactly once per wait()
- Priority inversion can happen with multiple priority levels