-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCAN_disk_scheduling.c
149 lines (122 loc) · 4.51 KB
/
SCAN_disk_scheduling.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// C Program to Simulate SCAN Disk Scheduling Algorithm
// SCAN is a disk-scheduling algorithm to determine the
// motion of the disk's arm and head in servicing read
// and write requests.It is also called the "elevator"
// algorithm because it behaves like an elevator. An
// elevator continues to travel in its current direction
// (up or down) until it is empty, stopping only to let
// individuals off or pick up new individuals on its
// path. From am implementation perspective, the drive
// maintains a buffer of pending read/write requests, along
// with the associated cylinder number of the request, ib
// which lower cylinder numbers indicate that the cylinder
// is closer to the spindle and higher numbers indicate
// it is father away.
#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;
// Reading the maximum Range of the Disk.
printf("Enter the maximum range of Disk: ");
scanf("%d",&maxrange);
// Reading the number of Queue Requests(Disk access requests)
printf("Enter the number of queue requests: ");
scanf("%d",&n);
// Reading the initial head position.(ie. the starting point of execution)
printf("Enter the initial head position: ");
scanf("%d",&headposition);
// Reading disk positions to be read in the order of arrival
printf("Enter the disk positions to be read(queue): ");
for(i=1;i<=n;i++) // Note that i varies from 1 to n instead of 0 to n-1
{
scanf("%d",&temp); //Reading position value to a temporary variable
//Now if the requested position is greater than current headposition,
//then pushing that to array queue1
if(temp>headposition)
{
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++;
}
}
//Now we have to sort the two arrays
//SORTING array queue1[] in ascending order
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
//SORTING array queue2[] in descending order
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]<queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
//Copying first array queue1[] into queue[]
for(i=1,j=0;j<temp1;i++,j++)
{
queue[i]=queue1[j];
}
//Setting queue[i] to maxrange because the head goes to
//end of disk and comes back in scan Algorithm
queue[i]=maxrange;
//Copying second array queue2[] after that first one is copied, into queue[]
for(i=temp1+2,j=0;j<temp2;i++,j++)
{
queue[i]=queue2[j];
}
//Setting queue[i] to 0. Because that is the innermost cylinder.
queue[i]=0;
//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;
// Calculating SEEK TIME. seek is initially set to 0 in the declaration part.
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]);
// Adding difference to the current seek time value
seek = seek + difference;
// Displaying a message to show the movement of disk head
printf("Disk head moves from position %d to %d with Seek %d \n", queue[j], queue[j+1], difference);
}
// Calculating Average Seek time
averageSeekTime = seek/(float)n;
//Display Total and Average Seek Time(s)
printf("Total Seek Time= %d\n", seek);
printf("Average Seek Time= %f\n", averageSeekTime);
}
// Defining function absoluteValue
int absoluteValue(int x)
{
if(x>0)
{
return x;
}
else
{
return x*-1;
}
}