FCFS
Refined and Debugged Codeunknown
java
4 years ago
3.5 kB
7
Indexable
package com.company;
import java.util.Comparator;
import java.util.PriorityQueue;
class FCFS{
static PriorityQueue<Process> processQueue = new PriorityQueue<Process>(10, new Comparator<Process>() {
public int compare(Process process1, Process process2) {
return (int)(process1.getArrivalTime()-process2.getArrivalTime());
}
});
static PriorityQueue<Process> readyQueue = new PriorityQueue<Process>(10, new Comparator<Process>() {
public int compare(Process process1, Process process2) {
return (int)(process1.getArrivalTime()-process2.getArrivalTime());
}
});
static GlobalTimer globalTimer = new GlobalTimer(0);
public static void main(String[] args) {
int arr[] = new int[10];
int arr2[] = new int[10];
int i=0;
processQueue.add(new Process(1,3,2,globalTimer));
processQueue.add(new Process(2,6,3,globalTimer));
processQueue.add(new Process(3,1,4,globalTimer));
processQueue.add(new Process(4,4,5,globalTimer));
while(globalTimer.time < 100){
// TASK: Write your code here
// You may look for available priority queue methods here: https://www.javatpoint.com/java-priorityqueue
// Check for processes that have arrived using checkIfNewProcessArrived()
// If found, move them to ready queue
// If any process is ready to run, run a process from ready queue following the queue policy
// Otherwise print the global time and mention that no process is ready to run
int bt;
if (checkIfNewProcessArrived()) {
readyQueue.add(processQueue.poll());
for (Process p : readyQueue) {
while (p.duration > 0) {
arr2[i]=p.duration;
runProcessInCpu();
p.duration--;
arr[i]=globalTimer.time;
i++;
}
}
} else {
System.out.println(globalTimer.time + " & No process is running");
}
globalTimer.time++;
}
// TASK: Write your code here
// Print performance statistics
int ct,tt,wt;
double avg_wt,avg_tat,sum=0.0, sum2=0.0;
i=0;
for (Process p:readyQueue){
ct = arr[i];
tt= ct-p.arrivalTime;
wt= tt-arr2[i];
sum+=wt;
sum2+=tt;
System.out.println("Process ID: "+p.id+" CT:"+ct+" TAT: "+tt+" WT: "+wt);
tt=0; ct=0; wt=0;
i++;
}
avg_wt= sum/i;
avg_tat= sum2/i;
System.out.println("Average waiting Time: "+avg_wt+" Average TAT time: "+avg_tat);
}
public static boolean checkIfNewProcessArrived(){
// TASK: Write your code here
// return True/False by comparing the earliest arrival time from process queue with the global time
for (Process p:processQueue){
if (p.arrivalTime<p.globalTimer.time){
return true;
}
}
return false;
}
public static void runProcessInCpu(){
// TASK: Write your code here
// Retrieve a process that is ready to run and run it
int i=0;
for (Process p : readyQueue) {
while (p.duration > 0) {
p.runProcess();
p.duration--;
}
}
}
}Editor is loading...