Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

sstf program in c

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
int main()
{
    int RQ[100],i,n,TotalHeadMoment=0,initial,count=0;
    printf("Enter the number of Requests
");
    scanf("%d",&n);
    printf("Enter the Requests sequence
");
    for(i=0;i<n;i++)
     scanf("%d",&RQ[i]);
    printf("Enter initial head position
");
    scanf("%d",&initial);
    
    // logic for sstf disk scheduling
    
        /* loop will execute until all process is completed*/
    while(count!=n)
    {
        int min=INT_MAX,d,index;
        for(i=0;i<n;i++)
        {
           d=abs(RQ[i]-initial);
           if(min>d)
           {
               min=d;
               index=i;
           }
           
        }
        TotalHeadMoment=TotalHeadMoment+min;
        initial=RQ[index];
        // 1000 is for max
        // you can use any number
        RQ[index]=INT_MAX;
        count++;
    }
    
    printf("Total head movement is %d",TotalHeadMoment);
    return 0;
}
 
PREVIOUS NEXT
Tagged: #sstf #program
ADD COMMENT
Topic
Name
9+3 =