JavaScript Code
class Process {
constructor(id, arrivalTime, burstTime, predictedTime) {
this.id = id;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
this.predictedTime = predictedTime;
this.remainingTime = burstTime;
}
}
function SRTF(processes) {
let currentTime = 0;
let completedProcesses = [];
let readyQueue = [];
let runningProcess = null;
while (completedProcesses.length < processes.length) {
// Check if there are any processes that have arrived
for (let i = 0; i < processes.length; i++) {
if (processes[i].arrivalTime <= currentTime && !completedProcesses.includes(processes[i])) {
readyQueue.push(processes[i]);
}
}
// Sort readyQueue by remaining time
readyQueue.sort((a, b) => {
if (a.remainingTime === b.remainingTime) {
return a.predictedTime - b.predictedTime;
}
return a.remainingTime - b.remainingTime;
});
// If runningProcess has completed, add it to completedProcesses and set runningProcess to null
if (runningProcess && runningProcess.remainingTime === 0) {
completedProcesses.push(runningProcess);
runningProcess = null;
}
// If there are processes in the readyQueue, start the one with the shortest remaining time
if (readyQueue.length > 0) {
const shortestProcess = readyQueue.shift();
if (runningProcess !== shortestProcess) {
runningProcess = shortestProcess;
}
}
// Decrement remaining time of runningProcess and increment currentTime
if (runningProcess) {
runningProcess.remainingTime--;
currentTime++;
} else {
currentTime++;
}
}
return completedProcesses;
}
// Example usage
const processes = [
new Process(1, 0, 6, 4),
new Process(2, 2, 4, 2),
new Process(3, 4, 2, 1),
new Process(4, 5, 3, 3)
];
const completedProcesses = SRTF(processes);
console.log(completedProcesses);
Shortest Remaining Time First (SRTF) With predicted Time
CPU scheduling algorithms are essential components of operating systems that determine the order in which processes are executed on a computer’s central processing unit (CPU). These algorithms aim to optimize CPU utilization, reduce waiting times, and enhance system performance by efficiently managing the execution of tasks in a multi-tasking environment. Various algorithms, such as First-Come, First-Served (FCFS), Shortest Job Next (SJN), Round Robin (RR), and Priority Scheduling, are employed to achieve these objectives, each with its own set of advantages and limitations. In this article, we study about Shortest Remaining Time First CPU Scheduling algorithm.