-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiskSchedulingPolicy(CLOOK).cpp
57 lines (44 loc) · 1.3 KB
/
DiskSchedulingPolicy(CLOOK).cpp
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
#include <bits/stdc++.h>
using namespace std;
// direction towards large value
int findShortest(int head, int *queue, int n)
{
int index;
for (int i = 0; i < n; i++)
if (queue[i] < head)
index = i;
return index;
}
int main()
{
int head, n, range, total = 0;
cout << "Enter the head position: ";
cin >> head;
cout << "\nEnter the number of requests: ";
cin >> n;
cout << "\nEnter the range of cylinder: ";
cin >> range;
int queue[n];
cout << "\nEnter the cylinder numbers for the requests: ";
for (int i = 0; i < n; i++)
cin >> queue[i];
sort(queue, queue + n);
cout << "\nThe order of execution: " << endl;
int index = findShortest(head, queue, n);
total += abs(head - queue[index + 1]);
cout << head << "-->" << queue[index + 1] << endl;
for (int i = index + 1; i < n - 1; i++)
{
total += abs(queue[i + 1] - queue[i]);
cout << queue[i] << "-->" << queue[i + 1] << endl;
}
total += abs(queue[n - 1] - queue[0]);
cout << queue[n - 1] << "-->" << queue[0] << endl;
for (int i = 1; i <= index; i++)
{
total += abs(queue[i] - queue[i - 1]);
cout << queue[i - 1] << "-->" << queue[i] << endl;
}
cout << "\nTotal Headmovements: " << total;
return 0;
}