0% found this document useful (0 votes)
88 views3 pages

Shortest Remaining Time Java

The document implements Shortest Remaining Time First (SRTF) CPU scheduling algorithm in Java. It defines a Process class with attributes like ID, arrival time and burst time. It takes a list of processes, implements priority queue based on remaining time and simulates SRTF scheduling to calculate total waiting time and average waiting time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views3 pages

Shortest Remaining Time Java

The document implements Shortest Remaining Time First (SRTF) CPU scheduling algorithm in Java. It defines a Process class with attributes like ID, arrival time and burst time. It takes a list of processes, implements priority queue based on remaining time and simulates SRTF scheduling to calculate total waiting time and average waiting time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

*;

class Process {

int id;

int arrivalTime;

int burstTime;

int remainingTime;

public Process(int id, int arrivalTime, int burstTime) {

this.id = id;

this.arrivalTime = arrivalTime;

this.burstTime = burstTime;

this.remainingTime = burstTime;

public class SRTFScheduling {

public static void main(String[] args) {

List<Process> processes = new ArrayList<>();

processes.add(new Process(1, 0, 5));

processes.add(new Process(2, 2, 3));

processes.add(new Process(3, 4, 2));

processes.add(new Process(4, 6, 4));

srtfScheduling(processes);

public static void srtfScheduling(List<Process> processes) {

int currentTime = 0;
int totalWaitingTime = 0;

int completedProcesses = 0;

PriorityQueue<Process> queue = new PriorityQueue<>(Comparator.comparingInt(p ->


p.remainingTime));

while (completedProcesses < processes.size()) {

for (Process process : processes) {

if (process.arrivalTime <= currentTime && !queue.contains(process)) {

queue.add(process);

if (!queue.isEmpty()) {

Process currentProcess = queue.poll();

System.out.println("Executing process " + currentProcess.id + " at time " + currentTime);

currentProcess.remainingTime--;

if (currentProcess.remainingTime == 0) {

completedProcesses++;

totalWaitingTime += currentTime - currentProcess.arrivalTime - currentProcess.burstTime + 1;

System.out.println("Process " + currentProcess.id + " completed at time " + (currentTime +


1));

} else {

System.out.println("Idle at time " + currentTime);

currentTime++;

}
System.out.println("Average waiting time: " + (double) totalWaitingTime / processes.size());

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy