Sub topic 5
FCFS and SSTF Disk Scheduling
This chapter assumes that you know what a process, system call, kernel, and interrupt are. You should also be comfortable with absolute differences and simple arithmetic. We measure disk movement in cylinders.
Why scheduling is needed
A disk request contains a cylinder number that the disk head must visit. If several requests are waiting, the operating system chooses their service order. A good order reduces head movement, which reduces seek time.
For a request at cylinder r and a current head position h, the movement is:
|r - h|
The total movement is the sum of the movement for every request.
FCFS scheduling
First-Come, First-Served (FCFS) handles requests in their arrival order. It is simple and fair because no request is deliberately moved ahead of an earlier request.
Its weakness is that nearby requests may be separated by distant requests. This can cause unnecessary head movement.
Consider this queue:
98, 183, 37, 122, 14, 124, 65, 67
The initial head position is 53.
| Step | Movement | Distance | Column | Column |
|---|---|---|---|---|
| 53 → 98 | ` | 98 - 53 | ` | 45 |
| 98 → 183 | ` | 183 - 98 | ` | 85 |
| 183 → 37 | ` | 37 - 183 | ` | 146 |
| 37 → 122 | ` | 122 - 37 | ` | 85 |
| 122 → 14 | ` | 14 - 122 | ` | 108 |
| 14 → 124 | ` | 124 - 14 | ` | 110 |
| 124 → 65 | ` | 65 - 124 | ` | 59 |
| 65 → 67 | ` | 67 - 65 | ` | 2 |
The total is:
45 + 85 + 146 + 85 + 108 + 110 + 59 + 2 = 640 cylinders
SSTF scheduling
Shortest Seek Time First (SSTF) selects the pending request closest to the current head position. It is a greedy algorithm: at each step, it chooses the locally shortest movement.
Starting at 53, the order is:
65, 67, 37, 14, 98, 122, 124, 183
The distances are:
12 + 2 + 30 + 23 + 84 + 24 + 2 + 59 = 236 cylinders
SSTF reduces movement in this example, but it can cause starvation. Starvation means that a request waits for an unbounded or very long time because closer requests keep arriving. A request far from the current head is especially vulnerable.
Comparing FCFS and SSTF
| Algorithm | Main rule | Strength | Main risk |
|---|---|---|---|
| FCFS | Serve arrival order | Fair and simple | High total movement |
| SSTF | Serve nearest request | Usually lowers seek movement | Starvation and less fairness |
When two requests are equally close under SSTF, the implementation needs a tie-breaking rule. A fixed rule, such as choosing the older request, makes the result reproducible.
def fcfs(head, requests):
total = 0
for request in requests:
total += abs(request - head)
head = request
return total
def sstf(head, requests):
pending = list(requests)
total = 0
while pending:
request = min(pending, key=lambda r: abs(r - head))
total += abs(request - head)
head = request
pending.remove(request)
return total
queue = [98, 183, 37, 122, 14, 124, 65, 67]
print(fcfs(53, queue)) # 640
print(sstf(53, queue)) # 236Exercise
With the head at cylinder 50 and pending requests [10, 40, 55], what request does SSTF choose first, and how far does the head move?
Show answer
It chooses cylinder 55 because it is 5 cylinders away. The head moves 5 cylinders.