Overview
The Printer Control scenario simulates a print server managing multiple print jobs competing for a limited number of available printers. This demonstrates the use of counting semaphores to manage access to multiple identical resources.Real-World Problem
Consider an office with a shared print server:- 10 users submit print jobs
- Only 3 printers are available
- Jobs must wait if all printers are busy
- When a printer finishes, the next job in queue can start
N resources (printers) and M consumers (jobs) where M > N.
Shared Resources
The shared resources are multiple printer objects (a fixed pool). Each printer tracks its current owner, completed jobs, total pages printed, and status.
Synchronization Algorithm
This scenario uses a Counting Semaphore to manage the printer pool:Wait for Available Printer
Print job executes WAIT on the semaphore. If count > 0, it decrements and proceeds. If count = 0, it blocks until a printer is released.
Scenario Setup
printerScenario.js
Configuration Options
| Parameter | Description | Default |
|---|---|---|
jobCount | Number of print jobs (threads) | Varies |
printerCount | Number of available printers | Minimum 1 |
jobs | Array of job configurations with pages | 1 page per job |
Example Execution Flow
Thread Instructions
Each print job thread executes:- WAIT_SEM - Request a printer from the pool (blocks if none available)
- PRINT - Print the specified number of pages
- SIGNAL_SEM - Release the printer back to the pool
- END - Terminate thread
The semaphore count represents the number of available printers. When it reaches 0, new jobs must wait. Each SIGNAL operation makes one printer available for the next waiting job.
Semaphore vs Mutex
| Aspect | Mutex | Counting Semaphore |
|---|---|---|
| Resources | 1 (binary) | N (counting) |
| Use Case | Mutual exclusion | Resource pooling |
| Initial Value | Unlocked | Number of resources |
| Example | Bank account | Printer pool |
Key Learning Points
- Counting Semaphores: Track multiple identical resources
- Resource Pool: Jobs share access to N printers
- Blocking: Jobs wait when all printers are busy
- Fairness: Jobs typically served in FIFO order (implementation-dependent)
- Statistics: Each printer tracks its usage metrics independently