-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLJF.cpp
More file actions
51 lines (41 loc) · 1.31 KB
/
LJF.cpp
File metadata and controls
51 lines (41 loc) · 1.31 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
#include <iostream>
#include <algorithm>
using namespace std;
struct Process {
int pid; // process id
int at; // arrival time
int bt; // burst time
};
bool compareAT(const Process& p1, const Process& p2) {
return p1.at < p2.at;
}
int main() {
int n;
cout << "Enter the number of processes: ";
cin >> n;
Process* processes = new Process[n];
cout << "Enter process details (Pid, AT, BT) one by one:\n";
for (int i = 0; i < n; ++i) {
cin >> processes[i].pid >> processes[i].at >> processes[i].bt;
}
sort(processes, processes + n, compareAT);
int clock = 0;
double tsum = 0, wsum = 0;
cout << "Pid \t AT \t BT \t CT \t TAT \t WT\n";
for (int i = 0; i < n; ++i) {
clock = max(clock, processes[i].at);
int ct = clock + processes[i].bt;
int tat = ct - processes[i].at;
int wt = tat - processes[i].bt;
tsum += tat;
wsum += wt;
cout << processes[i].pid << "\t" << processes[i].at << "\t" << processes[i].bt << "\t" << ct << "\t" << tat << "\t" << wt << endl;
clock = ct;
}
double tavg = tsum / n;
double wavg = wsum / n;
cout << "Tsum = " << tsum << " \t WT sum = " << wsum << endl;
cout << "Tavg = " << tavg << " \t WT avg = " << wavg << endl;
delete[] processes;
return 0;
}