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.



Advantage:-

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

Disadvantage:-

  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()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf(“Enter the number of Requestsn”);
scanf(“%d”,&n);
printf(“Enter the Requests sequencen”);
for(i=0;i<n;i++)
scanf(“%d”,&RQ[i]);
printf(“Enter initial head positionn”);
scanf(“%d”,&initial);
printf(“Enter total disk sizen”);
scanf(“%d”,&size);
printf(“Enter the head movement direction for high 1 and for low 0n”);
scanf(“%d”,&move);
// logic for Scan disk scheduling
/*logic for sort the request array */
for(i=0;i<n;i++)
{
for(j=0;j<ni1;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++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
// last movement for max size
TotalHeadMoment=TotalHeadMoment+abs(sizeRQ[i1]-1);
initial = size1;
for(i=index1;i>=0;i–)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index1;i>=0;i–)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
// last movement for min size
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
initial =0;
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
printf(“Total head movement is %d”,TotalHeadMoment);
return 0;
}

Output:-

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

Must visit:-

                    Program of disk scheduling algorithm

Recommended Post:

Hackerearth Problems:-

Data structure:-

Key points:-

 MCQs:-

Leave a Reply

Your email address will not be published.