-
Notifications
You must be signed in to change notification settings - Fork 0
/
rr.html
236 lines (197 loc) · 6.45 KB
/
rr.html
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<!DOCTYPE html>
<html>
<head>
<title>Round Robin Scheduler</title>
<style>
body {
background-color: black;
color: #ffd700;
font-family: Arial, sans-serif;
}
.container {
/* max-width: 600px; */
/* margin: 0 auto; */
margin-left: 220px;
padding: 20px;
}
h1 {
/* text-align: center; */
margin-left: 300px;
}
.processes {
margin-top: 40px;
}
.process {
display: flex;
align-items: center;
margin-bottom: 10px;
}
.process .pid {
background-color: #ffd700;
color: black;
border-radius: 3px;
padding: 5px 10px;
margin-right: 10px;
}
.process .burst-time, .process .arrival-time {
background-color: black;
color: #ffd700;
padding: 5px 10px;
margin-right: 10px;
}
.output {
margin-top: 40px;
text-align: center;
}
table {
margin-top: 20px;
margin-left: 100px;
width: 60%;
border-collapse: collapse;
}
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ffd700;
}
th {
background-color: #ffd700;
color: black;
}
.gantt-chart {
margin-top: 40px;
display: flex;
align-items: center;
justify-content: center;
margin-right: 200px;
}
.process-bar {
height: 40px;
background-color: #ffd700;
display: flex;
align-items: center;
justify-content: center;
margin-right: 10px;
}
.btn{
width:150px;
height:40px;
background-color: #ffd700;
color:black;
border-radius: 10px;
margin-left:10px;
font-size: 16px;
}
.btn-1{
width:150px;
height:40px;
background-color: #ffd700;
color:black;
border-radius: 10px;
margin-left:-230px;
font-size: 16px;
}
input{
width:150px;
height:30px;
}
#result{
margin-right: 220px;
}
</style>
</head>
<body>
<div class="container">
<h1>ROUND ROBIN</h1>
<label for="num-processes">Number of Processes:</label>
<input type="number" id="num-processes">
<label for="time-quantum">Time Quantum:</label>
<input type="number" id="time-quantum">
<button onclick="createTable()" class="btn">Create Table</button>
<div id="process-table"></div>
<div class="output">
<button onclick="runRoundRobin()" class="btn-1">Run Round Robin</button>
<p id="result"></p>
</div>
<div class="gantt-chart" id="gantt-chart"></div>
</div>
<script>
function createTable() {
const numProcessesInput = document.getElementById('num-processes');
const numProcesses = parseInt(numProcessesInput.value);
if (isNaN(numProcesses) || numProcesses <= 0) {
alert('Please enter a valid number of processes.');
return;
}
let tableHtml = '<table>';
tableHtml += '<tr><th>PID</th><th>Arrival Time</th><th>Burst Time</th></tr>';
for (let i = 0; i < numProcesses; i++) {
tableHtml += `<tr><td>P${i + 1}</td><td><input type="number" class="arrival-time-input"></td><td><input type="number" class="burst-time-input"></td></tr>`;
}
tableHtml += '</table>';
const processTableDiv = document.getElementById('process-table');
processTableDiv.innerHTML = tableHtml;
}
function runRoundRobin() {
const arrivalTimeInputs = document.getElementsByClassName('arrival-time-input');
const burstTimeInputs = document.getElementsByClassName('burst-time-input');
const arrivalTimes = [];
const burstTimes = [];
for (let i = 0; i < arrivalTimeInputs.length; i++) {
const arrivalTime = parseInt(arrivalTimeInputs[i].value);
const burstTime = parseInt(burstTimeInputs[i].value);
if (isNaN(arrivalTime) || isNaN(burstTime) || arrivalTime < 0 || burstTime <= 0) {
alert('Please enter valid arrival time and burst time for all processes.');
return;
}
arrivalTimes.push(arrivalTime);
burstTimes.push(burstTime);
}
const timeQuantumInput = document.getElementById('time-quantum');
const timeQuantum = parseInt(timeQuantumInput.value);
if (isNaN(timeQuantum) || timeQuantum <= 0) {
alert('Please enter a valid time quantum.');
return;
}
const processes = [];
for (let i = 0; i < arrivalTimes.length; i++) {
processes.push({ pid: `P${i + 1}`, arrivalTime: arrivalTimes[i], burstTime: burstTimes[i] });
}
let currentTime = 0;
let completedProcesses = 0;
let totalWaitingTime = 0;
let totalTurnaroundTime = 0;
const ganttChartDiv = document.getElementById('gantt-chart');
let ganttChartHtml = 'Gantt <br> Chart: ';
while (completedProcesses < processes.length) {
let allProcessesComplete = true;
for (let i = 0; i < processes.length; i++) {
const process = processes[i];
if (process.burstTime > 0) {
allProcessesComplete = false;
const executionTime = Math.min(process.burstTime, timeQuantum);
process.burstTime -= executionTime;
currentTime += executionTime;
ganttChartHtml += `<div class="process-bar" style="width: ${executionTime * 100 / Math.max(...burstTimes)}px;color:black;">${process.pid}</div>`;
if (process.burstTime === 0) {
completedProcesses++;
const turnaroundTime = currentTime - process.arrivalTime;
const waitingTime = turnaroundTime - burstTimes[i];
totalWaitingTime += waitingTime;
totalTurnaroundTime += turnaroundTime;
}
}
}
if (allProcessesComplete) {
break;
}
}
const averageWaitingTime = totalWaitingTime / processes.length;
const averageTurnaroundTime = totalTurnaroundTime / processes.length;
const resultElement = document.getElementById('result');
resultElement.innerHTML = `Average Waiting Time: ${averageWaitingTime.toFixed(2)}<br>Average Turnaround Time: ${averageTurnaroundTime.toFixed(2)}`;
ganttChartDiv.innerHTML = ganttChartHtml;
}
</script>
</body>
</html>