You are on page 1of 27

Disk Schedule

Write a program to implement the following disk scheduling algorithms

1. FCFS 2. SSTF 3. SCAN. 4. LOOK 5. C-SCAN 6. C-LOOK

Input : Algorithm selection, Max Cyclinder, Initial Position, Number of Requests, Request string

Output : Latency time.

#include<stdio.h>

#include <stdlib.h>

int fcfs()

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;

#include<stdbool.h>

int request[50];

int SIZE;

bool flag[50];

int dist(int,int);

struct max{

int data;

int dis;

int dat;

}kate[50],kat;

struct max check(int head,int n)

int i;

int j=0;

for(i=0;i<n;i++)

if(flag[i]==0){

kate[j].dis=dist(head,request[i]);

kate[j].data=request[i];

kate[j].dat=i;

j++;

n=j;

for (i = 0; i < n-1; i++)


{

for (j = 0; j < n-i-1; j++)

if (kate[j].dis > kate[j+1].dis)

int temp=kate[j].data;

kate[j].data=kate[j+1].data;

kate[j+1].data=temp;

temp=kate[j].dis;

kate[j].dis=kate[j+1].dis;

kate[j+1].dis=temp;

temp=kate[j].dat;

kate[j].dat=kate[j+1].dat;

kate[j+1].dat=temp;

return kate[0];

int dist(int a,int b)

if(a>b){

return a-b;

return b-a;

void stsk(int n)

int head,i;

int seekcount=0;

printf("ENTER THE CURRENT HEAD :\n");


scanf("%d",&head);

printf("SEEK SEQUENCE = ") ;

for(i=0;i<n;i++)

if(request[i]<SIZE-1)

printf("%d ",head);

kat=check(head,n);

seekcount=seekcount+kat.dis;

head=kat.data;

flag[kat.dat]=true;

printf("%d\nTOTAL DISTANCE : %d",head,seekcount);

int sstf()

int n,i;

printf("ENTER THE DISK SIZE :\n");

scanf("%d",&SIZE);

printf("ENTER THE NO OF REQUEST SEQUENCE :\n");

scanf("%d",&n);

printf("ENTER THE REQUEST SEQUENCE :\n");

for(i=0;i<n;i++){

scanf("%d",&request[i]);

flag[i]=0;

stsk(n);

int absoluteValue(int x)
{

if(x>0)

return x;

else

return x*-1;

void scanfn()

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);

}
void lookfn()

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];

//Copying second array queue2[] after that first one is copied, into queue[]

for(i=temp1+1,j=0;j<temp2;i++,j++)

queue[i]=queue2[j];

//At this point, we have the queue[] with the requests in the

//correct order of execution as per LOOK 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);

void cscanfn()

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 ascending 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;

//Moving Disk head to the inner most cylinder, because this is Circular Scan.

queue[i+1]=0;
//Copying second array queue2[] after that first one is copied, into queue[]

for(i=temp1+3,j=0;j<temp2;i++,j++)

queue[i]=queue2[j];

//At this point, we have the queue[] with the requests in the

//correct order of execution as per C-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+1; 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);


}

void clookfn()

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 ascending 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];

//Moving Disk head to the inner most requested cylinder,

//because this is Circular LOOK.

queue[i]=queue2[0];

//Copying second array queue2[] after that first one is copied, into queue[]

for(i=temp1+1,j=0;j<temp2;i++,j++)

queue[i]=queue2[j];

//At this point, we have the queue[] with the requests in the

//correct order of execution as per C-LOOK 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);

int main()

int num;

printf("Enter the choice: ");

scanf("%d", &num);

if(num==1)

fcfs();

else if(num==2)

sstf();
}

else if(num==3)

scanfn();

else if(num==4)

lookfn();

else if(num==5)

cscanfn();

else if(num==6)

clookfn();

Alternate code

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<math.h>

#include<assert.h>

#include<limits.h>

