-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRound Robin.cpp
More file actions
83 lines (67 loc) · 2.17 KB
/
Copy pathRound Robin.cpp
File metadata and controls
83 lines (67 loc) · 2.17 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
#include<bits/stdc++.h>
using namespace std;
double switching_time = 0.5;
int cnt = 0;
struct process {
string name;
int at, bt, remaining_bt, ct, tat, wt;
};
vector<process> pro = {
{"P1", 0, 8, 8, 0, 0, 0}, // at, bt, remaining bt, ct, tat, wt
{"P2", 1, 5, 5, 0, 0, 0},
{"P4", 3, 4, 4, 0, 0, 0},
{"P3", 2, 10, 10, 0, 0, 0},
};
bool compareByAT(const process &p1, const process &p2){
return p1.at < p2.at; // sort ascending order
}
void solve(){
//sort by arrival time
sort(pro.begin(), pro.end(), compareByAT);
int n = pro.size();
vector<bool>visited(n, 0);
int completed = 0;
int tc = 3;
int current_time = 0;
while(completed<n){
for(int i = 0; i<n; i++){
if(visited[i] == 0 && pro[i].at<= current_time){
if(pro[i].remaining_bt == tc){
pro[i].remaining_bt -= tc;
current_time += tc;
visited[i] = 1;
completed++;
pro[i].ct = current_time;
pro[i].tat = pro[i].ct - pro[i].at;
pro[i].wt = pro[i].tat - pro[i].bt;
}
else if(pro[i].remaining_bt<tc){
current_time += pro[i].remaining_bt;
pro[i].remaining_bt = 0;
visited[i] = 1;
completed++;
pro[i].ct = current_time;
pro[i].tat = pro[i].ct - pro[i].at;
pro[i].wt = pro[i].tat - pro[i].bt;
}
else{
pro[i].remaining_bt -= tc;
current_time += tc;
}
}
}
}
}
void show() {
cout << setw(10) << "Process" << setw(10) << "AT" << setw(10) << "BT" << setw(10) << "CT"
<< setw(10) << "TAT" << setw(10) << "WT" << endl;
for (auto &p : pro) {
cout << setw(10) << p.name << setw(10) << p.at << setw(10) << p.bt << setw(10) << p.ct
<< setw(10) << p.tat << setw(10) << p.wt << endl;
}
}
int main()
{
solve();
show();
}