@@ -6,33 +6,41 @@ using namespace std;
6
6
/* * Global Constants **/
7
7
const string TRACE = " trace" ;
8
8
const string SHOW_STATISTICS = " stats" ;
9
- const string ALGORITHMS[8 ] = {" " ," FCFS" ," RR" ," SPN" ," SRT" ," HRRN" ," FB-1" ," FB-2i" };
9
+ const string ALGORITHMS[9 ] = {" " ," FCFS" ," RR" ," SPN" ," SRT" ," HRRN" ," FB-1" ," FB-2i" , " AGING " };
10
10
11
- bool sortByServiceTime (const tuple<string, int , int >& a,const tuple<string, int , int >& b){
11
+ bool sortByServiceTime (const tuple<string, int , int >& a,const tuple<string, int , int >& b)
12
+ {
12
13
return (get<2 >(a) < get<2 >(b));
13
14
}
14
- bool sortByArrivalTime (const tuple<string, int , int >& a,const tuple<string, int , int >& b){
15
+ bool sortByArrivalTime (const tuple<string, int , int >& a,const tuple<string, int , int >& b)
16
+ {
15
17
return (get<1 >(a) < get<1 >(b));
16
18
}
17
19
18
- string getProcessName (tuple<string, int , int >& a){
20
+ string getProcessName (tuple<string, int , int >& a)
21
+ {
19
22
return get<0 >(a);
20
23
}
21
24
22
- int getArrivalTime (tuple<string, int , int >& a){
25
+ int getArrivalTime (tuple<string, int , int >& a)
26
+ {
23
27
return get<1 >(a);
24
28
}
25
29
26
- int getServiceTime (tuple<string, int , int >& a){
30
+ int getServiceTime (tuple<string, int , int >& a)
31
+ {
27
32
return get<2 >(a);
28
33
}
29
- void decrementServiceTime (tuple<string, int , int >& a){
34
+ void decrementServiceTime (tuple<string, int , int >& a)
35
+ {
30
36
get<2 >(a) = get<2 >(a)-1 ;
31
37
}
32
38
33
- void firstComeFirstServe (){
39
+ void firstComeFirstServe ()
40
+ {
34
41
int time=getArrivalTime (processes[0 ]);
35
- for (int i=0 ;i<processes.size ();i++){
42
+ for (int i=0 ; i<process_count; i++)
43
+ {
36
44
string processName = getProcessName (processes[i]);
37
45
int processIndex = processToIndex[processName];
38
46
int arrivalTime = getArrivalTime (processes[i]);
@@ -42,41 +50,48 @@ void firstComeFirstServe(){
42
50
turnAroundTime[processIndex]= (finishTime[processIndex]-arrivalTime);
43
51
normTurn[processIndex]= (turnAroundTime[processIndex]*1.0 /serviceTime);
44
52
45
- for (int j = time;j<finishTime[processIndex];j++)
53
+ for (int j = time; j<finishTime[processIndex]; j++)
46
54
timeline[j][processIndex]=' *' ;
47
- for (int j=arrivalTime;j<time;j++)
55
+ for (int j=arrivalTime; j<time; j++)
48
56
timeline[j][processIndex]=' .' ;
49
57
time+=serviceTime;
50
58
}
51
59
}
52
60
53
- void roundRobin (){
61
+ void roundRobin ()
62
+ {
54
63
55
64
}
56
65
57
- void shortestProcessNext (){
66
+ void shortestProcessNext ()
67
+ {
58
68
priority_queue<pair<int ,int >,vector<pair<int ,int >>,greater<pair<int ,int >>>pq; // pair of service time and index
59
69
int j=0 ;
60
- for (int i=0 ;i<last_instant;i++){
61
- for (;j<process_count;){
62
- if (getArrivalTime (processes[j])<=i){
70
+ for (int i=0 ; i<last_instant; i++)
71
+ {
72
+ for (; j<process_count;)
73
+ {
74
+ if (getArrivalTime (processes[j])<=i)
75
+ {
63
76
pq.push (make_pair (getServiceTime (processes[j]),j));
64
77
j++;
65
- }else
78
+ }
79
+ else
66
80
break ;
67
81
}
68
- if (!pq.empty ()){
82
+ if (!pq.empty ())
83
+ {
69
84
int processIndex = pq.top ().second ;
70
85
int arrivalTime = getArrivalTime (processes[processIndex]);
71
86
int serviceTime = getServiceTime (processes[processIndex]);
72
87
pq.pop ();
73
88
74
89
int temp = arrivalTime;
75
- for (;temp<i;temp++)
90
+ for (; temp<i; temp++)
76
91
timeline[temp][processIndex]=' .' ;
77
92
78
93
temp = i;
79
- for (;temp<i+serviceTime;temp++)
94
+ for (; temp<i+serviceTime; temp++)
80
95
timeline[temp][processIndex]=' *' ;
81
96
82
97
finishTime[processIndex]= (i+serviceTime);
@@ -87,108 +102,132 @@ void shortestProcessNext(){
87
102
}
88
103
}
89
104
90
- void shortestRemainingTime (){
105
+ void shortestRemainingTime ()
106
+ {
91
107
priority_queue<pair<int ,int >,vector<pair<int ,int >>,greater<pair<int ,int >>>pq;
92
108
int j=0 ;
93
- for (int i=0 ;i<last_instant;i++){
94
- for (;j<process_count;){
95
- if (getArrivalTime (processes[j])==i){
109
+ for (int i=0 ; i<last_instant; i++)
110
+ {
111
+ for (; j<process_count;)
112
+ {
113
+ if (getArrivalTime (processes[j])==i)
114
+ {
96
115
pq.push (make_pair (getServiceTime (processes[j]),j));
97
116
j++;
98
- }else
117
+ }
118
+ else
99
119
break ;
100
120
}
101
- if (!pq.empty ()){
121
+ if (!pq.empty ())
122
+ {
102
123
int processIndex = pq.top ().second ;
103
124
int remainingTime = pq.top ().first ;
104
125
pq.pop ();
105
126
int serviceTime = getServiceTime (processes[processIndex]);
106
127
int arrivalTime= getArrivalTime (processes[processIndex]);
107
128
timeline[i][processIndex]= ' *' ;
108
129
109
- if (remainingTime==1 ){// process finished
130
+ if (remainingTime==1 ) // process finished
131
+ {
110
132
finishTime[processIndex]= i+1 ;
111
133
turnAroundTime[processIndex]= (finishTime[processIndex]-arrivalTime);
112
134
normTurn[processIndex]= (turnAroundTime[processIndex]*1.0 /serviceTime);
113
- }else {
135
+ }
136
+ else
137
+ {
114
138
pq.push (make_pair (remainingTime-1 ,processIndex));
115
139
}
116
140
}
117
141
}
118
142
119
- for (int i=0 ;i<process_count;i++){
143
+ for (int i=0 ; i<process_count; i++)
144
+ {
120
145
int arrivalTime = getArrivalTime (processes[i]);
121
- for (int k=arrivalTime;k<finishTime[i];k++){
146
+ for (int k=arrivalTime; k<finishTime[i]; k++)
147
+ {
122
148
if (timeline[k][i]!=' *' )
123
149
timeline[k][i]=' .' ;
124
150
}
125
151
}
126
152
}
127
153
128
- void highestResponseRatioNext (){
154
+ void highestResponseRatioNext ()
155
+ {
129
156
130
157
}
131
158
132
- void feedbackQ1 (){
159
+ void feedbackQ1 ()
160
+ {
133
161
134
162
}
135
163
136
- void feedbackQ2i (){
164
+ void feedbackQ2i ()
165
+ {
137
166
138
167
}
139
168
140
- void printAlgorithm (){
169
+ void printAlgorithm ()
170
+ {
141
171
cout<<ALGORITHMS[stoi (algorithms[0 ])]<<endl;
142
172
}
143
173
144
- void printProcesses (){
174
+ void printProcesses ()
175
+ {
145
176
cout<<" Process " ;
146
- for (int i=0 ;i<process_count;i++)
177
+ for (int i=0 ; i<process_count; i++)
147
178
cout<<" | " <<getProcessName (processes[i])<<" " ;
148
179
cout<<" |" <<endl;
149
180
}
150
- void printArrivalTime (){
181
+ void printArrivalTime ()
182
+ {
151
183
cout<<" Arrival " ;
152
- for (int i=0 ;i<process_count;i++)
184
+ for (int i=0 ; i<process_count; i++)
153
185
cout<<" | " <<getArrivalTime (processes[i])<<" " ;
154
186
155
187
cout<<" |" <<endl;
156
188
}
157
- void printServiceTime (){
189
+ void printServiceTime ()
190
+ {
158
191
cout<<" Service " ;
159
- for (int i=0 ;i<process_count;i++)
192
+ for (int i=0 ; i<process_count; i++)
160
193
cout<<" | " <<getServiceTime (processes[i])<<" " ;
161
194
cout<<" | Mean|" <<endl;
162
195
}
163
- void printFinishTime (){
196
+ void printFinishTime ()
197
+ {
164
198
cout<<" Finish " ;
165
- for (int i=0 ;i<process_count;i++)
199
+ for (int i=0 ; i<process_count; i++)
166
200
cout<<" | " <<finishTime[i]<<" " ;
167
201
cout<<" |-----|" <<endl;
168
202
}
169
- void printTurnAroundTime (){
203
+ void printTurnAroundTime ()
204
+ {
170
205
cout<<" Turnaround " ;
171
206
int sum =0 ;
172
- for (int i=0 ;i<process_count;i++){
207
+ for (int i=0 ; i<process_count; i++)
208
+ {
173
209
cout<<" | " <<turnAroundTime[i]<<" " ;
174
210
sum+=turnAroundTime[i];
175
211
}
176
212
cout<<fixed<<setprecision (2 );
177
213
cout<<" | " <<(1.0 *sum/turnAroundTime.size ())<<" |" <<endl;
178
214
}
179
215
180
- void printNormTurn (){
216
+ void printNormTurn ()
217
+ {
181
218
cout<<" NormTurn " ;
182
219
cout<<fixed<<setprecision (2 );
183
220
float sum =0 ;
184
- for (int i=0 ;i<process_count;i++){
221
+ for (int i=0 ; i<process_count; i++)
222
+ {
185
223
cout<<" | " <<normTurn[i]<<" " ;
186
224
sum+=normTurn[i];
187
225
}
188
226
189
227
cout<<" | " <<(1.0 *sum/normTurn.size ())<<" |" <<endl;
190
228
}
191
- void printStats (){
229
+ void printStats ()
230
+ {
192
231
printAlgorithm ();
193
232
printProcesses ();
194
233
printArrivalTime ();
@@ -198,15 +237,18 @@ void printStats(){
198
237
printNormTurn ();
199
238
}
200
239
201
- void printTimeline (){
240
+ void printTimeline ()
241
+ {
202
242
cout<<ALGORITHMS[stoi (algorithms[0 ])]<<" " ;
203
- for (int i=0 ;i<=last_instant;i++)
243
+ for (int i=0 ; i<=last_instant; i++)
204
244
cout<<" " <<i%10 <<" " ;
205
245
cout<<endl;
206
246
cout<<" ------------------------------------------------" <<endl;
207
- for (int i=0 ;i<process_count;i++){
247
+ for (int i=0 ; i<process_count; i++)
248
+ {
208
249
cout<<getProcessName (processes[i])<<" \t " ;
209
- for (int j=0 ;j<last_instant;j++){
250
+ for (int j=0 ; j<last_instant; j++)
251
+ {
210
252
cout<<" |" <<timeline[j][i];
211
253
}
212
254
cout<<" |" <<endl;
@@ -217,9 +259,21 @@ void printTimeline(){
217
259
218
260
int main ()
219
261
{
220
- freopen (" input.txt" ," r" ,stdin);
262
+ // freopen("input.txt","r",stdin);
221
263
parse ();
222
- shortestProcessNext ();
264
+
265
+ /*
266
+ cout << endl;
267
+ printAlgorithm();
268
+ printProcesses();
269
+ printArrivalTime();
270
+ printServiceTime();
271
+ cout << endl;
272
+ */
273
+
274
+ // shortestProcessNext();
275
+ firstComeFirstServe ();
276
+
223
277
printTimeline ();
224
278
return 0 ;
225
279
}
0 commit comments