What is the SSTF disk scheduling algorithm?

Overview

SSTFShortest Seek Time First is a disk scheduling algorithm that serves requests generated by the MMUMemory Management Unit.

In this algorithm, the R/W head looks for the nearest track number to serve the I/O request.

How is SSTF different from FCFS?

  • The seek time using SSTF is less as compared to the FCFS algorithm.
  • Unlike the FCFS algorithm, the SSTF has the problem of starvation. This is because some requests have to wait for a longer amount of time until they are processed.

Example

Let’s consider a disk containing 200 tracks(0-199), a request queue containing track numbers [82,170,43,140,24,16,190], and the current position of read-write head=50. The requirement is to find the total number of track movements in cylinders.

SSTF scheduling algorithm

Input

  • We enter the number of requests: 7.
  • We enter the sequence of requests: 82 170 43 140 24 16 190.
  • We enter the initial head position: 50.

Implementation of SSTF

# include<stdio.h>
#include<stdlib.h>
int main()
{
int ReadyQueue[100],i,n,TotalHeadMoment=0,initial,count=0;
// The number of requests
scanf("%d",&n);
//Enter the requests sequence
for(i=0;i<n;i++)
scanf("%d",&ReadyQueue[i]);
//Enter the initial head position
scanf("%d",&initial);
while(count!=n)
{
int min=1000,diff,index;
for(i=0;i<n;i++)
{
diff=abs(ReadyQueue[i]-initial);
if(min>diff)
{
min=diff;
index=i;
}
}
TotalHeadMoment=TotalHeadMoment+min;
initial=ReadyQueue[index];
ReadyQueue[index]=1000;
count++;
}
printf("Total head movement is %d",TotalHeadMoment);
}

Enter the input below

Code explanation

  • Line 5: We declare the variables ReadyQueue[100],TotalHeadMoment=0,initial, and count=0.

  • Lines 6 to12: We ask the user to provide the number of requests, the sequence of requests, and the initial head position as input.

  • Lines 15 to 20: To find the least head movement required to serve the request, we try to determine the difference between each head request diff=abs(ReadyQueue[i]-initial).

  • Lines 20 to 25: We follow the below steps.

    • First, we initialize a variable min with a large value 1000 in the code.
    • Next, we compare the min value with the difference diff calculated earlier, based on the condition min > diff. We check if the min value is greater than the diff value or not.
    • Lastly, if the condition is met, we reassign the min variable a value of diff. This is to ensure that the variable min contains the minimum value.
  • Lines 26 to 32: We add up the total head movements after each request served. Finally, we print the total number of head movements.

Free Resources