1)C program for FCFS disk scheduling :
#include<stdio.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
scanf("%d",&queue[i]);
printf("Enter the initial head position\n");
scanf("%d",&head);
queue[0]=head;
for(j=0;j<=n-1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with
seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}
2)SSTF
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int queue[100],t[100],head,seek=0,n,i,j,temp;
float avg;
// clrscr();
printf("*** SSTF Disk Scheduling Algorithm ***\n");
printf("Enter the size of Queue\t");
scanf("%d",&n);
printf("Enter the Queue\t");
for(i=0;i<n;i++)
{
scanf("%d",&queue[i]);
}
printf("Enter the initial head position\t");
scanf("%d",&head);
for(i=1;i<n;i++)
t[i]=abs(head-queue[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(t[i]>t[j])
{
temp=t[i];
t[i]=t[j];
t[j]=temp;
temp=queue[i];
queue[i]=queue[j];
queue[j]=temp;
}
}
}
for(i=1;i<n-1;i++)
{
seek=seek+abs(head-queue[i]);
head=queue[i];
}
printf("\nTotal Seek Time is%d\t",seek);
avg=seek/(float)n;
printf("\nAverage Seek Time is %f\t",avg);
getch();
}
3) // C Program to Simulate SCAN Disk Scheduling Algorithm
//visit www.nanogalaxy.org for more programs.
#include<stdio.h>
int absoluteValue(int); // Declaring function absoluteValue
void main()
{
int queue[25],n,headposition,i,j,k,seek=0, maxrange,
difference,temp,queue1[20],queue2[20],temp1=0,temp2=0;
float averageSeekTime;
// Reading the maximum Range of the Disk.
printf("Enter the maximum range of Disk: ");
scanf("%d",&maxrange);
// Reading the number of Queue Requests(Disk access requests)
printf("Enter the number of queue requests: ");
scanf("%d",&n);
// Reading the initial head position.(ie. the starting point of
execution)
printf("Enter the initial head position: ");
scanf("%d",&headposition);
// Reading disk positions to be read in the order of arrival
printf("Enter the disk positions to be read(queue): ");
for(i=1;i<=n;i++) // Note that i varies from 1 to n instead of 0
to n-1
{
scanf("%d",&temp); //Reading position value to a temporary
variable
//Now if the requested position is greater than current
headposition,
//then pushing that to array queue1
if(temp>headposition)
{
queue1[temp1]=temp; //temp1 is the index variable of
queue1[]
temp1++; //incrementing temp1
}
else //else if temp < current headposition,then push to
array queue2[]
{
queue2[temp2]=temp; //temp2 is the index variable of
queue2[]
temp2++;
}
}
//Now we have to sort the two arrays
//SORTING array queue1[] in ascending order
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
//SORTING array queue2[] in descending order
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]<queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
//Copying first array queue1[] into queue[]
for(i=1,j=0;j<temp1;i++,j++)
{
queue[i]=queue1[j];
}
//Setting queue[i] to maxrange because the head goes to
//end of disk and comes back in scan Algorithm
queue[i]=maxrange;
//Copying second array queue2[] after that first one is copied,
into queue[]
for(i=temp1+2,j=0;j<temp2;i++,j++)
{
queue[i]=queue2[j];
}
//Setting queue[i] to 0. Because that is the innermost cylinder.
queue[i]=0;
//At this point, we have the queue[] with the requests in the
//correct order of execution as per scan algorithm.
//Now we have to set 0th index of queue[] to be the initial
headposition.
queue[0]=headposition;
// Calculating SEEK TIME. seek is initially set to 0 in the
declaration part.
for(j=0; j<=n; j++) //Loop starts from headposition. (ie. 0th index
of queue)
{
// Finding the difference between next position and current
position.
difference = absoluteValue(queue[j+1]-queue[j]);
// Adding difference to the current seek time value
seek = seek + difference;
// Displaying a message to show the movement of disk head
printf("Disk head moves from position %d to %d with Seek %d \
n",
queue[j], queue[j+1], difference);
}
// Calculating Average Seek time
averageSeekTime = seek/(float)n;
//Display Total and Average Seek Time(s)
printf("Total Seek Time= %d\n", seek);
printf("Average Seek Time= %f\n", averageSeekTime);
}
// Defining function absoluteValue
int absoluteValue(int x)
{
if(x>0)
{
return x;
}
else
{
return x*-1;
}
}
// END OF CODE
//Code written and commented by Nived Kannada
4) C program for C-SCAN disk scheduling :
#include<stdio.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],
temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>=head)
{
queue1[temp1]=temp;
temp1++;
}
else
{
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]>queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
queue[i+1]=0;
for(i=temp1+3,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[0]=head;
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with
seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}