Java Code
import java.util.*;
public class SRTF {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of processes: ");
int n = sc.nextInt();
int[] processId = new int[n];
int[] arrivalTime = new int[n];
int[] burstTime = new int[n];
int[] remainingTime = new int[n];
int[] predictedTime = new int[n];
int[] completionTime = new int[n];
int[] turnaroundTime = new int[n];
int[] waitingTime = new int[n];
boolean[] isCompleted = new boolean[n];
int currentTime = 0;
int completed = 0;
for (int i = 0; i < n; i++) {
System.out.println("Enter details for process " + (i + 1));
System.out.print("Process ID: ");
processId[i] = sc.nextInt();
System.out.print("Arrival time: ");
arrivalTime[i] = sc.nextInt();
System.out.print("Burst time: ");
burstTime[i] = sc.nextInt();
remainingTime[i] = burstTime[i];
System.out.print("Predicted time: ");
predictedTime[i] = sc.nextInt();
}
while (completed!=n) {
int shortest=-1;
for (int i=0; i<n;i++) {
if (!isCompleted[i] && arrivalTime[i] <= currentTime) {
if (shortest == -1 || remainingTime[i] < remainingTime[shortest]) {
shortest=i;
}
}
}
if (shortest == -1) {
currentTime++;
continue;
}
remainingTime[shortest]--;
if (remainingTime[shortest] == 0) {
completionTime[shortest] = currentTime + 1;
turnaroundTime[shortest] = completionTime[shortest] - arrivalTime[shortest];
waitingTime[shortest] = turnaroundTime[shortest] - burstTime[shortest];
isCompleted[shortest] = true;
completed++;
}
currentTime++;
}
System.out.println("\nProcess\tArrival Time\tBurst Time\tPredicted Time\tCompletion Time\tTurnaround Time\tWaiting Time");
for (int i = 0; i < n; i++) {
System.out.println(processId[i] + "\t\t" + arrivalTime[i] + "\t\t" + burstTime[i] + "\t\t" + predictedTime[i] + "\t\t\t" + completionTime[i] + "\t\t\t" + turnaroundTime[i] + "\t\t\t" + waitingTime[i]);
}
double totalTurnaroundTime = 0;
double totalWaitingTime = 0;
for (int i = 0; i < n; i++) {
totalTurnaroundTime += turnaroundTime[i];
totalWaitingTime += waitingTime[i];
}
System.out.println("\nAverage Turnaround Time: " + (totalTurnaroundTime / n));
System.out.println("Average Waiting Time: " + (totalWaitingTime / n));
}
}
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.
Contact Us