Write a computer program to simulate Round Robin scheduling Round Robin · Proces
ID: 3684154 • Letter: W
Question
Write a computer program to simulate Round Robin scheduling
Round Robin
· Process Arrives
- They have different arrival times. Generate task arrival with random arrival times and random service time. Use the rand function to generate values.
· Upon arrival, a process is appended at the end of the ready queue, top of the queue, or right after the current process which is in service at the time of arrival
· Process Has:
§ ID
§ Service time
§ Arrival Time
-As time progresses, process will have
Start time, wait time, accumulated time, end time etc.
· As time goes on, processes will be picked up by the dispatcher one at a time from the ready queue and will be allowed to execute for one quantum
o Quantum will be a variable, use quantum = 10
o Have a variable for context switch CS, use CS = 0. Your program should be able to use any value for CS.
· Main program will call the various subprograms as needed
· Main program should also have a variable to represent clock time
· If a process completes before the time quantum has been finished, do not waste the time just doing nothing. Let another process execute following RR algorithm.
Explanation / Answer
ProcessRunner.java
import java.util.ArrayList;
import java.util.Comparator;
public class ProcessRunner {
final static int TOTAL_JOBS = 100;
final static int QUANTUM = 1;
final static int CS = 0;
static ArrayList<Process> processList;
static ProcessLogger log;
static int clockTime = 0;
static int currentIndex = 0;
static boolean preempted = false;
public static void main(String[] args) {
processList = new ArrayList<>();
processList.add(new Process(0, 0, 0));
for (int i = 1; i <= TOTAL_JOBS; i++) {
processList.add(new Process(i, arrivalTime(i), serviceTime()));
}
processList.remove(0);
log = new ProcessLogger();
while (log.size() < TOTAL_JOBS) {
if (current().getArrivalTime() > clockTime) {
if (currentIndex == 0) {
clockTime++;
} else {
currentIndex = 0;
}
} else {
preempted = false;
for (int i = 0; i < QUANTUM; i++) {
current().setAccumulatedTime(current().getAccumulatedTime() + 1);
clockTime++;
if (current().getAccumulatedTime() >= current().getServiceTime()) {
current().setEndTime(clockTime);
current().setTurnaroundTime(clockTime - current().getArrivalTime());
current().setWaitTime(
current().getEndTime() -
current().getServiceTime() -
current().arrivalTime
);
log.add(processList.remove(currentIndex));
preempted = true;
break;
}
}
clockTime += CS;
if (!preempted) {
if (currentIndex >= processList.size() - 1) {
currentIndex = 0;
} else {
currentIndex++;
}
}
}
}
log.sort();
System.out.println(log.toString());
}
public static Process current() {
if (!processList.isEmpty()) {
return processList.get(currentIndex);
}
return null;
}
public static double arrivalTime(int id) {
double intertime = interArrivalTime();
System.out.println(intertime);
return Math.ceil(processList.get(id - 1).getArrivalTime() + intertime);
}
public static double interArrivalTime() {
return -0.2 * Math.log(1 - Math.random());
}
public static double serviceTime() {
return Math.ceil(2 + (5 - 2) * Math.random());
}
}
Process.java
public class Process {
long id;
double arrivalTime;
double serviceTime;
double waitTime;
double endTime;
double accumulatedTime;
double turnaroundTime;
Process(long id, double arrivalTime, double serviceTime)
{
this.id = id;
this.arrivalTime = arrivalTime;
this.serviceTime = serviceTime;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public double getArrivalTime() {
return arrivalTime;
}
public void setArrivalTime(double arrivalTime) {
this.arrivalTime = arrivalTime;
}
public double getServiceTime() {
return serviceTime;
}
public void setServiceTime(double serviceTime) {
this.serviceTime = serviceTime;
}
public double getWaitTime() {
return waitTime;
}
public void setWaitTime(double waitTime) {
this.waitTime = waitTime;
}
public double getEndTime() {
return endTime;
}
public void setEndTime(double endTime) {
this.endTime = endTime;
}
public double getAccumulatedTime() {
return accumulatedTime;
}
public void setAccumulatedTime(double accumulatedTime) {
this.accumulatedTime = accumulatedTime;
}
public double getTurnaroundTime() {
return turnaroundTime;
}
public void setTurnaroundTime(double turnaroundTime) {
this.turnaroundTime = turnaroundTime;
}
public String toString() { return "Process " + id + ": " + getWaitTime() + " " + getEndTime(); }
}
ProcessLogger.java
import java.util.ArrayList;
import java.util.Comparator;
public class ProcessLogger extends ArrayList<Process> {
double totalWaitTime;
double totalTurnaroundTime;
ProcessLogger() {
super();
totalWaitTime = 0;
totalTurnaroundTime = 0;
}
@Override
public boolean add(Process process) {
addTotalWaitTime(process.getWaitTime());
addTotalTurnaroundTime(process.getTurnaroundTime());
return super.add(process);
}
public void sort() {
this.sort(new Comparator<Process>() {
@Override
public int compare(Process o1, Process o2) {
if (o1.getId() < o2.getId()) {
return -1;
} else if (o1.getId() > o2.getId()) {
return 1;
}
return 0;
}
});
}
@Override
public String toString() {
if (this.isEmpty()) {
return "EMPTY PROCESS LOGGER";
}
String list = new String();
list += "PROCESS LOGGER " +
" TOTAL_PROCESSES_COMPLETED: " + this.size() + " " +
" AVERAGE_TURNAROUND_TIME: " + this.getAverageTurnaroundTime() + " " +
" AVERAGE_WAIT_TIME: " + this.getAverageWaitTime() + " " +
" PROCESS_LIST: ";
for (int i = 0; i < this.size(); i++) {
list += " PROCESS " + this.get(i).getId() + " " +
" END TIME: " + this.get(i).getEndTime() + " " +
" TURNAROUND TIME: " + this.get(i).getTurnaroundTime() + " " +
" WAIT TIME: " + this.get(i).getWaitTime() + " ";
}
return list;
}
public void addTotalWaitTime(double time) {
this.totalWaitTime += time;
}
public void addTotalTurnaroundTime(double time) {
this.totalTurnaroundTime += time;
}
public double getAverageWaitTime() {
if (!this.isEmpty()) { return this.totalWaitTime / this.size(); }
else { return 0; }
}
public double getAverageTurnaroundTime() {
if (!this.isEmpty()) { return this.totalTurnaroundTime / this.size(); }
else { return 0; }
}
}
ProcessQueue.java
import java.util.ArrayList;
public class ProcessQueue extends ArrayList<Process> {
ArrayList<Process> list;
}
sample output
0.07264525898451231
0.4575002652496899
0.07606324601308
0.10165710338515913
0.05288095534713003
0.5383155071285267
0.4296789216923756
0.1515379199638421
0.03208653252449137
0.2480098832185439
0.45276328068014665
0.45516153750203686
0.7779228302672317
0.149568986056484
0.09013516867718979
0.3891983196413344
0.05790203289327998
0.22009716041163008
0.3082872852723024
0.14615045635125434
0.12286488038658126
0.04364653063107668
0.5031206162932808
0.06361356508062829
0.054515999715798596
0.1289715262270734
0.007380113725917016
0.23449100052816796
0.015433121458636588
0.7836446941369103
0.015237034990389822
0.6217474157037365
0.15094953107454367
0.045840451914813465
0.060196177568290855
0.0589168657875234
0.0558870323488965
0.2485999469716826
0.020969648472708935
0.3348867574384563
0.03847559590981303
0.19493541231665223
0.33819459947282154
0.13005785170298417
0.3649588077717563
0.022998859839013622
0.19581616005070523
0.012670311820759218
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.