Overview
The Robot Station scenario simulates two robots that need to use a shared workstation to process parts. This demonstrates Peterson’s Algorithm, a classic software-only solution for mutual exclusion between exactly two threads without hardware support.Real-World Problem
Consider a manufacturing setup where:- Two robots need to use the same precision workstation
- Only one robot can use the station at a time
- No hardware locks available, must coordinate in software
- Both robots need multiple cycles of access
Shared Resources
The shared resources are:
- Workstation - Can only be used by one robot at a time
- Peterson Lock - Software lock using flags and turn variable
- Usage statistics - Tracks how many times each robot used the station
Synchronization Algorithm
Peterson’s Algorithm uses two arrays and one turn variable:Scenario Setup
petersonScenario.js
Configuration Options
| Parameter | Description | Default |
|---|---|---|
cyclesPerRobot | Number of times each robot uses the station | Minimum 1 |
Peterson’s Algorithm is designed for exactly 2 threads. The scenario always creates 2 robots regardless of configuration. Generalizing to N threads requires different algorithms (like Lamport’s Bakery Algorithm).
Peterson’s Algorithm Pseudocode
Example Execution Flow
Thread Instructions
Each robot thread executes (repeated for each cycle):- PETERSON_LOCK - Execute Peterson’s entry protocol
- USE_STATION - Use the workstation (critical section)
- PETERSON_UNLOCK - Execute Peterson’s exit protocol
- After all cycles: END - Terminate thread
Why Peterson’s Algorithm Works
Mutual Exclusion
Progress (Deadlock-Free)
Bounded Waiting (Starvation-Free)
Comparison with Hardware Solutions
| Aspect | Peterson’s | Mutex (Hardware) |
|---|---|---|
| Hardware Support | None needed | Atomic instructions |
| Thread Count | Exactly 2 | Arbitrary |
| Performance | Busy-waiting | Can sleep/wake |
| Complexity | Moderate | Simple API |
| Portability | Requires memory ordering | Hardware-dependent |
Key Learning Points
- Software-Only Solution: No special hardware instructions needed
- Two Threads Only: Algorithm designed specifically for N=2
- Busy-Waiting: Threads actively check flag in a loop (spinlock)
- Memory Ordering: Requires proper memory fence/barrier in real systems
- Historical Significance: Proves mutual exclusion possible without hardware
- Fairness: The “turn” variable prevents starvation
Modern Considerations
Important: Peterson’s Algorithm relies on sequential consistency of memory operations. On modern processors with out-of-order execution and caching, you need memory barriers to ensure correctness. Most production code uses hardware-supported primitives instead.
Common Issues
Educational Value
Peterson’s Algorithm is rarely used in production but is pedagogically valuable:- Demonstrates that mutual exclusion is possible in software
- Shows the complexity of lock-free programming
- Illustrates memory model considerations
- Provides foundation for understanding modern synchronization
- Proves that hardware atomics are not strictly necessary
Extensions
| Algorithm | Threads | Fairness |
|---|---|---|
| Peterson’s | 2 | Fair |
| Filter Lock | N | Fair (FIFO) |
| Bakery Algorithm | N | Fair (ticket-based) |
| Tournament Tree | N (power of 2) | Fair |