What is the SSTF disk scheduling algorithm?
Overview
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.
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 requestsscanf("%d",&n);//Enter the requests sequencefor(i=0;i<n;i++)scanf("%d",&ReadyQueue[i]);//Enter the initial head positionscanf("%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, andcount=0. -
Lines 6 to12: We ask the user to provide the number of
requests, thesequence of requests, and theinitial head positionas 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
minwith a large value1000in the code. - Next, we compare the
minvalue with the differencediffcalculated earlier, based on the conditionmin>diff. We check if theminvalue is greater than thediffvalue or not. - Lastly, if the condition is met, we reassign the
minvariable a value ofdiff. This is to ensure that the variablemincontains the minimum value.
- First, we initialize a variable
-
Lines 26 to 32: We add up the total head movements after each request served. Finally, we print the total number of head movements.