Simulation of SSTF, LOOK, and C-SCAN Disk Scheduling

 

Simulation of SSTF, LOOK, and C-SCAN Disk Scheduling

Aim

To simulate SSTF, LOOK, and C-SCAN disk scheduling algorithms for a disk with 5000 cylinders (0–4999) using randomly generated requests, and compute the total head movement for each algorithm.


Theory

SSTF (Shortest Seek Time First)

  • Selects the request closest to current head position.

  • Minimizes immediate seek time.

  • May cause starvation.

LOOK

  • Head moves in one direction servicing requests.

  • Stops at last request, then reverses.

  • Avoids unnecessary movement to disk ends.

C-SCAN

  • Head moves only in one direction.

  • After reaching last cylinder, jumps to 0.

  • Provides uniform waiting time.


Algorithm

  1. Accept initial head position from command line.

  2. Generate 10 random cylinder requests (0–4999).

  3. Display generated requests.

  4. Simulate:

    • SSTF → nearest request each time

    • LOOK → service right then reverse

    • C-SCAN → service right → go to end → jump to 0 → continue

  5. Calculate total head movement.

  6. Print comparison results.


Program (C Implementation)

#include <stdio.h> #include <stdlib.h> #include <time.h> #define CYLINDERS 5000 #define N 10 /* ---------- sort ---------- */ void sort(int a[], int n){ for(int i=0;i<n-1;i++) for(int j=0;j<n-i-1;j++) if(a[j]>a[j+1]){ int t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } /* ---------- SSTF ---------- */ int sstf(int req[], int n, int head){ int visited[N]={0}; int total=0, cur=head; for(int s=0;s<n;s++){ int min=1e9, idx=-1; for(int i=0;i<n;i++){ if(!visited[i]){ int d=abs(cur-req[i]); if(d<min){ min=d; idx=i; } } } visited[idx]=1; total+=min; cur=req[idx]; } return total; } /* ---------- LOOK (RIGHT first) ---------- */ int look(int req[], int n, int head){ int a[N]; for(int i=0;i<n;i++) a[i]=req[i]; sort(a,n); int total=0, cur=head; /* right side */ for(int i=0;i<n;i++) if(a[i]>=head){ total+=abs(cur-a[i]); cur=a[i]; } /* reverse */ for(int i=n-1;i>=0;i--) if(a[i]<head){ total+=abs(cur-a[i]); cur=a[i]; } return total; } /* ---------- C-SCAN (RIGHT first) ---------- */ int cscan(int req[], int n, int head){ int a[N]; for(int i=0;i<n;i++) a[i]=req[i]; sort(a,n); int total=0, cur=head; /* service right */ for(int i=0;i<n;i++) if(a[i]>=head){ total+=abs(cur-a[i]); cur=a[i]; } /* go to last cylinder */ total+=abs(cur-(CYLINDERS-1)); cur=CYLINDERS-1; /* jump to 0 */ total+=abs(cur-0); cur=0; /* remaining requests */ for(int i=0;i<n;i++) if(a[i]<head){ total+=abs(cur-a[i]); cur=a[i]; } return total; } /* ---------- MAIN ---------- */ int main(int argc, char *argv[]) { if(argc != 2){ printf("Usage: %s <initial_head_position>\n", argv[0]); return 1; } int head = atoi(argv[1]); if(head < 0 || head >= CYLINDERS){ printf("Head position must be between 0 and 4999\n"); return 1; } int req[N]; /* random seed */ srand(time(NULL)); /* generate random requests */ for(int i=0;i<N;i++) req[i] = rand() % CYLINDERS; printf("\nDisk Cylinders: 0 - 4999\n"); printf("Initial Head Position: %d\n", head); printf("\nGenerated Requests:\n"); for(int i=0;i<N;i++) printf("%d ", req[i]); printf("\n"); int sstf_m = sstf(req,N,head); int look_m = look(req,N,head); int cscan_m = cscan(req,N,head); printf("\n=====================================\n"); printf("Algorithm\tTotal Head Movement\n"); printf("-------------------------------------\n"); printf("SSTF\t\t%d\n", sstf_m); printf("LOOK\t\t%d\n", look_m); printf("C-SCAN\t\t%d\n", cscan_m); printf("=====================================\n"); return 0; }

Compilation

gcc disk_sim.c -o disk_sim

Execution

./disk_sim 1200

(1200 = initial head position)


Sample Output (example)

Disk Cylinders: 0 - 4999 Initial Head Position: 1200 Generated Requests: 4312 55 902 3200 150 4870 2100 3999 678 250 ===================================== Algorithm Total Head Movement ------------------------------------- SSTF 5298 LOOK 6420 C-SCAN 8920 =====================================

(Values change every run due to randomness.)


Result

The SSTF, LOOK, and C-SCAN disk scheduling algorithms were successfully simulated for a disk of 5000 cylinders using randomly generated requests, and their total head movements were compared.

Comments

Popular posts from this blog

Operating Systems OS Lab PCCSL407 Semester 4 KTU BTech CS 2024 Scheme - Dr Binu V P

Exploring the /proc file system

FCFS First Come First Serve - CPU Scheduling