-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDISKSHED_SCAN.cpp
More file actions
109 lines (85 loc) · 2.83 KB
/
DISKSHED_SCAN.cpp
File metadata and controls
109 lines (85 loc) · 2.83 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
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
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int callSCAN(int arr[], int init, int maxreq, int len, bool isLeft) {
int sum = 0;
// Sorting the requests
sort(arr, arr + len);
// Finding the virtual location of init in the request array
int pos = lower_bound(arr, arr + len, init) - arr;
int left = pos - 1;
int right = pos;
// Moving towards the specified direction (left or right)
if (isLeft) {
// Moving towards the left side first in the array
sum += abs(init - arr[left]);
cout << arr[left] << endl;
while (left != 0) {
sum += abs(arr[left] - arr[left - 1]);
cout << arr[left - 1] << endl;
left--;
}
// Moving to track 0
sum += arr[0];
cout << "0" << endl;
// Now moving towards the right direction
sum += abs(0 - arr[right]);
cout << arr[right] << endl;
while (right < len - 1) {
sum += abs(arr[right] - arr[right + 1]);
cout << arr[right + 1] << endl;
right++;
}
} else {
// Moving towards the right side first in the array
sum += abs(init - arr[right]);
cout << arr[right] << endl;
while (right < len - 1) {
sum += abs(arr[right] - arr[right + 1]);
cout << arr[right + 1] << endl;
right++;
}
// Moving to the maximum possible track request from where the head will reverse its direction
sum += abs(arr[len - 1] - maxreq);
cout << maxreq << endl;
// Now moving towards the left direction
sum += abs(maxreq - arr[left]);
cout << arr[left] << endl;
while (left != 0) {
sum += abs(arr[left] - arr[left - 1]);
cout << arr[left - 1] << endl;
left--;
}
}
return sum;
}
int main() {
int maxreq;
cout << "Enter the maximum number of cylinders: ";
cin >> maxreq;
int len;
cout << "Enter the number of disk requests: ";
cin >> len;
int arr[len];
cout << "Enter the disk request queue: ";
for (int i = 0; i < len; ++i) {
cout << "Request " << (i + 1) << ":";
cin >> arr[i];
}
int init;
cout << "Enter the current head position: ";
cin >> init;
char direction;
cout << "Enter the direction of movement (L for left, R for right): ";
cin >> direction;
bool isLeft = false;
if (direction == 'L' || direction == 'l') {
isLeft = true;
}
int res = callSCAN(arr, init, maxreq, len, isLeft);
cout << "Total Head Movement: " << res << endl;
float tpt = (float)len / (float)res;
cout << "Throughput: " << tpt << endl;
return 0;
}