Write code in java to simulate the following CPU scheduling algorithm Priority S
ID: 3694684 • Letter: W
Question
Write code in java to simulate the following CPU scheduling algorithm
Priority Scheduling without Preemption (PR_noPREMP).
Take arrival times into account. Whenever a new process arrives, add it to a priority queue, in which the key is the priority value read from the input file. After Time 0, scheduling decisions are made only when the process that currently has the CPU terminates (no preemption). The scheduling decision will be giving the CPU to the process with the highest priority (smallest priority number). Ties are broken arbitrarily.
Your program should read an input file named “input.txt” and write the results into an output file named “output.txt”. The formats of these files are as follows:
Input File:
The first line has a space-separated list of scheduling algorithm names to run (any combination of the four names given above).
The second line has a single integer representing the number of processes in the file.
The rest of the file has one line per process with the following information:
Process number Arrival Time CPU burst time Priority
If multiple processes have the same arrival time, your scheduling algorithm should assume that there are negligibly small differences in arrival times with the processes arriving in the order they appear in the file. For priority scheduling, assume that smaller numbers indicate higher priorities. Non-priority algorithms will simply ignore the priority field.
Output File:
The output file will have the scheduling results for each of the algorithms listed in the input file. There should be one line for each CPU assignment (one line for each vertical line in the Gantt chart). Each line should have two numbers: one indicating the time point and one indicating the process number that got the CPU at that point. The last line for each scheduling algorithm should have the average waiting time.
Examples:
Input 3 (from the book)
PR_noPREMP
5
1 0 10 3
2 0 1 1
3 0 2 4
4 0 1 5
5 0 5 2
Output 3
PR_noPREMP
0 2
1 5
6 1
16 3
18 4
AVG Waiting Time: 8.2
Priority Queue Implementation
You can implement the priority queue using the easiest and most convenient way for you (the simplest implementation is probably an unsorted array).
Explanation / Answer
import java.io.File;
import java.io.PrintWriter;
import java.util.*;
public class CPUScheduler {
private static final int priorityOrder=3,pidOrder=0,burstTimeOrder=2,arrivalTimeOrder=1;
private Set<Scheduler> Scheduler_settings = new HashSet<>();
private Scanner scan;
private int processCount;
private processData processes[];
private int orderMode = 2;
private PrintWriter toFile;
//Create a Scanner object from input file, throws a exception when file not found
private void setup(String fileName) {
try {
scan = new Scanner(new File(fileName));
} catch (Exception e) {
toFile.println("file not found");
}
try{
toFile=new PrintWriter("output.txt","UTF-8");
}
catch(Exception e) {
toFile.println("file not found");
}
}
public void execute(){
organizeInput();
simulator();
toFile.close();
}
public static void main(String[] args) {
CPUScheduler cpu = new CPUScheduler();
cpu.execute();
}
private void calculateAverageRunningTime(){
int totalWaitingTime=0;
for(int i=0;i<processCount;i++)
{
totalWaitingTime+=processes[i].waitingTime;
processes[i].reset();
}
toFile.println("AVG Waiting Time: "+(float)totalWaitingTime/(float)processCount);
}
private void implementRR(int timeQuanta){
Queue<processData> readyQueue=new LinkedList<>();//This is ready Queue
//Ordering process list according to arrival time
orderMode=arrivalTimeOrder;
Arrays.sort(processes);
//variables to hold time, previous states and job status
int last=-1,time=0,allDone=0;
//Process in control of CPU will hold this variable processINcpu
processData processINcpu=null;
//Run until all jobs are done
while(allDone<processCount){
//loading processeses into ready queue based on arrival time
for(int i=0;i<processes.length;i++)
{
if(last<processes[i].arrivalTime && processes[i].arrivalTime<=time)
readyQueue.add(processes[i]);
}
//loading process in cpu at the end of ready queue based upon its completion status
if(processINcpu!=null && processINcpu.workDone!=0)
{
readyQueue.add(processINcpu);
}
//Giving CPU to process at the front of queue
processINcpu=readyQueue.remove();
//storing current status into variables for calculation purposes
last=time;
// printing cpu status at current time
toFile.println(time+" "+processINcpu.pid);
//updating time variable
time+=processINcpu.work(timeQuanta,time);
//updating number of process that are done
if(processINcpu.workDone==0) {
allDone++;
processINcpu.setWaitingTime(time-processINcpu.burstTime-processINcpu.arrivalTime);
}
}
// calculating average waiting time
calculateAverageRunningTime();
}
private void implementSJF(){
List<processData> readyQueue =new ArrayList<>();
orderMode=burstTimeOrder;
Arrays.sort(processes);
processData processINcpu=null;
int allDone=0,time=0,last=-1;
while(allDone<processCount){
for(int i=0;i<processCount;i++)
{
if(last<processes[i].arrivalTime && processes[i].arrivalTime<=time)
readyQueue.add(processes[i]);
}
Collections.sort(readyQueue);
processINcpu=readyQueue.remove(0);
last=time;
toFile.println(time+" "+processINcpu.pid);
time+=processINcpu.work(0,time);
if(processINcpu.workDone==0) {
allDone+=1;
processINcpu.setWaitingTime(time-processINcpu.burstTime-processINcpu.arrivalTime);
}
}
calculateAverageRunningTime();
}
private void implementPR_noPREMP(){
List<processData> readyQueue =new ArrayList<>();
orderMode=arrivalTimeOrder;
Arrays.sort(processes);
processData processINcpu=null;
int allDone=0,time=0,last=-1;
while(allDone<processCount){
for(int i=0;i<processCount;i++)
{
if(last<processes[i].arrivalTime && processes[i].arrivalTime<=time)
readyQueue.add(processes[i]);
}
orderMode=priorityOrder;
Collections.sort(readyQueue);
processINcpu=readyQueue.remove(0);
last=time;
toFile.println(time+" "+processINcpu.pid);
time+=processINcpu.work(0,time);
if(processINcpu.workDone==0) {
allDone+=1;
processINcpu.setWaitingTime(time-processINcpu.burstTime-processINcpu.arrivalTime);
}
}
calculateAverageRunningTime();
}
private void organizeInput() {
setup("input.txt");
Scanner firstLine = new Scanner(scan.nextLine());
Scanner secondLine = new Scanner(scan.nextLine());
while (firstLine.hasNext()) {
String type = firstLine.next();
int timeQuanta = 0;
if (type.equals("RR")) {
timeQuanta = firstLine.nextInt();
}
Scheduler_settings.add(new Scheduler(type, timeQuanta));
}
processCount = secondLine.nextInt();
processes = new processData[processCount];
for (int i = 0; i < processCount; i++) {
Scanner thisLine = new Scanner(scan.nextLine());
processes[i] = new processData(thisLine.nextInt(), thisLine.nextInt(), thisLine.nextInt(), thisLine.nextInt());
}
}
private void implementPR_withPREMP(){
List<processData> readyQueue =new ArrayList<>();
orderMode=arrivalTimeOrder;
Arrays.sort(processes);
processData processINcpu=null;
int allDone=0,time=0,last=-1;
while(allDone<processCount){
for(int i=0;i<processCount;i++)
{
if(last<processes[i].arrivalTime && processes[i].arrivalTime<=time)
readyQueue.add(processes[i]);
}
if(processINcpu!=null && processINcpu.workDone!=0)
{
readyQueue.add(processINcpu);
}
orderMode=priorityOrder;
Collections.sort(readyQueue);
processINcpu=readyQueue.remove(0);
int timeTaken=time+processINcpu.workDone;
int interruptTime=0;
for(int i=0;i<processCount;i++)
{
if(last<processes[i].arrivalTime && processes[i].arrivalTime<timeTaken && processes[i].priority<processINcpu.priority)
{
interruptTime=processes[i].arrivalTime-time;
break;
}
}
last=time;
toFile.println(time+" "+processINcpu.pid);
time+=processINcpu.work(interruptTime,time);
if(processINcpu.workDone==0) {
allDone+=1;
processINcpu.setWaitingTime(time-processINcpu.burstTime-processINcpu.arrivalTime);
}
}
calculateAverageRunningTime();
}
private void simulator(){
Iterator<Scheduler> schedulers=Scheduler_settings.iterator();
while(schedulers.hasNext())
{
Scheduler currentSetting=schedulers.next();
if(currentSetting.type.equals("RR")){
toFile.println("RR");
implementRR(currentSetting.timeQuanta);
}
else if(currentSetting.type.equals("SJF")){
toFile.println("SJF");
implementSJF();
}
else if(currentSetting.type.equals("PR_noPREMP")){
toFile.println("PR_noPREMP");
implementPR_noPREMP();
}
else if(currentSetting.type.equals("PR_withPREMP")){
toFile.println("PR_withPREMP");
implementPR_withPREMP();
}
}
}
private class Scheduler {
String type;
int timeQuanta;
Scheduler(String type, int timeQuanta) {
this.timeQuanta = timeQuanta;
this.type = type;
}
public String toString() {
return "Scheduler: " + type + " timeQuanta: " + timeQuanta;
}
}
private class processData implements Comparable {
int pid;
int arrivalTime;
int burstTime;
int priority;
int workDone;
int waitingTime;
processData(int pid, int arrivalTime, int burstTime, int priority) {
this.pid = pid;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
this.priority = priority;
this.workDone=burstTime;
this.waitingTime=0;
}
public void reset()
{
waitingTime=0;
workDone=burstTime;
}
public int work(int timeQuanta,int time)
{
int retTime=0;
if(timeQuanta>0) {
if(timeQuanta<=workDone) {
workDone -= timeQuanta;
retTime=timeQuanta;
}
else
{
retTime=workDone;
workDone=0;
}
return retTime;
}
else
{
retTime=workDone;
workDone=0;
}
return retTime;
}
public void setWaitingTime(int waitingTime){
this.waitingTime=waitingTime;
}
public int compareTo(Object p) {
switch (orderMode) {
case 0:
if (this.pid > ((processData) p).pid)
return 1;
else if (this.pid < ((processData) p).pid)
return -1;
else
return 0;
case 1:
if (this.arrivalTime > ((processData) p).arrivalTime)
return 1;
else if (this.arrivalTime < ((processData) p).arrivalTime)
return -1;
else
return 0;
case 2:
if (this.burstTime > ((processData) p).burstTime)
return 1;
else if (this.burstTime < ((processData) p).burstTime)
return -1;
else
return 0;
case 3:
if (this.priority > ((processData) p).priority)
return 1;
else if (this.priority < ((processData) p).priority)
return -1;
else
return 0;
default:
return 0;
}
}
}
}
input.txt
SJF PR_noPREMP RR 3 PR_withPREMP
4
1 0 8 3
2 3 1 1
3 5 2 4
4 6 2 2
output.txt
PR_noPREMP
0 1
8 2
9 4
11 3
AVG Waiting Time: 3.5
PR_withPREMP
0 1
3 2
4 1
6 4
8 1
11 3
AVG Waiting Time: 2.25
RR
0 1
3 2
4 1
7 3
9 4
11 1
AVG Waiting Time: 2.5
SJF
0 1
8 2
9 3
11 4
AVG Waiting Time: 3.5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.