Lab 13 walkthrough — Disk scheduling algorithms: FCFS, SSTF, SCAN
Lab 13 walkthrough — Disk scheduling algorithms: FCFS, SSTF, SCAN
Recorded by Dr. Krithika L.B. Slot lab13-walkthrough
Theory link: 5.2 Disk scheduling
Lab 13 walkthrough — Disk scheduling algorithms: FCFS, SSTF, SCAN
Recorded by Dr. Krithika L.B. Slot lab13-walkthrough
Aim. To compute the order of service and the total head movement for FCFS, SSTF and SCAN disk scheduling.
Head movement between two requests = |current − next|; the total is the sum over the whole sequence (Session 5.2).
Serve in arrival order.
Repeatedly pick the unserved request closest to the head.
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.
/* 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;
}gcc disk1.c -o disk1./disk1Head movement: 53 -> 98 -> 183 -> 37 -> 122 -> 14 -> 124 -> 65 -> 67
Total head movement = 640 cylindersHead movement: 53 -> 65 -> 67 -> 37 -> 14 -> 98 -> 122 -> 124 -> 183
Total head movement = 236 cylindersHead movement: 53 -> 65 -> 67 -> 98 -> 122 -> 124 -> 183 -> 199 -> 37 -> 14
Total head movement = 331 cylindersPrepare these for the lab evaluation.
Revealed 0Requests near the head keep being chosen; a far request may wait indefinitely if nearer ones keep arriving.
The arm moves in one direction serving requests, then reverses — like a lift.
No; it is greedy. For this data, 53 → 37 → 14 → 65 → … gives 208.