Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 54 additions & 26 deletions src/components/RR.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,66 @@ const RoundRobinScheduler = ({ rows, quantum }) => {
if (rows && rows.length > 0 && quantum > 0) {
// Sort rows by arrival time
const sortedRows = rows.slice().sort((a, b) => parseInt(a.arrivalTime) - parseInt(b.arrivalTime));
const n = sortedRows.length;

let currentTime = 0;
let remainingBurstTimes = sortedRows.map((row) => parseInt(row.burstTime));
let waitingTimes = new Array(sortedRows.length).fill(0);
let turnaroundTimes = new Array(sortedRows.length).fill(0);
let waitingTimes = new Array(n).fill(0);
let turnaroundTimes = new Array(n).fill(0);

const updatedProcesses = [];

while (remainingBurstTimes.some((bt) => bt > 0)) {
for (let i = 0; i < sortedRows.length; i++) {
const burstTime = remainingBurstTimes[i];

if (burstTime > 0) {
const executeTime = Math.min(quantum, burstTime);
const start = currentTime; // Keep track of the actual start time
currentTime += executeTime;
remainingBurstTimes[i] -= executeTime;

const turnaroundTime = currentTime - parseInt(sortedRows[i].arrivalTime);
const waitingTime = turnaroundTime - parseInt(sortedRows[i].burstTime);

waitingTimes[i] = waitingTime;
turnaroundTimes[i] = turnaroundTime;

updatedProcesses.push({
...sortedRows[i],
startTime: start, // Set the actual start time
finishTime: currentTime,
waitingTime,
turnaroundTime,
});
}
// Proper ready-queue based Round Robin: a process index only enters
// the queue once its arrival time has actually passed.
const queue = [];
let nextArrivalIndex = 0;

const enqueueArrivals = (time) => {
while (nextArrivalIndex < n && parseInt(sortedRows[nextArrivalIndex].arrivalTime) <= time) {
queue.push(nextArrivalIndex);
nextArrivalIndex++;
}
};

// Nothing can run before the first process arrives
currentTime = parseInt(sortedRows[0].arrivalTime);
enqueueArrivals(currentTime);

while (queue.length > 0) {
const i = queue.shift();

const executeTime = Math.min(quantum, remainingBurstTimes[i]);
const start = currentTime; // Keep track of the actual start time
currentTime += executeTime;
remainingBurstTimes[i] -= executeTime;

const turnaroundTime = currentTime - parseInt(sortedRows[i].arrivalTime);
const waitingTime = turnaroundTime - parseInt(sortedRows[i].burstTime);

waitingTimes[i] = waitingTime;
turnaroundTimes[i] = turnaroundTime;

updatedProcesses.push({
...sortedRows[i],
startTime: start, // Set the actual start time
finishTime: currentTime,
waitingTime,
turnaroundTime,
});

// Any process that arrived during this quantum joins the queue
// before the process that just ran gets re-queued (standard RR tie-break).
enqueueArrivals(currentTime);

if (remainingBurstTimes[i] > 0) {
queue.push(i);
}

// If nobody is ready but processes are still left to arrive, jump
// the clock forward instead of leaving a gap or scheduling early.
if (queue.length === 0 && nextArrivalIndex < n) {
currentTime = Math.max(currentTime, parseInt(sortedRows[nextArrivalIndex].arrivalTime));
enqueueArrivals(currentTime);
}
}

Expand Down