Sub topic 6
FCFS and SSTF scheduling
This chapter covers the two simplest disk scheduling policies, First-Come First-Served (FCFS) and Shortest Seek Time First (SSTF). You will trace both by hand, compute total head movement, and see why SSTF can starve a request. I assume you know the disk geometry and the access-time formula from the previous two chapters. That means a cylinder is the set of tracks at one arm position, and seek time dominates the cost of a request.
Why the order of requests matters
A disk can serve only one request at a time, and moving the arm between cylinders is the slowest part of that work. While the disk is busy, other processes queue more requests. The scheduler picks which pending request to serve next. Serving them in a different order can reduce the total distance the arm travels, and so reduce total seek time.
GATE questions measure this cost as total head movement. It is the sum of the absolute cylinder differences between consecutive stops, starting from the current head position. If seek time is proportional to distance, total head movement is proportional to total seek time.
All examples use one classic queue. The head starts at cylinder 53, and the pending requests, in arrival order, are:
98, 183, 37, 122, 14, 124, 65, 67
FCFS
FCFS serves requests in arrival order. It needs no sorting, and it is fair because no request waits behind a later arrival.
| Move | Distance |
|---|---|
| 53 to 98 | 45 |
| 98 to 183 | 85 |
| 183 to 37 | 146 |
| 37 to 122 | 85 |
| 122 to 14 | 108 |
| 14 to 124 | 110 |
| 124 to 65 | 59 |
| 65 to 67 | 2 |
| **Total** | **640** |
The arm swings back and forth across the disk. For example, the move from 122 to 14 and back out to 124 wastes distance, and cylinders 65 and 67 sit next to each other but are served last.
SSTF
SSTF always serves the pending request closest to the current head position. This is a greedy rule, so it makes the best local choice at each step without looking ahead.
- From 53 the nearest request is 65 (distance 12).
- From 65 the nearest is 67 (distance 2).
- From 67 the nearest is 37 (distance 30), because 98 is 31 away.
- From 37 the nearest is 14 (distance 23).
- From 14 the nearest remaining is 98 (distance 84).
- From 98 the nearest is 122 (24), then 124 (2), then 183 (59).
The total is 12 + 2 + 30 + 23 + 84 + 24 + 2 + 59 = 236. That is about 63% less movement than FCFS on the same queue.
Code to check your traces
This program computes the service order and the total movement for both policies.
def total_movement(head, order):
total = 0
for cyl in order:
total += abs(cyl - head)
head = cyl
return total
def fcfs_order(head, requests):
return list(requests)
def sstf_order(head, requests):
pending = list(requests)
order = []
while pending:
# min() returns the first minimum, so ties go to the
# request that arrived earlier in the list.
nxt = min(pending, key=lambda r: abs(r - head))
order.append(nxt)
pending.remove(nxt)
head = nxt
return order
queue = [98, 183, 37, 122, 14, 124, 65, 67]
for name, fn in (("FCFS", fcfs_order), ("SSTF", sstf_order)):
order = fn(53, queue)
print(name, order, total_movement(53, order))
# FCFS [98, 183, 37, 122, 14, 124, 65, 67] 640
# SSTF [65, 67, 37, 14, 98, 122, 124, 183] 236Edge cases and common errors
- Count the first move. The total includes the distance from the starting head position to the first request. Forgetting it is the most common arithmetic slip.
- Recompute nearest at every step. SSTF is not a sort by cylinder number. The nearest request depends on where the head is now, so a request below the head can be served before one above it, as 37 was served before 98.
- Ties. If two requests are equally far, such as 40 and 60 from head 50, the rule does not decide. GATE questions normally avoid ties or state a tie-break. In practice many implementations pick the earlier arrival or the lower cylinder, so state your assumption.
- Do not add the return trip. Neither policy moves the head after the last request. The total ends at the final stop.
- Request lists are snapshots. The traces above assume no new requests arrive. A real system keeps receiving them, which is what causes the problem below.
Starvation under SSTF
SSTF is not fair. Suppose the head is near cylinder 50 and a request for cylinder 190 is pending. If processes keep issuing requests for cylinders 45 to 60, each new request is closer than 190, so SSTF keeps choosing them. The request at 190 waits as long as the stream continues. This is starvation: a request is postponed indefinitely even though it is valid.
FCFS cannot starve a request, because the wait is bounded by the requests that arrived before it. The cost is higher total movement.
| Property | FCFS | SSTF |
|---|---|---|
| Selection rule | Oldest request | Closest request to the head |
| Total head movement | Often high | Usually much lower |
| Overhead | Queue only | Search for minimum each step |
| Starvation possible | No | Yes, for distant requests |
| Optimal total movement | Not guaranteed | Not guaranteed |
Note the last row. SSTF is greedy, so it can miss the best total. For head 53 and requests 40 and 70, it goes to 70 only if 70 is closer, and it may end up crossing the disk twice. It is better than FCFS on average, but it is not an optimal schedule. SSTF also tends to favor the middle cylinders, because a head near the center has requests close by on both sides.
The next chapter covers SCAN and its variants, which keep SSTF's low movement while bounding the wait.
Exercise
The head is at cylinder 50. The pending queue, in arrival order, is 82, 170, 43, 140, 24, 16, 190. Compute total head movement under FCFS and under SSTF.
Show answer
FCFS: 32 + 88 + 127 + 97 + 116 + 8 + 174 = 642. SSTF order is 43, 24, 16, 82, 140, 170, 190, with moves 7 + 19 + 8 + 66 + 58 + 30 + 20 = 208.
Exercise
A disk has 5000 cylinders and a seek takes 0.02 ms per cylinder crossed. For the chapter's example queue starting at 53, what is the total seek time under FCFS and under SSTF, ignoring rotation and transfer?
Show answer
FCFS: 640 cylinders x 0.02 ms = 12.8 ms. SSTF: 236 x 0.02 ms = 4.72 ms. The disk size does not matter here, because only the distance travelled counts.
Exercise
Why can SSTF starve a request but FCFS cannot? Give a concrete arrival pattern.
Show answer
SSTF always picks the closest request, so a distant request loses to every nearer request that arrives. If the head is near cylinder 50 and new requests for cylinders 45 to 60 keep arriving, a request for cylinder 190 is never chosen. FCFS serves in arrival order, so a request waits only for the requests that arrived before it.