Lesson 4 · Storage and I/O

C-SCAN Disk Scheduling: One-Way Sweeps

Serve requests in one direction only. At the end of the disk, jump back to cylinder 0 without serving anything, and sweep the same way again.

C-SCAN Disk Scheduling: One-Way Sweeps concept diagramA visual explanation of the layout and operations shown in this lesson.sweep up, jump back to 0 without serving, then sweep up again050100150199014376594122140183199cylinder829281843161991423head 57total head movement: 378 cylinders
1

How the One-Way Sweep Works

  1. Sort the pending requests by cylinder
  2. Serve every request at or above the head, lowest first
  3. Keep going to the last cylinder (199 on a 0-199 disk)
  4. Jump to cylinder 0 and serve nothing on the way
  5. Serve the remaining requests upward, lowest first

C-SCAN, short for circular scan, moves the disk arm in one direction only. It serves every pending request on the way up, runs on to the last cylinder, then jumps straight back to cylinder 0 without serving anything and starts the next upward sweep. The cylinders behave like a ring: after 199 comes 0.

It is SCAN with the downward pass removed. SCAN serves on the way back too; C-SCAN treats the way back as dead travel.

  • One service direction, always the same one
  • The jump is a reset, not a service pass
  • If no request sits below the head, there is no jump
2

Worked Example: 378 Cylinders from Head 57

This is the same queue the disk scheduling and FCFS lessons use, so the totals compare directly. The head starts at 57 and moves toward higher cylinders.

Five requests sit above 57 and are served first, for 126 cylinders. The arm then runs on to 199 even though nothing is waiting there, jumps to 0, and climbs to 14 and 37. The service order is 65, 94, 122, 140, 183, 14, 37.

Of the 378 cylinders, 229 serve nothing: 16 to reach the edge, 199 for the jump and 14 from cylinder 0 up to the first request. Some textbooks leave the jump out of the total and give 179; say which convention you use in an answer.

Queue 94, 183, 37, 122, 14, 140, 65; head at 57 moving up; disk 0-199
MoveSeekRunning total
57 → 6588
65 → 942937
94 → 1222865
122 → 1401883
140 → 18343126
183 → 199 (no request)16142
199 → 0 (jump)199341
0 → 1414355
14 → 3723378
  • Order: 65, 94, 122, 140, 183, then 14, 37
  • Total 378 with the jump counted, 179 without
  • FCFS needs 666 on this queue; SCAN 327
3

C-SCAN vs SCAN: Even Waiting

SCAN passes over the middle of the disk twice per round trip, once each way, but reaches each edge only once. With the arm sweeping steadily, a request at cylinder 100 waits about 100 cylinders of travel on average, while one at 199 can wait almost 400 and averages about 200. Edge requests wait twice as long.

C-SCAN fixes that by sweeping the same way every time. Each cylinder is passed once per cycle at the same interval, so the expected wait is about half a cycle wherever the request sits. Waits become even, not shorter: the middle cylinders give up what the edges gain. The cost in head movement is 378 cylinders here, against 327 for SCAN.

Counting cylinders also overstates the jump. A seek is mostly the arm speeding up, slowing down and settling on the track, so one 199-cylinder seek takes far less time than many short seeks adding up to 199.

C-SCAN vs SCAN on the head-57 queue
SCANC-SCAN
Serves while movingBoth waysUp only
Total head movement327378
Middle vs edge waitEdge waits about twice as longAbout the same everywhere
Way backServes requestsOne jump, serves nothing
  • SCAN: lower total, uneven waits
  • C-SCAN: higher total, even waits
  • One long seek is cheaper in time than its cylinder count suggests
Implementation

C-SCAN on the head-57 queue

def c_scan(requests, head, disk_size=200):
    above = sorted(c for c in requests if c >= head)
    below = sorted(c for c in requests if c < head)
    # Both edges are visited: the top to finish the sweep, cylinder 0 to
    # restart it. Neither serves a request.
    order = above + [disk_size - 1, 0] + below if below else above
    total, position = 0, head
    for cylinder in order:
        total += abs(cylinder - position)
        position = cylinder
    return order, total


order, total = c_scan([94, 183, 37, 122, 14, 140, 65], 57)
print(" ".join(map(str, 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 cScan(const vector<int>& requests, int head, vector<int>& order, int diskSize = 200) {
    int total = 0, position = head;
    // Up to the edge, jump to 0, then up again. Neither edge serves a request.
    vector<int> above, below;
    for (int c : requests) (c >= head ? above : below).push_back(c);
    sort(above.begin(), above.end());
    sort(below.begin(), below.end());
    for (int c : above) order.push_back(c);
    if(!below.empty()) {
      order.push_back(diskSize - 1);
      order.push_back(0);
    }
    for (int c : below) order.push_back(c);
    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 = cScan(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 cScan(int[] requests, int head, List<Integer> order, int diskSize) {
        int total = 0, position = head;
        // Up to the edge, jump to 0, then up again. Neither edge serves a request.
        List<Integer> above = new ArrayList<>(), below = new ArrayList<>();
        for (int c : requests) (c >= head ? above : below).add(c);
        Collections.sort(above);
        Collections.sort(below);
        order.addAll(above);
        if(!below.isEmpty()) {
          order.add(diskSize - 1);
          order.add(0);
        }
        order.addAll(below);
        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 = cScan(requests, 57, order, 200);
        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

head 57, moving up, disk 0-1990199head at 57clockwise = up94183371221414065head movementtotal0
Step 1

Seven requests on a ring of cylinders

The head is at 57 and moving up. C-SCAN only ever serves in that direction, so it helps to draw the cylinders as a ring: after 199 the next cylinder it serves from is 0. The ring is a picture of the order, not of the hardware; the arm still sits on a straight line.

Output
1 / 12
4

Edge Cases and Variants

Nothing below the head. If every request is at or above the head, the sweep ends at the last one and no jump happens: 126 cylinders on this queue without 14 and 37. The code checks for this.

A request just behind the head. A request for cylinder 56 that arrives once the arm has left 57 waits for the arm to reach 199, jump to 0 and climb back: 142 + 199 + 56 = 397 cylinders of travel, almost a full cycle. That is C-SCAN's worst wait, and it is the same for every cylinder, which is the point.

Direction. The sweep can run downward instead: serve down to 0, jump to 199, serve down again. The total changes, the idea does not. C-LOOK keeps the one-way sweep but turns at the last request and jumps only as far as the lowest one, which cuts this queue to 318.

On SSDs none of this applies: there is no arm, and Linux usually runs NVMe drives with the none scheduler. For hard disks, mq-deadline sorts by sector but adds expiry times, which gives the bounded wait C-SCAN aims for by a timer instead of by geometry.

  • No requests below the head means no jump
  • Worst wait: a request just behind the head, about one full cycle
  • C-LOOK trims both edges: 318 here