Lesson 5 · Storage and I/O

LOOK Disk Scheduling: Turn at the Last Request

Move the head in its current direction, serve each request it reaches, and reverse as soon as nothing is left ahead instead of running on to the end of the disk.

LOOK Disk Scheduling: Turn at the Last Request concept diagramA visual explanation of the layout and operations shown in this lesson.like SCAN, but turns at the last request rather than the disk edge05010015019914376594122140183cylinder82928184314623head 57total head movement: 295 cylinders
1

How the LOOK Algorithm Works

  1. Split the requests into those ahead of the head and those behind it
  2. Sort the ones ahead in the direction of travel
  3. Serve them in that order
  4. When none are left ahead, reverse
  5. Serve the rest in the new direction, nearest first

The LOOK disk scheduling algorithm moves the read/write head in one direction and serves each pending request as it reaches it. Before each move it looks further along that direction. When nothing is waiting there, it reverses and serves the rest on the way back.

It is SCAN without the empty run to cylinder 0 or the last cylinder. The head only travels between the lowest and highest requests in the queue, so the disk size never enters the sum.

  • Needs the head position, the direction and the queue
  • A request at the head's own cylinder costs 0 and is served at once
  • The answer changes with the starting direction
2

LOOK Disk Scheduling Example: 295 Cylinders

This is the queue every disk scheduling lesson here uses: requests 94, 183, 37, 122, 14, 140, 65 on a disk of cylinders 0 to 199, head at 57 and moving up.

Going up, the head serves 65, 94, 122, 140 and 183: 8 + 29 + 28 + 18 + 43 = 126. Nothing waits above 183, so it reverses there. Going down it serves 37 (146) and 14 (23). Total head movement: 295 cylinders. Average seek per request: 295 / 7 ≈ 42.1.

Had the head been moving down at 57, the order would be 37, 14, then 65 up to 183: 20 + 23 + 51 + 29 + 28 + 18 + 43 = 212. Always state the direction before working the answer; it is part of the question.

  • Order moving up: 65, 94, 122, 140, 183, 37, 14
  • Up: 126, then one long move of 146, then 23
  • Moving down first: 212, because the short side is served first
3

LOOK vs SCAN

Both serve the same requests in the same order. The only difference is where the head turns. SCAN continues from 183 to cylinder 199 and comes back, so it pays 16 cylinders twice: 327 against LOOK's 295.

That gives a quick check for any answer: the saving is twice the gap between the outermost request and the disk edge in the starting direction. If a request sits at cylinder 199 the gap is 0 and the two totals match.

The gap can be most of the trip. With the head at 57 moving up and only 37 and 14 waiting, LOOK reverses at once and moves 43 cylinders. SCAN still goes up to 199 and back: 327 for two requests.

LOOK vs SCAN on the head-57 queue, moving up
SCANLOOK
Turns atCylinder 199, the disk edge183, the highest request
Ends at1414
Total head movement327295
Needs the disk sizeYesNo
Longest possible waitAbout two full-disk sweepsAbout two sweeps of the request range
  • Same order, different turning point
  • Saving = 2 × (edge − outermost request)
  • Equal totals when a request sits on the edge
Implementation

LOOK on the head-57 queue

def look(requests, head):
    above = sorted(c for c in requests if c >= head)
    below = sorted((c for c in requests if c < head), reverse=True)
    # No disk edge: the head turns at the outermost pending request.
    order = above + below
    total, position = 0, head
    for cylinder in order:
        total += abs(cylinder - position)
        position = cylinder
    return order, total


order, total = look([94, 183, 37, 122, 14, 140, 65], 57)
print(*order, "|", total, "cylinders")
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
using namespace std;

// Returns total head movement in cylinders; `order` receives the service order.
int look(const vector<int>& requests, int head, vector<int>& order) {
    // No disk edge: the head turns at the outermost pending request.
    vector<int> above, below;
    for (int c : requests) (c >= head ? above : below).push_back(c);
    sort(above.begin(), above.end());
    sort(below.rbegin(), below.rend());
    for (int c : above) order.push_back(c);
    for (int c : below) order.push_back(c);
    int total = 0, position = head;
    for (int cylinder : order) {
        total += abs(cylinder - position);
        position = cylinder;
    }
    return total;
}

int main() {
    vector<int> requests = {94, 183, 37, 122, 14, 140, 65}, order;
    int total = look(requests, 57, order);
    for (int c : order) cout << c << " ";
    cout << "| " << total << " cylinders" << endl;
    return 0;
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    // Returns total head movement in cylinders; `order` receives the service order.
    static int look(int[] requests, int head, List<Integer> order) {
        // No disk edge: the head turns at the outermost pending request.
        List<Integer> above = new ArrayList<>(), below = new ArrayList<>();
        for (int c : requests) (c >= head ? above : below).add(c);
        Collections.sort(above);
        below.sort(Collections.reverseOrder());
        order.addAll(above);
        order.addAll(below);
        int total = 0, position = head;
        for (int cylinder : order) {
            total += Math.abs(cylinder - position);
            position = cylinder;
        }
        return total;
    }

    public static void main(String[] args) {
        int[] requests = {94, 183, 37, 122, 14, 140, 65};
        List<Integer> order = new ArrayList<>();
        int total = look(requests, 57, order);
        StringBuilder line = new StringBuilder();
        for (int c : order) line.append(c).append(" ");
        System.out.println(line + "| " + total + " cylinders");
    }
}
Watch it run

Step through it

Running on requests 94 183 37 122 14 140 65, head 57 moving up, disk 0-199

the queue on cylinders 0-19901995765941221401833714seektotal57 starthead at 57, moving up →
Step 1

Seven requests, head at 57

The disk has cylinders 0 to 199. Seven requests wait at 94, 183, 37, 122, 14, 140 and 65, and the head sits at 57, last seen moving up. Each row below will be one request served, so the ledger on the right adds up move by move.

Output
1 / 10
4

Waiting Times and Edge Cases

LOOK cannot starve a request. The head reverses only when nothing is left ahead, so every request is reached within one trip out and one trip back. That is the difference from SSTF, which totals less here (228) but can leave a far request waiting while near ones keep arriving.

The waits are uneven, though. A request that arrives just behind the head as it turns waits the longest. On the example, a request for 175 arriving as the head leaves 183 waits 146 + 23 + 161 = 330 cylinders, while one arriving just ahead would wait 8. Cylinders in the middle are passed twice per round trip; the ends only once. C-LOOK serves in one direction only to even this out.

Two smaller cases come up in exams. If no request is ahead, the head reverses straight away. If every request is ahead, LOOK never reverses at all and the total is just the distance to the furthest one.

  • No starvation: a request waits at most one trip out and back
  • Just behind the turn is the worst place to arrive
  • Middle cylinders get served more often than the ends
5

Where LOOK Is Used

Kernel elevators for hard disks behaved like LOOK rather than SCAN: they sorted pending requests by block number and turned when the sorted list ran out, because a seek to an empty edge buys nothing. Linux's bfq, its usual choice for rotational drives, still sorts and merges requests by position beneath its fairness rules.

On SSDs and NVMe drives there is no head to move, so the order saves nothing and Linux sets these devices to the none scheduler.

  • Hard disks: request order by position, turning at the last request
  • SSDs: no seek cost, so no reordering