0% found this document useful (0 votes)
26 views10 pages

Disk SchedulingAlgorithms

The document explains three disk scheduling algorithms: FCFS, SCAN, and C-SCAN. FCFS processes requests in the order they arrive, while SCAN moves the disk arm in one direction until it reaches the end before reversing. C-SCAN operates similarly to SCAN but moves the disk arm in a circular fashion to the opposite end after reaching the limit, ensuring efficient request servicing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views10 pages

Disk SchedulingAlgorithms

The document explains three disk scheduling algorithms: FCFS, SCAN, and C-SCAN. FCFS processes requests in the order they arrive, while SCAN moves the disk arm in one direction until it reaches the end before reversing. C-SCAN operates similarly to SCAN but moves the disk arm in a circular fashion to the opposite end after reaching the limit, ensuring efficient request servicing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

FCFS:

FCFS is the simplest of all the Disk Scheduling Algorithms. In FCFS, the requests are addressed in the
order they arrive in the disk queue. 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.

#include<stdio.h>

#include<stdlib.h>

int main()

int RQ[100],i,n,TotalHeadMoment=0,initial;

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

printf("Enter initial head position\n");

scanf("%d",&initial);

// logic for FCFS disk scheduling

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

TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];

printf("Total head moment is %d",TotalHeadMoment);

return 0;

OUTPUT:

Enter the number of Request

Enter the Requests Sequence

95 180 34 119 11 123 62 64

Enter initial head position

50

Total head movement is 644

SCAN :

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.
include<stdio.h>

#include<stdlib.h>

int main()

int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;

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

printf("Enter initial head position\n");

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

TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);

initial=RQ[i];

// last movement for max size

TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);

initial = size-1;

for(i=index-1;i>=0;i--)

TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);

initial=RQ[i];

}
}

// if movement is towards low value

else

for(i=index-1;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

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

Total head movement is 337


C-SCAN:

In SCAN algorithm, the disk arm again scans the path that has been scanned, after reversing its
direction. So, it may be possible that too many requests are waiting at the other end or there may be
zero or few requests pending at the scanned area. These situations are avoided in CSAN algorithm in
which the disk arm instead of reversing its direction goes to the other end of the disk and starts
servicing the requests from there. So, the disk arm moves in a circular fashion and this algorithm is
also similar to SCAN algorithm and hence it is known as C-SCAN (Circular SCAN).

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 values.

#include<stdio.h>

#include<stdlib.h>

int main()

int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;

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

printf("Enter initial head position\n");

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 C-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++)

TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);

initial=RQ[i];

// last movement for max size

TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);

/*movement max to min disk */

TotalHeadMoment=TotalHeadMoment+abs(size-1-0);

initial=0;

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

TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);

initial=RQ[i];

// if movement is towards low value

else

for(i=index-1;i>=0;i--)

TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);

initial=RQ[i];

// last movement for min size

TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);

/*movement min to max disk */

TotalHeadMoment=TotalHeadMoment+abs(size-1-0);

initial =size-1;
for(i=n-1;i>=index;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

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

Total head movement is 382

You might also like