void validate(int inp[],int request_n,int cy_max){

for(int i=0;i<request_n;i++){

assert(inp[i]<=cy_max);

}
void print_op(int total){

printf("seek operations:%d",total);

void fcfs(int cy_max,int posi,int inp[],int request_n){

validate(inp,request_n,cy_max);

int total=0;

for(int i=0;i<request_n;i++){

total+=abs(inp[i]-posi);

posi=inp[i];

print_op(total);

int find_closest(int inp[],int request_n,int posi,int used[]){

int n=0;

while(used[n]==1 && n<request_n){

n++;

int m=inp[n]-posi;

for(int i=0;i<request_n;i++){

if(((inp[i]-posi)<m) && used[i]!=1){

m=inp[i]-posi;

n=i;

return n;

void sstf(int cy_max,int posi,int inp[],int request_n){

validate(inp,request_n,cy_max);

int total=0,rem=request_n,n;

int used[request_n];

for(int i=0;i<request_n;i++){
used[i]=0;

while(rem>0){

n=find_closest(inp,request_n,posi,used);

used[n]=1;

total+=abs(inp[n]-posi);

posi=inp[n];

rem--;

print_op(total);

int find_max(int inp[],int request_n){

int n=0;

for(int i=0;i<request_n;i++){

if(inp[i]>inp[n]){

n=i;

return n;

//diretcionn?

void scan(int cy_max,int posi,int inp[],int request_n){

validate(inp,request_n,cy_max);

int total=0;

if(inp[find_max(inp,request_n)]<posi){

print_op(posi);

else{

total+=inp[find_max(inp,request_n)]+posi;

print_op(total);

}
}

int find_min(int inp[],int request_n){

int m=0;

for(int i=0;i<request_n;i++){

if(inp[m]>inp[i]){

m=i;

return m;

//direction?

void look(int cy_max,int posi,int inp[],int request_n){

validate(inp,request_n,cy_max);

int total=0;

int n=find_max(inp,request_n);

int m=find_min(inp,request_n);

total=abs(inp[m]-posi);

total+=abs(inp[n]-inp[m]);

if(inp[n]<posi){

total=abs(posi-inp[m]);

if(inp[m]>posi){

total=abs(posi-inp[n]);

print_op(total);

//direction?

int find_max_less_posi(int inp[],int request_n,int posi){

int i=0;

while(inp[i]>posi && i<request_n){

i++;
}

if(i==request_n){

return 0;

for(int j=0;j<request_n;j++){

if(inp[j]>inp[i] && inp[j]<posi){

i=j;

return inp[i];

void cscan(int cy_max,int posi,int inp[],int request_n){

validate(inp,request_n,cy_max);

int total=0;

int n=find_max(inp,request_n);

int m=find_min(inp,request_n);

total=cy_max-posi+cy_max+find_max_less_posi(inp,request_n,posi)-2;

if(inp[m]>posi){

total=inp[n]-posi;

if(inp[n]<posi){

total=posi-inp[m];

print_op(total);

//direction?

void clook(int cy_max,int posi,int inp[],int request_n){

validate(inp,request_n,cy_max);

int total=0;

int n=find_max(inp,request_n);

int m=find_min(inp,request_n);
total=inp[n]-posi+inp[n]-inp[m]+find_max_less_posi(inp,request_n,posi)-inp[m];

if(inp[m]>posi){

total=inp[n]-posi;

if(inp[n]<posi){

total=posi-inp[m];

print_op(total);

int main(){

int choice,cy_max,posi,request_n;

printf("Enter choice,Max cylinder,\ninitial position and number of requests:");

scanf("%d %d %d %d",&choice,&cy_max,&posi,&request_n);

int *inp=(int *)malloc(sizeof(int)*request_n);

printf("enter the request string array:\n");

for(int i=0;i<request_n;i++){

scanf("%d",&inp[i]);

switch(choice){

case 1:

fcfs(cy_max,posi,inp,request_n);

break;

case 2:

sstf(cy_max,posi,inp,request_n);

break;

case 3:

scan(cy_max,posi,inp,request_n);

break;

case 4:

look(cy_max,posi,inp,request_n);

break;
case 5:

cscan(cy_max,posi,inp,request_n);

break;

case 6:

clook(cy_max,posi,inp,request_n);

break;

default:

printf("invalid choice");

return 0;

}
Alternate code output:

You might also like