-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisk Arm Scheduling.cpp
152 lines (135 loc) · 3.71 KB
/
Disk Arm Scheduling.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
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
144
145
146
147
148
149
150
151
152
#include<bits/stdc++.h>
using namespace std;
vector<int>requests;
void FCFS(int head)
{
vector<int>r;
int movement=0;
r = requests;
cout << "_______FCFS_______" <<endl<<endl;
cout << "Cylinder Serving Order : "<<head;
for(int i=0; i<r.size(); i++){
movement+= abs(head-r[i]);
head = r[i];
cout << " -> "<<r[i];
}
cout << endl << "Total Cylinder Movement : "<<movement << endl<<endl;
}
void SSTF(int head)
{
vector<int> r;
int index;
int movement=0;
r = requests;
r.push_back(head);
sort(r.begin(),r.end());
cout << "_______SSTF_______" <<endl<<endl;
cout << "Cylinder Serving Order : "<<head;
//get current head position
for(int i=0; i<r.size(); i++){
if(head==r[i]){
index = i;
break;
}
}
while(r.size()>1){
if(index-1>-1 && index+1<r.size()){
int prev = head - r[index-1];
int next = r[index+1] - head;
if(prev<next){
movement+=prev;
r.erase(r.begin()+index);
head = r[index-1];
index--;
cout << " -> " << head;
}else{
movement+=next;
r.erase(r.begin()+index);
head = r[index];
cout << " -> " << head;
}
}else if(index<1){
int next = r[index+1] - head;
r.erase(r.begin()+index);
head=r[index];
movement+=next;
cout << " -> " << head;
}else{
int prev = head - r[index-1];
movement+=prev;
r.erase(r.begin()+index);
head = r[index-1];
index--;
cout << " -> " << head;
}
}
cout << endl << "Total Cylinder Movement : "<<movement << endl<<endl;
}
void CSCAN(int head,int no_of_heads)
{
vector<int> r;
int index;
int movement=0;
r = requests;
r.push_back(head);
r.push_back(0);
r.push_back(no_of_heads-1);
sort(r.begin(),r.end());
//get current head position
for(int i=0; i<r.size(); i++){
if(head==r[i]){
index = i;
break;
}
}
cout << "_______CSACN_______" <<endl<<endl;
cout << "Cylinder Serving Order : "<<head;
for(int i=index; i<r.size()-1; i++){
movement+=(r[i+1] - r[i]);
cout << " -> "<<r[i+1];
}
movement+=(no_of_heads-1);
cout << " -> " << r[0];
for(int i=0; i<index-1; i++){
movement+= (r[i+1] - r[i]);
cout << " -> "<<r[i+1];
}
cout << endl << "Total Cylinder Movement : "<<movement << endl<<endl;
}
int main()
{
int no_of_heads,min_head,max_head,no_of_requests,Head;
cout << "Number of Heads : ";
cin >> no_of_heads;
cout << "Number of Cylinder Requests : ";
cin >> no_of_requests;
cout << "Cylinder Requests : ";
for (int i = 0; i < no_of_requests; ++i) {
int value;
cin >> value;
requests.push_back(value);
}
cout << "Current Head : ";
cin >> Head;
FCFS(Head);
SSTF(Head);
CSCAN(Head,no_of_heads);
return 0;
}
/**
Input 0:
200
8
98 183 37 122 14 124 65 67
53
Output 0:
_______FCFS_______
Cylinder Serving Order : 53 -> 98 -> 183 -> 37 -> 122 -> 14 -> 124 -> 65 -> 67
Total Cylinder Movement : 640
_______SSTF_______
Cylinder Serving Order : 53 -> 65 -> 67 -> 37 -> 14 -> 98 -> 122 -> 124 -> 183
Total Cylinder Movement : 236
_______CSACN_______
Cylinder Serving Order : 53 -> 65 -> 67 -> 98 -> 122 -> 124 -> 183 -> 199 -> 0 -> 14 -> 37
Total Cylinder Movement : 382
*/