-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
406 lines (332 loc) · 13.5 KB
/
script.js
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
function readCsvFile(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (event) => {
const text = event.target.result;
const data = parseCsv(text);
resolve(data);
};
reader.onerror = (error) => reject(error);
reader.readAsText(file);
});
}
function parseCsv(text) {
const rows = text.split("\n").slice(1);
const tasks = rows.map((row) => {
const [task, enter_time, time_needed] = row.split(",");
const color = getRandomColor();
return [task.trim(), +enter_time.trim(), +time_needed.trim(), color];
});
return tasks;
}
function getRandomColor() {
const randomColor = Math.floor(Math.random() * 16777215).toString(16);
return `#${randomColor}`;
}
async function processFile() {
const input = document.getElementById("fileInput");
if (input.files.length === 0) {
alert("Please upload a CSV file");
return;
}
const file = input.files[0];
const tasks = await readCsvFile(file);
drawFCFS(tasks);
drawHRRN(tasks);
drawSPN(tasks);
drawRR(tasks);
drawSRTF(tasks);
}
function drawFCFS(tasks) {
tasks.sort((a, b) => a[1] - b[1]); // Sort tasks based on enter time
const barsContainer = document.getElementById("barsContainerFCFS");
barsContainer.innerHTML = "";
let time = 0;
let totalWaitTime = 0;
tasks.forEach((task) => {
const [taskName, enterTime, timeNeeded, color] = task;
// Calculate wait time
const waitTime = Math.max(0, time - enterTime);
totalWaitTime += waitTime;
// Set start and end times
const startTime = Math.max(time, enterTime);
const endTime = startTime + timeNeeded;
// Create bar for the task
const barDiv = document.createElement("div");
barDiv.className = "bar";
barDiv.style.backgroundColor = color;
barDiv.style.width = timeNeeded * 50 + "px";
barDiv.innerHTML = `
<div class="start-time">${startTime}</div>
<div class="task-name">${taskName}</div>
<div class="end-time">${endTime}</div>
`;
barsContainer.appendChild(barDiv);
time = endTime; // Update current time
});
const averageWaitTime = (totalWaitTime / tasks.length).toFixed(2);
document.getElementById(
"averageWaitTimeFCFS"
).innerText = `Average Wait Time: ${averageWaitTime} time units`;
}
function drawHRRN(tasks) {
// Sort the tasks based on their enter time
tasks.sort((a, b) => a[1] - b[1]);
const barsContainer = document.getElementById("barsContainerHRRN");
barsContainer.innerHTML = ""; // Clear previous content
let time = 0;
let totalWaitTime = 0;
let completedTasks = 0;
while (tasks.length > 0) {
// Filter tasks that have entered the system (i.e., arrived)
const availableTasks = tasks.filter((task) => task[1] <= time);
if (availableTasks.length === 0) {
// If no tasks are available, jump to the time of the next task
time = tasks[0][1]; // Move time to the next task's enter time
continue;
}
// Calculate the response ratio for each available task
availableTasks.forEach((task) => {
const [taskName, enterTime, timeNeeded] = task;
const waitTime = time - enterTime;
task.responseRatio = (waitTime + timeNeeded) / timeNeeded; // Calculate HRRN
});
// Sort tasks by their response ratio (highest first)
availableTasks.sort((a, b) => b.responseRatio - a.responseRatio);
const currentTask = availableTasks[0]; // The task to run next
const [taskName, enterTime, timeNeeded, color] = currentTask;
// Calculate wait time for this task
const waitTime = time - enterTime;
totalWaitTime += waitTime;
// Set start and end times
const startTime = time; // Starts as current time
const endTime = startTime + timeNeeded;
// Create bar for the task
const barDiv = document.createElement("div");
barDiv.className = "bar";
barDiv.style.backgroundColor = color;
barDiv.style.width = timeNeeded * 50 + "px"; // Scale width
barDiv.innerHTML = `
<div class="start-time">${startTime}</div>
<div class="task-name">${taskName}</div>
<div class="end-time">${endTime}</div>
`;
barsContainer.appendChild(barDiv);
// Update time and remove task from the list
time = endTime;
tasks = tasks.filter((task) => task !== currentTask);
completedTasks++; // Increment the count of completed tasks
}
// Calculate average wait time only if there are completed tasks
const averageWaitTime =
completedTasks > 0 ? (totalWaitTime / completedTasks).toFixed(2) : 0;
document.getElementById(
"averageWaitTimeHRRN"
).innerText = `Average Wait Time: ${averageWaitTime} time units`;
}
function drawSPN(tasks) {
// Sort the tasks based on their enter time and then by time needed
tasks.sort((a, b) => {
if (a[1] === b[1]) {
return a[2] - b[2]; // Sort by time needed if enter times are the same
}
return a[1] - b[1]; // Sort by enter time
});
const barsContainer = document.getElementById("barsContainerSPN");
barsContainer.innerHTML = "";
let time = 0;
let totalWaitTime = 0;
let completedTasks = 0; // Initialize completed tasks counter
while (tasks.length > 0) {
// Filter tasks that have entered the system (i.e., arrived)
const availableTasks = tasks.filter((task) => task[1] <= time);
if (availableTasks.length === 0) {
// If no tasks are available, jump to the time of the next task
time = tasks[0][1]; // Move time to the next task's enter time
continue;
}
// Sort available tasks by time needed (SPN logic)
availableTasks.sort((a, b) => a[2] - b[2]);
const currentTask = availableTasks[0]; // The task to run next
const [taskName, enterTime, timeNeeded, color] = currentTask;
// Calculate wait time for this task
const waitTime = time - enterTime;
totalWaitTime += waitTime;
// Set start and end times
const startTime = time; // Starts as current time
const endTime = startTime + timeNeeded;
// Create bar for the task
const barDiv = document.createElement("div");
barDiv.className = "bar";
barDiv.style.backgroundColor = color;
barDiv.style.width = timeNeeded * 50 + "px"; // Scale width
barDiv.innerHTML = `
<div class="start-time">${startTime}</div>
<div class="task-name">${taskName}</div>
<div class="end-time">${endTime}</div>
`;
barsContainer.appendChild(barDiv);
// Update time and remove task from the list
time = endTime;
tasks = tasks.filter((task) => task !== currentTask);
completedTasks++; // Increment the count of completed tasks
}
// Calculate average wait time only if there are completed tasks
const averageWaitTime =
completedTasks > 0 ? (totalWaitTime / completedTasks).toFixed(2) : 0;
document.getElementById(
"averageWaitTimeSPN"
).innerText = `Average Wait Time: ${averageWaitTime} time units`;
}
function drawSRTF(tasks) {
// Sort the tasks based on enter time
tasks.sort((a, b) => a[1] - b[1]);
tasks.forEach((task) => task.push(task[2]));
const barsContainer = document.getElementById("barsContainerSRTF");
barsContainer.innerHTML = ""; // Clear previous content
let time = 0;
let totalWaitTime = 0;
let completedTasks = 0;
let readyQueue = [];
let taskInProgress = null;
let remainingTime = 0;
let taskStartTime = 0; // Tracks the start time of tasks
let task_name_next = null;
// Continue until all tasks are completed
while (tasks.length > 0 || readyQueue.length > 0 || taskInProgress !== null) {
// Add tasks that have arrived to the ready queue
while (tasks.length > 0 && tasks[0][1] <= time) {
readyQueue.push(tasks.shift());
}
// If there's a task in progress, check if it should be preempted
if (taskInProgress !== null) {
// Sort readyQueue by remaining time of tasks (Shortest remaining time first)
readyQueue.sort((a, b) => a[2] - b[2]);
// Check if any task in the readyQueue has shorter remaining time than the current task
for (let i = 0; i < readyQueue.length; i++) {
if (readyQueue[i][2] < remainingTime) {
// Preempt the current task and add it back to the queue
readyQueue.push(taskInProgress);
taskInProgress = readyQueue[i];
remainingTime = taskInProgress[2];
readyQueue.splice(i, 1);
break;
}
}
}
// If no task is in progress, pick the task with the shortest remaining time
if (taskInProgress === null && readyQueue.length > 0) {
readyQueue.sort((a, b) => a[2] - b[2]); // Sort by remaining time
taskInProgress = readyQueue.shift(); // Select the task with the shortest remaining time
remainingTime = taskInProgress[2];
taskStartTime = time; // Track the start time of the current task
}
if (taskInProgress !== null) {
const [taskName, enterTime, timeNeeded, color, temp_remaining] = taskInProgress;
if (task_name_next !== null) {
if(task_name_next == taskName){}else{
task_name_next = taskName
const waitTime = time - enterTime - (temp_remaining - timeNeeded);
totalWaitTime += waitTime;
//console.log(totalWaitTime,waitTime,time,enterTime, timeNeeded, taskName);
}
}else{
task_name_next = taskName
const waitTime = time - enterTime - (temp_remaining - timeNeeded);
totalWaitTime += waitTime;
//console.log(totalWaitTime,waitTime,time,enterTime, timeNeeded, taskName);
}
// Create a bar for each unit of task execution
const barDiv = document.createElement("div");
barDiv.className = "bar";
barDiv.style.backgroundColor = color;
barDiv.style.width = "50px"; // Width for each time unit
barDiv.innerHTML = `
<div class="start-time">${time}</div>
<div class="task-name">${taskName}</div>
<div class="end-time">${time + 1}</div>
`;
barsContainer.appendChild(barDiv);
remainingTime -= 1;
taskInProgress[2] -= 1; // Decrease remaining time for the current task
if (remainingTime === 0) {
taskInProgress = null; // Task is completed
completedTasks++; // Increment completed task count
}
}
time++; // Increment time
}
// Calculate the average wait time
const averageWaitTime = (totalWaitTime / completedTasks).toFixed(2);
document.getElementById(
"averageWaitTimeSRTF"
).innerText = `Average Wait Time: ${averageWaitTime} time units`;
}
function drawRR(tasks) {
const quantum = 1; // Quantum time for RR scheduling
tasks.sort((a, b) => a[1] - b[1]); // Sort tasks based on enter time
const barsContainer = document.getElementById("barsContainerRR");
barsContainer.innerHTML = ""; // Clear previous content
let time = 0;
let totalWaitTime = 0;
let completedTasks = 0;
let queue = []; // Ready queue
let remainingTasks = [...tasks]; // Copy the task list to track remaining tasks
let waitTimes = new Map(); // Track the wait times for each task
while (remainingTasks.length > 0 || queue.length > 0) {
// Add tasks that have entered the system (i.e., arrived) to the queue
while (remainingTasks.length > 0 && remainingTasks[0][1] <= time) {
queue.unshift(remainingTasks.shift());
}
if (queue.length === 0) {
// If no tasks are in the queue, jump to the next task's enter time
time = remainingTasks[0][1];
continue;
}
// Dequeue the next task to execute
const [taskName, enterTime, timeNeeded, color, realTimeNeeded] = queue.shift();
const initialTimeNeeded = realTimeNeeded; // Save the original time needed for later calculation
// Calculate wait time for this task (it has been waiting since it entered the queue)
//const waitTime = time - enterTime;
console.log(taskName, timeNeeded, realTimeNeeded)
//totalWaitTime += waitTime;
//waitTimes.set(taskName, (waitTimes.get(taskName) || 0) + waitTime);
// Set the start and end times for this task's quantum
const startTime = time;
const actualQuantum = Math.min(timeNeeded, quantum);
const endTime = startTime + actualQuantum;
// Create a bar for this task's execution in the Gantt chart
const barDiv = document.createElement("div");
barDiv.className = "bar";
barDiv.style.backgroundColor = color;
barDiv.style.width = actualQuantum * 50 + "px"; // Scale width based on time units
barDiv.innerHTML = `
<div class="start-time">${startTime}</div>
<div class="task-name">${taskName}</div>
<div class="end-time">${endTime}</div>
`;
barsContainer.appendChild(barDiv);
// Update time and check if this task is complete
time = endTime;
if (timeNeeded == actualQuantum){
if (realTimeNeeded == null){
totalWaitTime += (endTime-timeNeeded-enterTime);
}else{
totalWaitTime += (endTime-realTimeNeeded-enterTime);}
}
// If the task isn't finished, push it back into the queue with its remaining time
if (timeNeeded > actualQuantum) {
if(realTimeNeeded == null){
queue.push([taskName, enterTime, timeNeeded - actualQuantum, color, timeNeeded]);
}else{
queue.push([taskName, enterTime, timeNeeded - actualQuantum, color, realTimeNeeded]);} // Task goes back with reduced time
} else {
completedTasks++;
}
}
// Calculate the average wait time
const averageWaitTime = (totalWaitTime / tasks.length).toFixed(2);
document.getElementById(
"averageWaitTimeRR"
).innerText = `Average Wait Time: ${averageWaitTime} time units`;
}