-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRR.cpp
More file actions
151 lines (122 loc) · 4.27 KB
/
Copy pathRR.cpp
File metadata and controls
151 lines (122 loc) · 4.27 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <queue>
#include <random>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
struct Process {
int arrival_time;
int burst_time;
string name;
int remaining_time;
int completion_time;
};
struct GanttEntry {
int time;
string name;
};
void drawGanttChart(const vector<GanttEntry>& 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, time_quantum;
cout << "Enter the number of processes: ";
cin >> N;
cout << "Enter time quantum: ";
cin >> time_quantum;
vector<Process> processes;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distrib(1, 10);
// Initializing processes
for (int i = 0; i < N; i++) {
Process p;
p.arrival_time = distrib(gen);
p.burst_time = distrib(gen);
p.remaining_time = p.burst_time;
p.completion_time = 0; // Will be updated upon completion
p.name = "process" + std::to_string(i + 1);
processes.push_back(p);
}
queue<int> process_queue;
vector<GanttEntry> Gantt;
GanttEntry g;
g.time = 0;
int current_time = 0;
int completed = 0;
vector<bool> is_in_queue(N, false);
// Round Robin Scheduling
while (completed < N) {
bool added_to_queue = false;
// Add processes that have arrived to the queue
for (int i = 0; i < N; i++) {
if (processes[i].arrival_time <= current_time && processes[i].remaining_time > 0 && !is_in_queue[i]) {
process_queue.push(i);
is_in_queue[i] = true;
added_to_queue = true;
}
}
if (process_queue.empty()) {
bool all_arrived = true;
for (int i = 0; i < N; i++) {
if (processes[i].arrival_time > current_time) {
all_arrived = false;
break;
}
}
if (all_arrived) {
break;
}
current_time++;
continue;
}
int index = process_queue.front();
process_queue.pop();
is_in_queue[index] = false;
g.name = processes[index].name;
Gantt.push_back(g);
int time_spent = min(time_quantum, processes[index].remaining_time);
g.time += time_spent;
current_time += time_spent;
processes[index].remaining_time -= time_spent;
if (processes[index].remaining_time == 0) {
processes[index].completion_time = current_time;
completed++;
}
else {
process_queue.push(index);
is_in_queue[index] = true;
}
Gantt.push_back(g);
}
int total_turnaround_time = 0;
int total_waiting_time = 0;
cout << "\nProcess\t\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time\n";
cout << "------------------------------------------------------------------------------------------\n";
for (const auto& p : processes) {
int turnaround_time = p.completion_time - p.arrival_time;
int waiting_time = turnaround_time - p.burst_time;
total_turnaround_time += turnaround_time;
total_waiting_time += waiting_time;
// Print each row with fixed column width
cout << left << setw(12) << p.name << setw(14) << p.arrival_time << setw(12) << p.burst_time
<< setw(16) << p.completion_time << setw(16) << turnaround_time << setw(12) << waiting_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;
}