-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfcfs.cpp
More file actions
73 lines (57 loc) · 1.49 KB
/
Copy pathfcfs.cpp
File metadata and controls
73 lines (57 loc) · 1.49 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
#include<bits/stdc++.h>
using namespace std;
struct process{
string name;
int at, bt;
};
// List of processes
vector<process> pro = {
{"P1", 0, 7}, //name, at , bt
{"P2", 3, 4},
{"P3", 2, 3}
};
bool compareByAT(const process &p1, const process &p2){
return p1.at < p2.at; // sort ascending order
}
// different times
struct times{
string name;
int at, bt, ct, tat, wt;
times(string name, int at, int bt, int ct, int tat, int wt){
this-> name = name;
this-> at = at;
this-> bt = bt;
this-> ct = ct;
this-> tat = tat;
this-> wt = wt;
}
};
vector<times> table; // vector of diffrent times
void show(){
cout<<setw(10)<<"name"<<setw(10)<<"at"<<setw(10)<<"bt"<<setw(10)<<"ct"<<setw(10)<<"tat"<<setw(10)<<"wt"<<endl;
for(auto &p:table){
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;
}
}
void solve(){
// sort processes by at
sort(pro.begin(), pro.end(), compareByAT);
int ct_prev = 0;
for(int i = 0; i<pro.size(); i++){
string name;
int at, bt, ct, tat, wt;
name = pro[i].name;
at = pro[i].at;
bt = pro[i].bt;
ct = bt + max(ct_prev, at);
tat = ct - at;
wt = tat - bt;
table.push_back(times(name,at,bt,ct,tat,wt));
ct_prev = ct;
}
}
int main()
{
solve();
show();
}