Using multithreading and Real-Time Operating System (RTOS) concepts, this project implements a complex simulation of a building's elevator system. The system demonstrates process management, inter-process communication (IPC), and thread synchronization in C++.
The project is architected as a multi-process system where distinct components run as separate processes, communicating via IPC mechanisms. This design simulates a distributed control system typical of real-world elevator arrays.
The system consists of three main process types:
- Dispatcher Process: The central brain.
- Elevator Processes: Autonomous controllers for each physical elevator.
- IO Process: Interactive visualization and input handler.
The Dispatcher is the parent process responsible for:
- Process Creation: Spawning the Elevator and IO processes.
- Algorithm: Implementing the scheduling logic (in
algofunction) to assign floor requests to the most suitable elevator based on proximity, direction, and idle status. - Coordination: Monitoring the state of all elevators and routing commands from the IO system.
Each Elevator runs as an independent process (elevator1.exe, elevator2.exe). They are responsible for:
- State Management: Tracking current floor, direction, door status, and service mode (fault/no-fault).
- Movement Logic: Simulating travel between floors, handling door operations, and managing a local queue of destinations.
- Synchronization: Signaling completion of tasks to the Dispatcher.
The IO Process handles user interaction and visualization:
- Visualization: Renders the dynamic ASCII art state of the elevators (position, doors, status) to the console.
- Input Handling: Captures keyboard commands (manual floor requests, fault injection, passenger simulation) and validates them using Regex.
- Communication: Pipes validated commands to the Dispatcher.
The simulation relies heavily on rt.h (a custom RTOS wrapper) to implement advanced system programming concepts:
-
Inter-Process Communication (IPC):
- Pipelines: Used for streaming user commands from the IO process to the Dispatcher.
- Mailboxes: Message queues used by the Dispatcher to send commands (e.g., "Go to Floor 5") to specific Elevators.
- Datapools (Shared Memory): Used for low-latency status sharing between processes.
-
Synchronization:
- Semaphores: Coordinate producer-consumer relationships (e.g., waiting for new commands).
- Mutexes: Protect shared resources like the console output to prevent text collision (
consolemutex). - Monitors: Encapsulate shared data and synchronization logic (e.g., the
elevatorclass inelevator.h).
-
Multithreading:
- Inside the processes, individual threads manage distinct tasks like screen updates, input polling, and logic processing (e.g.,
e1update,e2update,algothreads in Dispatcher).
- Inside the processes, individual threads manage distinct tasks like screen updates, input polling, and logic processing (e.g.,
- Manual Control: Input commands to send elevators to specific floors.
- Auto Mode: Generates dynamic "passenger" objects that make random requests, testing the scheduler's efficiency.
- Fault Injection: Can simulate mechanical faults to test error handling and service isolation.
Project done in a pair-programming setting using Visual Studio LiveShare.
