-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
204 lines (193 loc) · 9.46 KB
/
main.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <iostream>
#include <algorithm>
#include <queue>
const int N = 100 + 10;
struct Process{
long id;
double arrive_time;
double serve_time;
double finish_time;
double round_time;
double weighted_time;
}process[N];
struct Result{
double average_time, weighted_average_time;
}result_FCFS, result_SPN, result_RR;
struct Result FCFS(int n){
std::sort(process, process + n, [&](Process a, Process b){return a.arrive_time < b.arrive_time;});
double sum_round_time = 0, sum_weighted_time = 0;
for(int i = 0 ; i < n ; i++){
if(i == 0){
process[i].finish_time = process[i].arrive_time + process[i].serve_time;
process[i].round_time = process[i].finish_time - process[i].arrive_time;
process[i].weighted_time = process[i].round_time / process[i].serve_time;
}
else{
if(process[i].arrive_time < process[i-1].finish_time){
process[i].finish_time = process[i-1].finish_time + process[i].serve_time;
process[i].round_time = process[i].finish_time - process[i].arrive_time;
process[i].weighted_time = process[i].round_time / process[i].serve_time;
}
else{
process[i].finish_time = process[i].arrive_time + process[i].serve_time;
process[i].round_time = process[i].finish_time - process[i].arrive_time;
process[i].weighted_time = process[i].round_time / process[i].serve_time;
}
}
sum_round_time += process[i].round_time;
sum_weighted_time += process[i].weighted_time;
}
printf("--------------------------------------------FCFS---------------------------------------------\n");
printf("ProcessID\tArrvieTime\tServeTime\tFinishTime\tRoundTime\tWeightedTime\n");
for(int i = 0 ; i < n ; i++){
printf("%ld\t\t%.1lf\t\t%.1lf\t\t%.1lf\t\t%.1lf\t\t%.1lf\n", process[i].id, process[i].arrive_time, process[i].serve_time, process[i].finish_time, process[i].round_time, process[i].weighted_time);
}
printf("\nAverageRoundTime = %.2lf\t\tAverageWeightedTime = %.2lf\n", sum_round_time * 1.0 / n, sum_weighted_time * 1.0 / n);
printf("---------------------------------------------------------------------------------------------\n\n");
return {sum_round_time * 1.0 / n, sum_weighted_time * 1.0 / n};
}
struct Result SPN(int n){
std::sort(process, process + n, [&](Process a, Process b){return a.arrive_time < b.arrive_time;});
double sum_round_time = 0, sum_weighted_time = 0;
for(int i = 0 ; i < n ; i++){
if(i == 0){
process[i].finish_time = process[i].arrive_time + process[i].serve_time;
process[i].round_time = process[i].finish_time - process[i].arrive_time;
process[i].weighted_time = process[i].round_time / process[i].serve_time;
}
else{
for(int j = i ; j < n ; j++){
if(process[j].arrive_time <= process[i-1].finish_time){
if(process[j].serve_time < process[i].serve_time){
std::swap(process[i], process[j]);
}
}
else{
break;
}
}
if(process[i].arrive_time < process[i-1].finish_time){
process[i].finish_time = process[i-1].finish_time + process[i].serve_time;
process[i].round_time = process[i].finish_time - process[i].arrive_time;
process[i].weighted_time = process[i].round_time / process[i].serve_time;
}
else{
process[i].finish_time = process[i].arrive_time + process[i].serve_time;
process[i].round_time = process[i].finish_time - process[i].arrive_time;
process[i].weighted_time = process[i].round_time / process[i].serve_time;
}
}
sum_round_time += process[i].round_time;
sum_weighted_time += process[i].weighted_time;
}
printf("---------------------------------------------SPN---------------------------------------------\n");
printf("ProcessID\tArrvieTime\tServeTime\tFinishTime\tRoundTime\tWeightedTime\n");
for(int i = 0 ; i < n ; i++){
printf("%ld\t\t%.1lf\t\t%.1lf\t\t%.1lf\t\t%.1lf\t\t%.1lf\n", process[i].id, process[i].arrive_time, process[i].serve_time, process[i].finish_time, process[i].round_time, process[i].weighted_time);
}
printf("\nAverageRoundTime = %.2lf\t\tAverageWeightedTime = %.2lf\n", sum_round_time * 1.0 / n, sum_weighted_time * 1.0 / n);
printf("---------------------------------------------------------------------------------------------\n\n");
return {sum_round_time * 1.0 / n, sum_weighted_time * 1.0 / n};
}
struct Result RR(int n, double slice_time){
std::sort(process, process + n, [&](Process a, Process b){return a.arrive_time < b.arrive_time;});
int unused_process_position = 0;
double present_time = 0;
double sum_round_time = 0, sum_weighted_time = 0;
std::queue<std::pair<int, double>> queue; // First element means Process ID, Second element means Rest Time for each process.
while(true){
if(!queue.empty()){
auto tmp = queue.front();
queue.pop();
if(tmp.second <= slice_time){
present_time += tmp.second;
process[tmp.first].finish_time = present_time;
process[tmp.first].round_time = process[tmp.first].finish_time - process[tmp.first].arrive_time;
process[tmp.first].weighted_time = process[tmp.first].round_time / process[tmp.first].serve_time;
sum_round_time += process[tmp.first].round_time;
sum_weighted_time += process[tmp.first].weighted_time;
for(int i = unused_process_position; i < n ; i++){
if(process[i].arrive_time <= present_time){
queue.push({i, process[i].serve_time});
if(i == n - 1){
unused_process_position = n;
}
}
else{
unused_process_position = i;
break;
}
}
}
else{ //tmp.second(rest time for process) > slice_time, need another slice to work.
present_time += slice_time;
for(int i = unused_process_position; i < n ; i++){
if(process[i].arrive_time <= present_time){
queue.push({i, process[i].serve_time});
if(i == n - 1){
unused_process_position = n;
}
}
else{
unused_process_position = i;
break;
}
}
queue.push({tmp.first, tmp.second - slice_time});
}
}
else{
if(unused_process_position == n){
break;
}
else{
present_time = process[unused_process_position].arrive_time;
queue.push({unused_process_position, process[unused_process_position].serve_time});
unused_process_position++;
}
}
}
printf("---------------------------------------------RR----------------------------------------------\n");
printf("ProcessID\tArrvieTime\tServeTime\tFinishTime\tRoundTime\tWeightedTime\n");
for(int i = 0 ; i < n ; i++){
printf("%ld\t\t%.1lf\t\t%.1lf\t\t%.1lf\t\t%.1lf\t\t%.1lf\n", process[i].id, process[i].arrive_time, process[i].serve_time, process[i].finish_time, process[i].round_time, process[i].weighted_time);
}
printf("\nAverageRoundTime = %.2lf\t\tAverageWeightedTime = %.2lf\n", sum_round_time * 1.0 / n, sum_weighted_time * 1.0 / n);
printf("---------------------------------------------------------------------------------------------\n\n");
return {sum_round_time * 1.0 / n, sum_weighted_time * 1.0 / n};
}
int main(){
int n;
int slice_time;
std::cout << "The Number Of Process = ";
std::cin >> n;
if(n > N){
std::cout << "Too Many Processes!\n";
return 0;
}
std::cout << "Slice Time = ";
std::cin >> slice_time;
std::cout << std::endl;
for(int i = 0 ; i < n ; i++){
std::cout << "Process ID = " << i << std::endl;
process[i].id = i;
std::cout << "Process Arrive Time = ";
std::cin >> process[i].arrive_time;
std::cout << "Process Serve Time = ";
std::cin >> process[i].serve_time;
std::cout << std::endl;
}
printf("-----------------Input--------------------\n");
printf("ProcessID\tArrvieTime\tServeTime\n");
for(int i = 0 ; i < n ; i++){
printf("%ld\t\t%.1lf\t\t%.1lf\n", process[i].id, process[i].arrive_time, process[i].serve_time);
}
printf("------------------------------------------\n\n");
result_FCFS = FCFS(n);
result_SPN = SPN(n);
result_RR = RR(n, slice_time);
std::cout << "FCFS : " << "Average Time : " << result_FCFS.average_time << "\t Weighted Average Time : " << result_FCFS.weighted_average_time << std::endl;
std::cout << "SPN : " << " Average Time : " << result_SPN.average_time << "\t Weighted Average Time : " << result_SPN.weighted_average_time << std::endl;
std::cout << "RR : " << " Average Time : " << result_RR.average_time << "\t Weighted Average Time : " << result_RR.weighted_average_time << std::endl;
return 0;
}