C-program of scan or elevator disk scheduling in operating system

What is Scan/ Elevator disk scheduling:-

It is also called as Elevator Algorithm. In this algorithm, the disk arm moves into a particular direction till the end, satisfying all the requests coming in its path, and then it turns backend moves in the reverse direction satisfying requests coming in its path.

It works in the way an elevator works, elevator moves in a direction completely till the last floor of that direction and then turns back.

Example:-  Given the following queue -- 95, 180, 34, 119, 11, 123, 62, 64 with the Read-write head initially at the track 50 and the tail track being at 199. head movement is towards low value.

1. Starvation is avoided
2. Low variance Occurs in waiting time and response time

1. In SCAN the head moves till the end of the disk despite the absence of requests to be serviced.

Code:-

#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
// logic for Scan disk scheduling
/*logic for sort the request array */
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}

}
}

int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
// if movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{
initial=RQ[i];
}
// last movement for max size
initial = size-1;
for(i=index-1;i>=0;i--)
{
initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
initial=RQ[i];
}
// last movement for min size
initial =0;
for(i=index;i<n;i++)
{
initial=RQ[i];
}
}
return 0;
}

Output:-

Enter the number of Request
8
Enter the Requests Sequence
95 180 34 119 11 123 62 64
50
Enter total disk size
200
Enter the head movement direction for high 1 and for low 0
1

Must visit:-

Recommended Post:

Hackerearth Problems:-

Data structure:-

Key points:-

MCQs:-