-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFCFS.cpp
More file actions
133 lines (106 loc) · 3.49 KB
/
Copy pathFCFS.cpp
File metadata and controls
133 lines (106 loc) · 3.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
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
133
#include <iostream>
#include <iomanip>
#include <random>
#include <vector>
#include <string>
using namespace std;
struct processes {
int arrival_time;
int Burst_time;
string names;
};
struct gant {
int time = 0;
string name;
};
struct endtime {
string name;
int stime;
int etime;
};
struct TurnAroundTime {
string name;
int time;
};
struct WaitingTime {
string name;
int time;
};
void drawGanttChart(const vector<gant>& Gantt) {
cout << "\nGantt Chart:\n";
cout << "-------------------------------------------------\n";
for (int i = 1; i < Gantt.size(); i++) {
cout << "| " << setw(8) << Gantt[i].name << " ";
}
cout << "|\n";
cout << "-------------------------------------------------\n";
for (int i = 1; i < Gantt.size(); i++) {
cout << setw(9) << Gantt[i - 1].time << " ";
}
cout << setw(9) << Gantt.back().time << "\n";
}
int main() {
int N;
cout << "Enter the number of processes: ";
cin >> N;
vector<processes> Process;
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> distrib(1, 10);
processes p1;
for (int i = 0; i < N; i++) {
p1.arrival_time = distrib(gen);
p1.Burst_time = distrib(gen);
p1.names = "process" + to_string(i + 1);
Process.push_back(p1);
}
vector<processes> CopyP = Process;
vector<gant> Gantt;
gant g1;
g1.time = 0;
int current_time = 0;
int total_turnaround_time = 0;
int total_waiting_time = 0;
vector<TurnAroundTime> Tvect;
vector<WaitingTime> Wvect;
while (!Process.empty()) {
int earliest_arrival = Process[0].arrival_time;
int index = 0;
for (int i = 1; i < Process.size(); i++) {
if (Process[i].arrival_time < earliest_arrival) {
earliest_arrival = Process[i].arrival_time;
index = i;
}
}
processes current_process = Process[index];
Process.erase(Process.begin() + index);
if (current_time < current_process.arrival_time) {
current_time = current_process.arrival_time;
}
g1.name = current_process.names;
g1.time = current_time;
Gantt.push_back(g1);
current_time += current_process.Burst_time;
g1.time = current_time;
Gantt.push_back(g1);
int turnaround_time = current_time - current_process.arrival_time;
int waiting_time = turnaround_time - current_process.Burst_time;
Tvect.push_back({ current_process.names, turnaround_time });
Wvect.push_back({ current_process.names, waiting_time });
total_turnaround_time += turnaround_time;
total_waiting_time += waiting_time;
}
cout << "\nProcess\t\tArrival Time\tBurst Time\tTurnaround Time\tWaiting Time\n";
cout << "-----------------------------------------------------------------------\n";
for (int i = 0; i < N; i++) {
cout << left << setw(12) << CopyP[i].names << setw(14) << CopyP[i].arrival_time
<< setw(12) << CopyP[i].Burst_time << setw(16) << Tvect[i].time
<< Wvect[i].time << "\n";
}
double avg_turnaround_time = (double)total_turnaround_time / N;
double avg_waiting_time = (double)total_waiting_time / N;
cout << "\nAverage Turnaround Time: " << fixed << setprecision(2) << avg_turnaround_time << endl;
cout << "Average Waiting Time: " << fixed << setprecision(2) << avg_waiting_time << endl;
drawGanttChart(Gantt);
return 0;
}