-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsjf_aging.cpp
More file actions
130 lines (107 loc) · 3.73 KB
/
sjf_aging.cpp
File metadata and controls
130 lines (107 loc) · 3.73 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
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int ct;
float tct=0.0;
float ATAT;
float AWT;
struct Process {
string name;
int arrivalTime;
int burstTime;
int age = 2;
Process(const string& name, int arrivalTime, int burstTime)
: name(name), arrivalTime(arrivalTime), burstTime(burstTime)
{}
};
// sort based on arrival time
struct ArrivalComparator {
bool operator()(const Process& p1, const Process& p2) {
return p1.arrivalTime < p2.arrivalTime;
}
};
// sort based on burst time
struct BurstComparator {
bool operator()(const Process& p1, const Process& p2) {
return p1.burstTime < p2.burstTime;
}
};
void calculate(const string& name, int at, int bt, int& currentTime) {
int st = currentTime;
ct = st + bt;
currentTime += bt;
int tat = ct - at;
int wt = tat - bt;
tct+=ct;
ATAT+=tat;
AWT+=wt;
cout << name << "\t\t" << at << "\t\t" << bt << "\t\t" << st << "\t\t\t" << ct << "\t\t\t" << tat << "\t\t\t" << wt << endl;
}
int main() {
vector<Process> processes;
vector<Process> temp;
int currentTime = 0;
cout << "Enter the number of processes: ";
int n;
cin >> n;
cout << "Enter process Arrival & Burst time:" << endl;
for (int i = 0; i < n; i++) {
cout << "For Process " << (i + 1) << ":" << endl;
int arrivalTime, burstTime;
cin >> arrivalTime >> burstTime;
processes.push_back(Process("P" + to_string(i + 1), arrivalTime, burstTime));
}
// sort based on arrival time
sort(processes.begin(), processes.end(), ArrivalComparator());
cout << "PID\t\t" << "Arrival\t\t" << "Burst\t\t" << "Starting\t\t" << "Completion\t\t" << "TurnAround\t\t" << "Waiting" << endl;
// calculate 1st process
calculate(processes[0].name, processes[0].arrivalTime, processes[0].burstTime, currentTime);
processes.erase(processes.begin());
while (!processes.empty() || !temp.empty()) {
// add processes which have arrived into the temporary list
if (!processes.empty()) {
for (int i = 0; i < processes.size(); i++) {
if (processes[i].arrivalTime <= currentTime) {
temp.push_back(processes[i]);
processes.erase(processes.begin() + i);
i--;
}
}
}
// if (!processes.empty()) {
// for (int i = 0; i < processes.size(); i++) {
// if (processes[i].arrivalTime <= currentTime) {
// temp.push_back(processes[i]);
// processes.erase(processes.begin() + i);
// i--;
// }
// }
// }
// sort based on burst time
sort(temp.begin(), temp.end(), BurstComparator());
bool flag = false;
for (int i = 0; i < temp.size(); i++) {
if (temp[i].age == 0) {
flag = true;
calculate(temp[i].name, temp[i].arrivalTime, temp[i].burstTime, currentTime);
temp.erase(temp.begin() + i);
i--;
} else {
flag = false;
}
}
if (!flag) {
calculate(temp[0].name, temp[0].arrivalTime, temp[0].burstTime, currentTime);
for (Process& p : temp) {
p.age -= 1;
}
temp.erase(temp.begin());
flag = false;
}
}
cout<<"Average waiting time:"<<AWT/n<<endl;
cout<<"Average turnaround time:"<<ATAT/n<<endl;
cout<<"Throughput :"<<(float)n/ct;
return 0;
}