Professional Documents
Culture Documents
Islamabad Campus
OPERATING SYSTEMS
Submitted by
Name: ________________________________
Submitted to:
79
LAB NO 13: SIMULATE DISK SCHEDULING ALGORITHMS
Duration 3 Hours
OS /Tool/Language C compiler
One of the responsibilities of the operating system is to use the hardware efficiently. For the
disk drives, meeting this responsibility entails having fast access time and large disk bandwidth.
Both the access time and the bandwidth can be improved by managing the order in which disk
I/O requests are serviced which is called as disk scheduling. The simplest form of disk
scheduling is, of course, the First Come First Serve (FCFS) algorithm. This algorithm is
intrinsically fair, but it generally does not provide the fastest service.
In the SCAN algorithm, the disk arm starts at one end, and moves towards the other end,
servicing requests as it reaches each cylinder, until it gets to the other end of the disk. At the
other end, the direction of head movement is reversed, and servicing continues. The head
continuously scans back and forth across the disk. C-SCAN is a variant of SCAN designed to
provide a more uniform wait time. Like SCAN, C-SCAN moves the head from one end of the
disk to the other, servicing requests along the way. When the head reaches the other end,
however, it immediately returns to the beginning of the disk without servicing any requests on
the return trip
a) First Come First Serve (FCFS) disk scheduling algorithm source code
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,n,req[50],mov=0,cp;
printf("enter the current position\n");
80
scanf("%d",&cp);
printf("enter the number of requests\n");
scanf("%d",&n);
printf("enter the request order\n");
for(i=0;i<n;i++)
{
scanf("%d",&req[i]);
}
mov=mov+abs(cp-req[0]); // abs is used to calculate the
absolute value
printf("%d -> %d",cp,req[0]);
for(i=1;i<n;i++)
{
mov=mov+abs(req[i]-req[i-1]);
printf(" -> %d",req[i]);
}
printf("\n");
printf("total head movement = %d\n",mov);
}
INPUT
enter the current position
45
enter the number of requests
5
enter the request order
30
66
24
75
50
OUTPUT
45 -> 30 -> 66 -> 24 -> 75 -> 50
total head movement = 169
81
b) SCAN Disk Scheduling Algorithm source code
#include<stdio.h>
int absoluteValue(int); // Declaring function absoluteValue
void main()
{
int queue[25],n,headposition,i,j,k,seek=0, maxrange,
difference,temp,queue1[20],queue2[20],temp1=0,temp2=0;
float averageSeekTime;
82
{
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++;
}
}
83
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
//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;
84
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]);
85
}INPUT
170
43
140
24
16
190
OUTPUT
86
c) C-SCAN Disk Scheduling Algorithm source 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 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);
87
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
}
}
88
// 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];
}
}
INPUT
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
89
200
Enter the head movement direction for high 1 and for low 0
1
OUTPUT
Lab Task(s)
90