-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSCAN-Disk-Sceduling.c
66 lines (61 loc) · 1.99 KB
/
SCAN-Disk-Sceduling.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
#include<stdio.h>
#include<stdlib.h>
void scan(int Ar[20], int n, int start);
void sort(int Ar[20], int n);
int main() {
int diskQueue[20], n, start, i;
printf("Enter the size of Queue: ");
scanf("%d", &n);
printf("Enter the Queue: ");
for(i=1;i<=n;i++) { /* head element to be read */
scanf("%d",&diskQueue[i]);
}
printf("Enter the initial head position: ");
scanf("%d", &start);
diskQueue[0] = start; /* injecting to the first position */
++n;
sort(diskQueue, n); /* total of n+1 elements */
scan(diskQueue, n, start);
return 0;
}
void scan(int Ar[20], int n, int start) {
int i, pos, diff, seekTime=0, current;
// position of the disk to start seeking
for(i=0;i<n;i++) {
if(Ar[i]==start) {
pos=i;
break;
}
}
// start seeking to the right
printf("\nMovement of Cylinders\n");
for(i=pos;i<n-1;i++) {
diff = abs(Ar[i+1] - Ar[i]);
seekTime += diff;
printf("Move from %d to %d with seek time %d\n", Ar[i], Ar[i+1], diff);
}
current=i; /* last element position */
// start seeking to the left
for(i=pos-1;i>=0;i--) {
diff = abs(Ar[current] - Ar[i]);
seekTime += diff;
printf("Move from %d to %d with seek time %d\n", Ar[current], Ar[i], diff);
current=i;
}
printf("\nTotal Seek Time: %d", seekTime);
// average of entered elements(n-1) excluding head
printf("\nAverage Seek Time = %f",(float) seekTime/(n-1));
printf("\n");
}
void sort(int Ar[20], int n) {
int i, j, tmp;
for(i=0;i<n-1;i++) {
for(j=0;j<n-1-i;j++) {
if(Ar[j]>Ar[j+1]) {
tmp = Ar[j];
Ar[j] = Ar[j+1];
Ar[j+1] = tmp;
}
}
}
}