Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

thats my java code which is a cpu priority with round robin , its a complete code but the gantt hart is missing something currently

thats my java code which is a cpu priority with round robin , its a complete code but the gantt hart is missing something
currently it is like this P1- P2- P3- P3- P4- P4-
whats missing is the times under each process for example : 0-P1-3-P3-5-P5-9-
PLEASE ADD THIS TO MY CODE AND CHECK IF IT WORKS
the java code : import java.util.*;
class ProcessManagement {
int processID;
int burstDuration;
int arrivalTime;
int priority;
int remainingTime;
int waitTime;
int completionTime;
int responseTime;
boolean isFinished;
boolean isFirstCycle;
public ProcessManagement(int pID, int aTime, int bDuration, int pri){
this.processID = pID;
this.arrivalTime = aTime;
this.burstDuration = bDuration;
this.priority = pri;
this.remainingTime = bDuration;
this.waitTime =0;
this.completionTime =0;
this.responseTime =-1;
this.isFinished = false;
this.isFirstCycle = true;
}
public int getPriority(){
return this.priority;
}
public int getID(){
return this.processID;
}
}
public class PriorityRoundRobin {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter time quantum:");
int quantum = scanner.nextInt();
List processList = new ArrayList<>();
System.out.println("Enter process ID, arrival time, burst time, and priority (terminate with 0000):");
while (true){
int pID = scanner.nextInt();
int aTime = scanner.nextInt();
int bDuration = scanner.nextInt();
int pri = scanner.nextInt();
if (pID ==0 && aTime ==0 && bDuration ==0 && pri ==0){
break;
}
processList.add(new ProcessManagement(pID, aTime, bDuration, pri));
}
scanner.close();
Collections.sort(processList, Comparator.comparingInt(p -> p.arrivalTime));
int currentTime =0;
int completedCount =0;
int totalWaitTime =0;
int totalTurnaroundTime =0;
int totalResponseTime =0;
StringBuilder gantt = new StringBuilder();
while (completedCount < processList.size()){
ProcessManagement current = getNextProcess(processList, currentTime);
if (current != null){
if (current.isFirstCycle){
current.responseTime = currentTime - current.arrivalTime;
current.isFirstCycle = false;
}
gantt.append(" P").append(current.processID).append("-");
int timeSlice = Math.min(quantum, current.remainingTime);
currentTime += timeSlice;
current.remainingTime -= timeSlice;
if (current.remainingTime ==0){
current.isFinished = true;
completedCount++;
current.completionTime = currentTime;
current.waitTime = current.completionTime - current.arrivalTime - current.burstDuration;
totalWaitTime += current.waitTime;
totalTurnaroundTime += currentTime - current.arrivalTime;
totalResponseTime += current.responseTime;
}
} else {
currentTime++;
gantt.append(".");
}
}
System.out.println("Scheduling done.");
System.out.println("Gantt Chart (Priority RR): "+ gantt.toString().trim());
for (ProcessManagement p : processList){
System.out.println("Process ID: "+ p.processID +", Turnaround Time: "+ p.completionTime +
", Wait Time: "+ p.waitTime +", Response Time: "+ p.responseTime);
}
System.out.println("Average Turnaround Time: "+(double) totalTurnaroundTime / processList.size());
System.out.println("Average Wait Time: "+(double) totalWaitTime / processList.size());
System.out.println("Average Response Time: "+(double) totalResponseTime / processList.size());
}
private static ProcessManagement getNextProcess(List processList, int currentTime){
return processList.stream()
.filter(p ->!p.isFinished && p.arrivalTime <= currentTime)
.min(Comparator.comparingInt(ProcessManagement::getPriority)
.thenComparingInt(ProcessManagement::getID))
.orElse(null);
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions