Write a program (preferably in c++ or java as they are the easiest for me to und
ID: 3806361 • Letter: W
Question
Write a program (preferably in c++ or java as they are the easiest for me to understand, but any similar language is fine) that will simulate FCFS, SJN, SRT, and round robin scheduling algorithms. For each algorithm, the program should compute and out put turnaround time and wating time of every job as well as the average waiting time and average turnaround time. The time quantum for round robin is 4 ms. (assume that the context switching time is 0). Input should be read from a file (who's name will be input from the user) with the following format as this example:
5
1 3 10
2 4 15
3 6 8
4 7 3
5 9 12
The first line in this file represents the number of processes.
Each line of the next lines represents one process. Each process is represented by three values: Job ID, Arrival time, CPU Cycle time
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.march;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
/**
*
* @author Sam
*/
public class Scheduling {
public static void fcfs(int[][] process){
int[][] processCopy = new int[process.length][];
for (int i=0; i<process.length; i++){
processCopy[i] = new int[process[i].length];
System.arraycopy(process[i], 0, processCopy[i], 0, process[i].length);
}
java.util.Arrays.sort(processCopy, (int[] a, int[] b) -> Double.compare(a[1], b[1])); //sort according to arrival time
int time = 0;
int totalWaitTime = 0;
int totalTATime = 0;
System.out.println("Pid wait time tat ");
for (int[] p:processCopy){
int waitTime = Math.max(time,p[1])-p[1];
totalWaitTime += waitTime;
time = Math.max(time,p[1])+p[2];
int tat = waitTime + p[2];
totalTATime += tat;
System.out.println(p[0]+" "+waitTime+" "+tat);
}
System.out.println("Avg wait time= "+(totalWaitTime/processCopy.length));
System.out.println("Avg turn around time= "+(totalTATime/processCopy.length));
}
public static void sjn(int[][] process){
boolean[] served = new boolean[process.length];
int time=Integer.MAX_VALUE;
for (int[] proces : process) {
if (proces[1] < time) {
time = proces[1]; //time is set to minimum value
}
}
int servedJob = 0;
int totalWaitTime = 0;
int totalTATime = 0;
System.out.println("Pid wait time tat ");
while (servedJob < process.length) {
int selectedProcess=-1;
for (int i =0; i<process.length; i++){
if (!served[i] && process[i][1]<=time){ //if the process is not served and has arrived already
if (selectedProcess == -1)
selectedProcess = i;
else if(process[i][2]<process[selectedProcess][2])
selectedProcess = i;
}
}
if (selectedProcess == -1){ //there is no process in queue at time time{
time = Integer.MAX_VALUE;
for (int i =1; i<process.length; i++)
if (!served[i] && process[i][1]<=time){
selectedProcess = i;
time = process[i][1];
}
}
int waitTime = time - process[selectedProcess][1];
int tat = waitTime + process[selectedProcess][2];
totalTATime += tat;
totalWaitTime += waitTime;
time += process[selectedProcess][2];
served[selectedProcess] = true;
servedJob++;
System.out.println(process[selectedProcess][0]+" "+waitTime+" "+tat);
}
System.out.println("Avg wait time= "+(totalWaitTime/process.length));
System.out.println("Avg turn around time= "+(totalTATime/process.length));
}
public static void roundRobin(int[][] process, int quantum){
int[][] processCopy = new int[process.length][];
boolean arrived[] = new boolean[process.length];
LinkedList<int[]> processList = new LinkedList<>();
for (int i=0; i<process.length; i++){
processCopy[i] = new int[process[i].length+1];
System.arraycopy(process[i], 0, processCopy[i], 0, process[i].length);
processCopy[i][process[i].length] = process[i][process[i].length-1];
}
java.util.Arrays.sort(processCopy, (int[] a, int[] b) -> Double.compare(a[1], b[1])); //sort according to arrival time
processList.add(processCopy[0]);
arrived[0]=true;
int time = processCopy[0][1];
int totalWaitTime = 0;
int totalTATime = 0;
int servedJobs = 0;
System.out.println("Pid wait time tat ");
while(servedJobs < processCopy.length){
int[] currentProcess = processList.remove(0); //remove first process in queue
time = time + Math.min(quantum, currentProcess[3]);
currentProcess[3] -= quantum;
for (int i=0; i<processCopy.length; i++) // add jobs that arrived mean while
if (!arrived[i] && processCopy[i][1]<=time){
processList.add(processCopy[i]);
arrived[i]=true;
}
if (currentProcess[3] <= 0){ //if current process completes its burst
int tat = time - currentProcess[1]; //tat = finishing time - arrival time
System.out.println(currentProcess[0]+" "+(tat-currentProcess[2])/*wait time is tat - total burst time*/+" "+tat);
totalTATime += tat;
totalWaitTime += (tat-currentProcess[2]);/*wait time is tat - total burst time*/
servedJobs ++;
}
else
processList.add(currentProcess);
if(processList.isEmpty()){ //if processor is idle, skip time to next job
time = Integer.MAX_VALUE;
for (int i=0; i<processCopy.length; i++) // add jobs that arrives next
if (!arrived[i]){
processList.add(processCopy[i]);
arrived[i]=true;
time = processCopy[i][0];
break;
}
}
}
System.out.println("Avg wait time= "+(totalWaitTime/process.length));
System.out.println("Avg turn around time= "+(totalTATime/process.length));
}
public static void main(String[] args) throws FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new FileReader("File name here"));
int size = Integer.parseInt(br.readLine());
int process[][] = new int[size][];
for (int i = 0; i<process.length; i++){
String[] tokens = br.readLine().split(" ");
process[i]= new int[tokens.length];
for (int j=0; j<tokens.length; j++)
process[i][j] = Integer.parseInt(tokens[j]);
}
fcfs(process);
sjn(process);
roundRobin(process, 4);
}
}
I have commented the critical areas, in case of doubt please comment in the comment section below
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.