-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsjf.cpp
More file actions
132 lines (106 loc) · 4.02 KB
/
Copy pathsjf.cpp
File metadata and controls
132 lines (106 loc) · 4.02 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
131
132
#include <iostream>
using namespace std;
struct process {
string p;
int at;
int bt;
};
class ShortestJob {
public:
void WaitingTime(process proc[], int n, int wt[], string gantt_p[], int gantt_t[], int& gantt_size) {
int rt[n];
for (int i = 0; i < n; i++)
rt[i] = proc[i].bt;
int complete = 0, t = 0, minm = INT_MAX;
int shortest = 0, finish_time;
bool check = false;
gantt_size = 0;
while (complete != n) {
for (int j = 0; j < n; j++) {
if ((proc[j].at <= t) && (rt[j] < minm) && rt[j] > 0) {
minm = rt[j];
shortest = j;
check = true;
}
}
if (check == false) {
t++;
continue;
}
// Execute the process for 1 unit time
rt[shortest]--;
minm = rt[shortest];
// Add process to Gantt chart if it just started or if it changes from a different process
if (gantt_size == 0 || gantt_p[gantt_size - 1] != proc[shortest].p) {
gantt_p[gantt_size] = proc[shortest].p;
gantt_t[gantt_size] = t; // Start time of the process in Gantt chart
gantt_size++;
}
if (minm == 0)
minm = INT_MAX;
if (rt[shortest] == 0) {
complete++;
check = false;
finish_time = t + 1;
wt[shortest] = finish_time - proc[shortest].bt - proc[shortest].at;
if (wt[shortest] < 0)
wt[shortest] = 0;
}
t++;
}
// Add final time for the last process in the Gantt chart
gantt_t[gantt_size] = t; // End time of the last executed process
}
void TurnAroundTime(process proc[], int n, int wt[], int tat[]) {
for (int i = 0; i < n; i++)
tat[i] = proc[i].bt + wt[i];
}
void AvgTime(process proc[], int n) {
int wt[n], tat[n], total_wt = 0, total_tat = 0;
string gantt_p[2 * n]; // Gantt chart process names
int gantt_t[2 * n]; // Gantt chart start times
int gantt_size = 0;
WaitingTime(proc, n, wt, gantt_p, gantt_t, gantt_size);
TurnAroundTime(proc, n, wt, tat);
cout << " P\t\t" << "BT\t\t" << "WT\t\t" << "TAT\t\t\n";
for (int i = 0; i < n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << proc[i].p << "\t\t" << proc[i].bt << "\t\t " << wt[i] << "\t\t " << tat[i] << endl;
}
cout << "\nAverage waiting time: " << (float)total_wt / (float)n;
cout << "\nAverage turn around time: " << (float)total_tat / (float)n;
// Display Gantt chart
cout << "\n\nGantt Chart:\n";
for (int i = 0; i < gantt_size; i++) {
cout << "| " << gantt_p[i] << " ";
}
cout << "|\n";
for (int i = 0; i < gantt_size; i++) {
cout << "|----";
}
cout << "|\n";
for (int i = 0; i <= gantt_size; i++) {
cout << gantt_t[i] << " ";
}
cout << "\n";
}
};
int main() {
ShortestJob obj;
int n;
cout << "\nEnter the number of processes: ";
cin >> n;
process* proc = new process[n];
for(int i = 0; i < n; i++) {
cout << "\nEnter name of process: ";
cin >> proc[i].p;
cout << "Enter arrival time of process: ";
cin >> proc[i].at;
cout << "Enter burst time of process: ";
cin >> proc[i].bt;
}
obj.AvgTime(proc, n);
delete[] proc;
return 0;
}