Elevator Scheduling Algorithms: FCFS, SSTF, SCAN, and LOOK
Saloni Agarwal
Posted on October 27, 2024
Since I’ve been working with Go for quite some time, I thought it would be a fun challenge to implement a few classic low-level design solutions in it.
When designing an elevator system, one crucial aspect is how to decide which floor to service next, especially when the elevator has multiple requests. Go’s straightforward syntax and performance make it ideal for modeling such systems, so I set out to create basic implementations of FCFS (First Come First Serve), SSTF (Shortest Seek Time First), SCAN, and LOOK algorithms.
1. First Come First Serve (FCFS)
I started with the simplest approach: service requests in the order they’re received. It’s easy to implement but can be inefficient if the requests are spread out across floors, leading to more travel time.
In FCFS, the elevator simply moves to each requested floor in the given order.
2. Shortest Seek Time First (SSTF)
SSTF tries to minimize travel by choosing the closest requested floor next. This reduces travel time but can lead to "starvation" for far-off requests if new closer requests keep coming.
This function finds the closest floor to the current floor each time, updating the elevator’s position after each move.
3. SCAN (Elevator Algorithm)
In SCAN, the elevator moves in one direction, servicing all requests in that direction until it reaches the end, then reverses. This approach is more fair than SSTF because it reduces starvation.
This function splits requests into floors above and below the current position. It serves all floors upwards, then downwards.
4. LOOK
LOOK is a slight variation of SCAN. Instead of going all the way to the end, the elevator reverses direction at the last request in each direction. It saves time by stopping where the requests end, not at the physical limits.
Welcome to the Low-Level System Design in Go repository! This repository contains various low-level system design problems and their solutions implemented in Go. The primary aim is to demonstrate the design and architecture of systems through practical examples.
Low-level system design involves understanding the core concepts of system architecture and designing scalable, maintainable, and efficient systems. This repository will try to cover solutions of various problems and scenarios using Go.
Parking Lot System
The first project in this repository is a Parking Lot System. This system simulates a parking lot where vehicles can be parked and unparked. It demonstrates:
Singleton design pattern for managing the parking lot instance.
Handling different types of vehicles (e.g., cars, trucks).