Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Lab 13: Disk scheduling algorithms: FCFS, SSTF, SCAN

Theory link: 5.2 Disk scheduling

Experiment 13 of 15 Module 5 2 hours

Video walkthrough

Lab 13 walkthrough — Disk scheduling algorithms: FCFS, SSTF, SCAN

Video coming soon

Lab 13 walkthrough — Disk scheduling algorithms: FCFS, SSTF, SCAN

Recorded by Dr. Krithika L.B. Slot lab13-walkthrough

Aim

Aim. To compute the order of service and the total head movement for FCFS, SSTF and SCAN disk scheduling.

Explanation

Head movement between two requests = |current − next|; the total is the sum over the whole sequence (Session 5.2).

FCFS

Serve in arrival order.

SSTF

Repeatedly pick the unserved request closest to the head.

SCAN

Sort the requests; go up serving requests ≥ head, continue to the last cylinder (size − 1), then reverse and serve the rest in decreasing order.

The program assumes the head moves toward higher cylinder numbers. Check your answers with the simulator in Session 5.2.

Code

disk1.c
/* disk1.c - disk scheduling: FCFS, SSTF, SCAN (BACSE106 Lab 13) */
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int n, head, size, choice, i, j, total = 0;
    int req[50], done[50] = {0};

    printf("Number of requests: ");
    scanf("%d", &n);
    printf("Request queue: ");
    for (i = 0; i < n; i++) scanf("%d", &req[i]);
    printf("Initial head position: ");
    scanf("%d", &head);
    printf("Disk size (number of cylinders): ");
    scanf("%d", &size);
    printf("1.FCFS  2.SSTF  3.SCAN (towards higher cylinders)  Choice: ");
    scanf("%d", &choice);

    printf("\nHead movement: %d", head);
    if (choice == 1) {                                   /* FCFS: in arrival order */
        for (i = 0; i < n; i++) {
            total += abs(req[i] - head);
            head = req[i];
            printf(" -> %d", head);
        }
    } else if (choice == 2) {                            /* SSTF: nearest pending request */
        for (i = 0; i < n; i++) {
            int k = -1;
            for (j = 0; j < n; j++)
                if (!done[j] && (k == -1 || abs(req[j] - head) < abs(req[k] - head)))
                    k = j;
            done[k] = 1;
            total += abs(req[k] - head);
            head = req[k];
            printf(" -> %d", head);
        }
    } else {                                             /* SCAN: up to the end, then reverse */
        int start = head;
        for (i = 0; i < n; i++)                          /* sort requests */
            for (j = i + 1; j < n; j++)
                if (req[j] < req[i]) { int t = req[i]; req[i] = req[j]; req[j] = t; }
        for (i = 0; i < n; i++)                          /* serve requests >= head going up */
            if (req[i] >= start) { total += req[i] - head; head = req[i]; printf(" -> %d", head); }
        total += (size - 1) - head;                      /* continue to the last cylinder */
        head = size - 1;
        printf(" -> %d", head);
        for (i = n - 1; i >= 0; i--)                     /* reverse and serve the rest */
            if (req[i] < start) { total += head - req[i]; head = req[i]; printf(" -> %d", head); }
    }
    printf("\nTotal head movement = %d cylinders\n", total);
    return 0;
}

Download disk1.c

How to run

  1. gcc disk1.c -o disk1
  2. ./disk1
  3. Enter the number of requests, the queue, the head position, the disk size and the algorithm.

Sample input & output

FCFS

Input
8 · 98 183 37 122 14 124 65 67 · head 53 · disk size 200 · choice 1
Output
Output
Head movement: 53 -> 98 -> 183 -> 37 -> 122 -> 14 -> 124 -> 65 -> 67
Total head movement = 640 cylinders

SSTF

Input
8 · 98 183 37 122 14 124 65 67 · head 53 · disk size 200 · choice 2
Output
Output
Head movement: 53 -> 65 -> 67 -> 37 -> 14 -> 98 -> 122 -> 124 -> 183
Total head movement = 236 cylinders

SCAN

Input
8 · 98 183 37 122 14 124 65 67 · head 53 · disk size 200 · choice 3
Output
Output
Head movement: 53 -> 65 -> 67 -> 98 -> 122 -> 124 -> 183 -> 199 -> 37 -> 14
Total head movement = 331 cylinders

Try it yourself

  1. Add a direction input so SCAN can move toward 0 first (expected total 236 for the same data).
  2. Print the average seek distance per request.
  3. Generate 50 random requests and compare the three algorithms.

Viva questions

Prepare these for the lab evaluation.

Revealed 0
Why can SSTF starve requests?

Requests near the head keep being chosen; a far request may wait indefinitely if nearer ones keep arriving.

Why is SCAN called the elevator algorithm?

The arm moves in one direction serving requests, then reverses — like a lift.

Is SSTF optimal?

No; it is greedy. For this data, 53 → 37 → 14 → 65 → … gives 208.