-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcscan.c
More file actions
65 lines (64 loc) · 1.37 KB
/
Copy pathcscan.c
File metadata and controls
65 lines (64 loc) · 1.37 KB
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
#include <stdio.h>
int head, a[20], i, distance, n, seektime, size;
void bubbleSort(int arr[], int n) {
int temp;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
printf("\nEnter Head position: ");
scanf("%d", &head);
printf("\nEnter number of disk requests: ");
scanf("%d", &n);
printf("\nEnter the disk size: ");
scanf("%d", &size);
printf("\nEnter the disk requests: ");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
a[n] = head;
n++;
bubbleSort(a, n);
int pos;
for (i = 0; i < n; i++) {
if (a[i] == head) {
pos = i;
break;
}
}
printf("\n\tC-SCAN DISK SCHEDULING\n\n");
for (i = pos; i < n - 1; i++) {
distance = a[i + 1] - a[i];
printf("Head movement from %d to %d : %d\n", a[i], a[i + 1],
distance);
seektime += distance;
}
if (a[n - 1] != size - 1) {
distance = (size - 1) - a[n - 1];
printf("Head movement from %d to %d : %d\n", a[n - 1], size - 1,
distance);
seektime += distance;
}
distance = size - 1;
printf("Head movement from %d to 0 : %d\n", size - 1, distance);
seektime += distance;
for (i = 0; i < pos; i++) {
if (i == 0) {
distance = a[i];
} else {
distance = a[i] - a[i - 1];
}
printf("Head movement from %d to %d : %d\n", i == 0 ? size - 1 :
a[i - 1], a[i], distance);
seektime += distance;
}
printf("\nTotal seek time is : %d\n", seektime);
return 0;
}