0% found this document useful (0 votes)
44 views7 pages

Disk Scheduling-1

The document describes the FCFS, SCAN, and C-SCAN disk scheduling algorithms. It provides pseudocode to implement each algorithm in C. For FCFS, it calculates the total head movement by taking requests in arrival order. For SCAN, it sorts requests and calculates head movement in either direction from initial position. For C-SCAN, it is similar to SCAN but moves between disk ends after serving requests in one direction.

Uploaded by

7uP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views7 pages

Disk Scheduling-1

The document describes the FCFS, SCAN, and C-SCAN disk scheduling algorithms. It provides pseudocode to implement each algorithm in C. For FCFS, it calculates the total head movement by taking requests in arrival order. For SCAN, it sorts requests and calculates head movement in either direction from initial position. For C-SCAN, it is similar to SCAN but moves between disk ends after serving requests in one direction.

Uploaded by

7uP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Disk Scheduling Algorithms

FCFS
Aim
Write a C program to implement FCFS Disk scheduling algorithm.
Algorithm
Step1: Start
Step2: Declare all the necessary variables,Enter the number of
requests,sequence of request and initial head position.
Step 3:Let ReadyQueue represents an array storing indexes of tracks that have
been requested in ascending order of their time of arrival. ‘initial’ is the position of
disk head.
Step4:Let us one by one take the tracks in default order and calculate the
absolute distance of the track from the head.
Step5:Increment the total seek count with this distance.
Step6:Currently serviced track position now becomes the new head position.
Step 7:Go to step 4 until all tracks in request array have not been serviced.
Step 8:Stop the program
Program

#include<stdio.h>
#include<stdlib.h>
int main()
{
int ReadyQueue[100],i,n,TotalHeadMov=0,initial;
printf("Enter the number of requests:");
scanf("%d",&n);
printf("Enter the sequence of request");
for(i=0;i<n;i++)
{
scanf("%d",&ReadyQueue[i]);
}
printf("Enter initial head position");
scanf("%d",&initial);
for(i=0;i<n;i++)
{
TotalHeadMov=TotalHeadMov+abs(ReadyQueue[i]-initial);
initial=ReadyQueue[i];
}
printf("Total Head Movement=%d",TotalHeadMov);
}

SCAN

Aim
Write a C program to implement SCAN Disk scheduling algorithm.
Algorithm
Step1: Start
Step2: Declare all the necessary variables,Enter the number of
requests,sequence of request and initial head position.
Step 3:Let ReadyQueue represents an array storing indexes of tracks that have
been requested in ascending order of their time of arrival. ‘initial’ is the position of
disk head.
Step 4:Sort the request array .
Step 5:Enter the Head movement Direction for high1 and for low 0
Step 6:If Head movement Direction is high then go to step 7 else go to step8
Step 7: Find the total head movement up to the tail end of the disk then move
backward to the first value in the sorted array and return the final total head
movement.
Step8: Find the total head movement up to begin (0) and then move to forward
direction up to the last value in the sorted array and return the final total head
movement.
Step 9:Stop the program
Program
#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;

C-SCAN
Aim
Write a C program to implement C-SCAN Disk scheduling algorithm.
Algorithm
Step1: Start
Step2: Declare all the necessary variables,Enter the number of
requests,sequence of request and initial head position.
Step 3:Let ReadyQueue represents an array storing indexes of tracks that have
been requested in ascending order of their time of arrival. ‘initial’ is the position of
disk head.
Step 4:Sort the request array .
Step 5:Enter the Head movement Direction for high1 and for low 0
Step 6:If Head movement Direction is high then go to step 7 else go to step8
Step 7: Find the total head movement up to the tail end of the disk then move tail
end to begin and find the remaining head movement in the forward direction up
to remaining values in the sorted array and return the final total head
movement.
Step8: Find the total head movement up to beginning (0)then move to tail end
and move in backward direction up to remaining values in the sorted array and
return the final total head movement.
Step 9:Stop the program

Program
#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;
}

You might also like