SCAN (elevator algorithm) in Operating Systems - Time & Space Complexity
We want to understand how the SCAN disk scheduling algorithm's running time changes as the number of disk requests grows.
Specifically, how does the number of requests affect the total work done by SCAN?
Analyze the time complexity of the following SCAN algorithm snippet.
function SCAN(requests, head, direction) {
sort requests in ascending order
while requests remain {
if direction is up {
service requests >= head in order
move head to max request
direction = down
} else {
service requests <= head in reverse order
move head to min request
direction = up
}
}
}
This code moves the disk head back and forth, servicing requests in order along the way.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing the sorted list of requests to service them in order.
- How many times: Each request is visited once during the head's movement in one direction.
As the number of requests increases, the algorithm must visit each request once while moving the head back and forth.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 moves to service all requests |
| 100 | About 100 moves to service all requests |
| 1000 | About 1000 moves to service all requests |
Pattern observation: The total work grows roughly in direct proportion to the number of requests.
Time Complexity: O(n)
This means the time to service all requests grows linearly with the number of requests.
[X] Wrong: "SCAN takes longer than linear time because the head moves back and forth multiple times."
[OK] Correct: Even though the head moves back and forth, each request is only serviced once, so the total work still grows linearly with the number of requests.
Understanding SCAN's time complexity helps you explain how disk scheduling balances efficiency and fairness, a useful skill in system design discussions.
What if the requests were not sorted before servicing? How would that affect the time complexity?
Practice
SCAN (elevator) disk scheduling algorithm?Solution
Step 1: Understand SCAN movement
SCAN moves the disk arm in one direction, serving all requests along the way until it reaches the last track in that direction.Step 2: Direction reversal
After reaching the end, the arm reverses direction and serves requests on the way back, similar to an elevator moving up and down.Final Answer:
The disk arm moves in one direction, servicing requests until it reaches the end, then reverses direction. -> Option BQuick Check:
SCAN = moves one way then reverses [OK]
- Confusing SCAN with FCFS (first-come, first-served)
- Thinking SCAN jumps directly to closest request
- Assuming SCAN moves randomly
Solution
Step 1: Identify SCAN's servicing order
SCAN moves the disk arm in one direction, servicing all requests in that path.Step 2: Direction reversal at end
When the arm reaches the last request or the disk end, it reverses direction and services requests on the return path.Final Answer:
It services requests by moving the arm in one direction, then reverses at the end. -> Option CQuick Check:
SCAN = one direction then reverse [OK]
- Confusing SCAN with SSTF (closest request first)
- Assuming SCAN ignores direction
- Thinking SCAN services requests randomly
Solution
Step 1: Identify current arm position and direction
The arm is at track 15 and moving towards higher tracks (upwards).Step 2: Find the closest request in the moving direction
Requests above 15 are 20, 22, and 40. The closest is track 20.Final Answer:
Track 20 -> Option AQuick Check:
SCAN services requests ahead in current direction first [OK]
- Choosing requests behind current arm position
- Ignoring arm's moving direction
- Picking the closest request overall instead of in direction
Solution
Step 1: Understand arm direction and position
The arm is at track 50 moving towards 0 (downwards).Step 2: Identify requests in arm's direction
Requests below 50 are 10, 20, 30, 40; above 50 is 60.Step 3: Find next request in direction
The closest request in the moving direction is track 40, not 60.Final Answer:
The arm will service track 40 next; the error is ignoring the arm's direction. -> Option DQuick Check:
SCAN respects arm direction when choosing requests [OK]
- Ignoring arm direction and picking closest request overall
- Assuming SCAN jumps to farthest request
- Confusing SCAN with SSTF
Solution
Step 1: Identify requests above current arm position
Arm at 50 moving up; requests above 50 are 62, 64, 95, 119, 123, 180.Step 2: Service requests upwards in order
Requests are serviced in ascending order: 62, 64, 95, 119, 123, 180.Step 3: Reach end of disk and reverse
SCAN continues to the last track (199) before reversing direction.Step 4: Service remaining requests on return
On return, service requests below 50 in descending order: 34, then 11.Final Answer:
[62, 64, 95, 119, 123, 180, 199, 34, 11] -> Option AQuick Check:
SCAN = up servicing, end, then down servicing [OK]
- Skipping the last track before reversing
- Not servicing requests in sorted order
- Ignoring requests on return path
Ready to prove it?
Pass the test and this pattern moves unknown
