forked from VAR-solutions/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsjf.cpp
More file actions
67 lines (55 loc) · 1.6 KB
/
sjf.cpp
File metadata and controls
67 lines (55 loc) · 1.6 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
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int p[n], at[n], bt[n], wt[n], tat[n], twt = 0, ttat = 0;
for (int i = 0; i < n; ++i) {
cin >> p[i] >> at[i] >> bt[i];
}
int rt[n];
for (int i = 0; i < n; i++)
rt[i] = bt[i];
int complete = 0, t = 0, minm = INT_MAX;
int shortest = 0, finish_time;
bool check = false;
while (complete != n) {
for (int j = 0; j < n; j++) {
if ((at[j] <= t) && (rt[j] < minm) && rt[j] > 0) {
minm = rt[j];
shortest = j;
check = true;
}
}
if (check == false) {
t++;
continue;
}
rt[shortest]--;
minm = rt[shortest];
if (minm == 0)
minm = INT_MAX;
if (rt[shortest] == 0) {
complete++;
check = false;
finish_time = t + 1;
wt[shortest] = finish_time - bt[shortest] - at[shortest];
if (wt[shortest] < 0)
wt[shortest] = 0;
}
t++;
}
for (int i = 0; i < n; i++)
tat[i] = bt[i] + wt[i];
cout << "Processes " <<" Burst time " <<" Waiting time " << " Turn around time\n";
for (int i = 0; i < n; i++) {
twt = twt + wt[i];
ttat = ttat + tat[i];
cout << " " << p[i] << "\t\t" <<bt[i] << "\t\t " << wt[i] <<"\t\t " << tat[i] << endl;
}
cout << "\nAverage waiting time = " <<
(float) twt / (float) n;
cout << "\nAverage turn around time = " <<
(float) ttat / (float) n;
return 0;
}