commit d3887d8c15746f1ab207f8b1385ab2c69ddf25ce
parent facc3d081a1b0be5c6faf81fd4cc72ddf1967e9f
Author: Cody Lewis <cody@codymlewis.com>
Date: Fri, 23 Aug 2019 08:28:01 +1000
Added comments
Diffstat:
9 files changed, 270 insertions(+), 48 deletions(-)
diff --git a/Dispatcher.java b/Dispatcher.java
@@ -22,19 +22,12 @@ public class Dispatcher {
private Map<Integer, LinkedList<Process>> processes;
/**
- * Default constructor
- */
- public Dispatcher() {
- }
-
- /**
* Input constructor
*
* @param schedulerAlgorithm Name of the scheduler algorithm to use
* @param filename Name of the data file
*/
public Dispatcher(String schedulerAlgorithm, String filename) {
- this();
processes = parseDataFile(filename);
scheduler = schedulerFactory(schedulerAlgorithm);
}
diff --git a/FB.java b/FB.java
@@ -5,7 +5,7 @@ import java.util.Collections;
/**
* <h1>FB - Comp2240A1</h1>
*
- * The round robin scheduler
+ * The Feedback scheduler
*
* @author Cody Lewis
* @version 1
@@ -16,44 +16,80 @@ public class FB extends Scheduler {
private static final int SLICE_SIZE = 4;
private String prevPid;
- private class FBProcess implements Comparable<FBProcess>, Comparator<FBProcess> {
+ /**
+ * Process wrapper class that includes priorities for feedback
+ */
+ private class FBProcess implements Comparable<FBProcess>,
+ Comparator<FBProcess> {
private Process process;
private Integer priority;
+ /**
+ * Default constuctor
+ */
public FBProcess() {
priority = 0;
}
+ /**
+ * Input constuctor
+ *
+ * @param process Process to compose this of
+ */
public FBProcess(Process process) {
+ this();
this.process = process;
- priority = 0;
}
+ /**
+ * Lower the priority of this
+ */
public void incrementPriority() {
if (priority < 5) {
priority++;
}
}
+ /**
+ * Get the process that this is composed of
+ *
+ * @return process contained in this
+ */
public Process getProcess() {
return process;
}
+ /**
+ * Get the priority of this
+ *
+ * @return Integer showing the priority of this
+ */
public Integer getPriority() {
return priority;
}
+ /**
+ * Compare the priority value of the to the other
+ *
+ * @param other other FBProcess to compare this to
+ * @return this.compareTo(other) where the compared values are Integer
+ */
+ @Override
public int compareTo(FBProcess other) {
return priority.compareTo(other.getPriority());
}
+ /**
+ * Compare two FBProcesses
+ *
+ * @param a a FBProcess
+ * @param b another FBProcess
+ * @return a.compareTo(b)
+ */
+ @Override
public int compare(FBProcess a, FBProcess b) {
return a.compareTo(b);
}
-
- public String toString() {
- return String.format("%s: %d", process.toString(), priority);
- }
}
private ArrayList<FBProcess> queue;
@@ -69,6 +105,11 @@ public class FB extends Scheduler {
prevPid = "";
}
+ /**
+ * Input constructor
+ *
+ * @param switchProcessTime time it takes to switch processes
+ */
public FB(int switchProcessTime) {
super(switchProcessTime);
queue = new ArrayList<>();
@@ -86,6 +127,11 @@ public class FB extends Scheduler {
queue.add(new FBProcess(process));
}
+ /**
+ * Add a wrapped process to the queue and sort by priority
+ *
+ * @param process a wrapped process
+ */
private void add(FBProcess process) {
queue.add(process);
Collections.sort(queue, new FBProcess());
@@ -101,6 +147,12 @@ public class FB extends Scheduler {
return queue.isEmpty();
}
+ /**
+ * Process the Process at the head of the queue
+ *
+ * @param time current time
+ * @return time the processing operation took
+ */
@Override
public int process(int time) {
if (!queue.isEmpty()) {
@@ -125,6 +177,12 @@ public class FB extends Scheduler {
return 0;
}
+ /**
+ * Switch to a new process
+ *
+ * @param time current time
+ * @return time that the switching operation took
+ */
@Override
protected int switchProcess(int time) {
newProcess = false;
diff --git a/FCFS.java b/FCFS.java
@@ -22,6 +22,11 @@ public class FCFS extends Scheduler {
queue = new LinkedList<>();
}
+ /**
+ * Input constructor
+ *
+ * @param switchProcessTime time it take to switch processes
+ */
public FCFS(int switchProcessTime) {
super(switchProcessTime);
queue = new LinkedList<>();
@@ -47,6 +52,12 @@ public class FCFS extends Scheduler {
return queue.peek() == null;
}
+ /**
+ * Process the Process at the head of the queue
+ *
+ * @param time current time
+ * @return time the processing operation took
+ */
@Override
public int process(int time) {
Process head = queue.peek();
@@ -65,6 +76,12 @@ public class FCFS extends Scheduler {
return 0;
}
+ /**
+ * Switch to a new process
+ *
+ * @param time current time
+ * @return time that the switching operation took
+ */
@Override
protected int switchProcess(int time) {
newProcess = false;
diff --git a/InvalidDataException.java b/InvalidDataException.java
@@ -1,3 +1,13 @@
+/**
+ * <h1>InvalidDataException - Comp2240A1</h1>
+ *
+ * Exception to be called when the data file is formatted incorrectly
+ *
+ * @author Cody Lewis
+ * @version 1
+ * @since 2019-08-17
+ */
+
public class InvalidDataException extends Exception {
static final long serialVersionUID = 1L;
public InvalidDataException() {
diff --git a/NRR.java b/NRR.java
@@ -3,9 +3,9 @@ import java.util.Comparator;
import java.util.Collections;
/**
- * <h1>RR - Comp2240A1</h1>
+ * <h1>NRR - Comp2240A1</h1>
*
- * The round robin scheduler
+ * The narrow round robin scheduler
*
* @author Cody Lewis
* @version 1
@@ -15,6 +15,9 @@ import java.util.Collections;
public class NRR extends Scheduler {
String prevPid;
+ /**
+ * Wrapper class that is inclusive of the scheduling of this
+ */
private class NarrowProcess implements Comparable<NarrowProcess>,
Comparator<NarrowProcess> {
private Process process;
@@ -22,6 +25,12 @@ public class NRR extends Scheduler {
private boolean used;
private Integer time;
+ /**
+ * Input constructor
+ *
+ * @param process Process that this will be composed of
+ * @param time time that this has been placed in the queue
+ */
public NarrowProcess(Process process, int time) {
this.process = process;
sliceSize = 4;
@@ -29,19 +38,36 @@ public class NRR extends Scheduler {
this.time = time;
}
+ /**
+ * Get the current size of a time slice for this
+ *
+ * @return current slice size
+ */
public int getSliceSize() {
return sliceSize;
}
-
+ /**
+ * Get the time that this was placed in the queue
+ *
+ * @return time a queue placement
+ */
public Integer getTime() {
return time;
}
+ /**
+ * Update the time of queue placement
+ *
+ * @param time new time of placement
+ */
public void setTime(int time) {
this.time = time;
}
+ /**
+ * Make the slice size a unit smaller
+ */
public void decrementSliceSize() {
if (used) {
sliceSize--;
@@ -50,15 +76,33 @@ public class NRR extends Scheduler {
}
}
+ /**
+ * Get the process contained in this
+ *
+ * @return process that this is composed of
+ */
public Process getProcess() {
return process;
}
+ /**
+ * Compare the time of queue placement of this to other
+ *
+ * @param other Another NarrowProcess
+ * @return this.time.compareTo(other.time)
+ */
@Override
public int compareTo(NarrowProcess other) {
return time.compareTo(other.getTime());
}
+ /**
+ * Compare two NarrowProcesses
+ *
+ * @param a A NarrowProcess
+ * @param b another NarrowProcess
+ * @return a.compareTo(b)
+ */
@Override
public int compare(NarrowProcess a, NarrowProcess b) {
return a.compareTo(b);
@@ -78,6 +122,11 @@ public class NRR extends Scheduler {
prevPid = "";
}
+ /**
+ * Input constructor
+ *
+ * @param switchProcessTime Time it takes to switch process
+ */
public NRR(int switchProcessTime) {
super(switchProcessTime);
queue = new ArrayList<>();
@@ -106,6 +155,12 @@ public class NRR extends Scheduler {
return queue.isEmpty();
}
+ /**
+ * Process the Process at the head of the queue
+ *
+ * @param time current time
+ * @return time the processing operation took
+ */
@Override
public int process(int time) {
if (!queue.isEmpty()) {
@@ -130,6 +185,12 @@ public class NRR extends Scheduler {
return 0;
}
+ /**
+ * Switch to a new process
+ *
+ * @param time current time
+ * @return time that the switching operation took
+ */
@Override
protected int switchProcess(int time) {
newProcess = false;
diff --git a/Process.java b/Process.java
@@ -47,6 +47,7 @@ public class Process {
/**
* Get the process id
+ *
* @return process id
*/
public String getPid() {
@@ -55,6 +56,7 @@ public class Process {
/**
* Get the time that the process arrived at the processor
+ *
* @return time that the process arrived at the processor
*/
public Integer getArrivalTime() {
@@ -63,6 +65,7 @@ public class Process {
/**
* Get the time that the process takes to execute
+ *
* @return time that the process takes to execute
*/
public Integer getServiceTime() {
@@ -71,6 +74,7 @@ public class Process {
/**
* Set the process id
+ *
* @param pid process id
*/
public void setPid(String pid) {
@@ -79,6 +83,7 @@ public class Process {
/**
* Set the time that the process arrived at the processor
+ *
* @param arrivalTime time that the process arrived at the processor
*/
public void setArrivalTime(Integer arrivalTime) {
@@ -87,26 +92,35 @@ public class Process {
/**
* Set the time that the process takes to execute
+ *
* @param serviceTime time that the process takes to execute
*/
public void setServiceTime(Integer serviceTime) {
this.serviceTime = serviceTime;
}
+ /**
+ * Process for the slice of time
+ *
+ * @param time current time
+ * @param slice Time slice allocate to this, 0 means no slicing
+ * @return amount of time taken
+ */
public int process(int time, int slice) {
if (lastProcessedTime == arrivalTime) {
startTime = time;
}
waitingTime += (time - lastProcessedTime);
+ int timeTaken = 0;
if (slice == 0) {
- slice = serviceTime;
+ timeTaken = serviceTime;
} else {
- slice = Integer.min(slice, serviceTime);
+ timeTaken = Integer.min(slice, serviceTime);
}
- turnaroundTime += (time - lastProcessedTime + slice);
- serviceTime -= slice;
- lastProcessedTime = time + slice;
- return slice;
+ turnaroundTime += (time - lastProcessedTime + timeTaken);
+ serviceTime -= timeTaken;
+ lastProcessedTime = time + timeTaken;
+ return timeTaken;
}
/**
@@ -136,13 +150,13 @@ public class Process {
return startTime;
}
+ /**
+ * Query whether this has finished processing
+ *
+ * @return true if finished else false
+ */
public boolean finished() {
return serviceTime == 0;
}
-
- public String toString() {
- return pid;
- }
-
}
diff --git a/RR.java b/RR.java
@@ -13,52 +13,75 @@ import java.util.Collections;
*/
public class RR extends Scheduler {
- String prevPid;
+ private String prevPid;
private static final int SLICE_SIZE = 4;
- private class RRProcess implements Comparable<RRProcess>, Comparator<RRProcess> {
+ /**
+ * A process wrapper class for Processes in this scheduler
+ */
+ private class RRProcess implements Comparable<RRProcess>,
+ Comparator<RRProcess> {
private Process process;
- private int sliceSize;
private boolean used;
private Integer time;
+ /**
+ * Input constructor
+ *
+ * @param process process that this will be composed of
+ * @param time time this was last placed in the queue
+ */
public RRProcess(Process process, int time) {
this.process = process;
- sliceSize = 4;
used = false;
this.time = time;
}
- public int getSliceSize() {
- return sliceSize;
- }
-
-
+ /**
+ * Get time this was last placed in the queue
+ *
+ * @return time of last placement in the queue
+ */
public Integer getTime() {
return time;
}
+ /**
+ * Update time of last placement in the queue
+ *
+ * @param time time last placed in queue
+ */
public void setTime(int time) {
this.time = time;
}
- public void decrementSliceSize() {
- if (used) {
- sliceSize--;
- } else {
- used = true;
- }
- }
-
+ /**
+ * Get the process contained in
+ *
+ * @return process this is composed of
+ */
public Process getProcess() {
return process;
}
+ /**
+ * Compare the time of placement between this and other RRProcess
+ *
+ * @param other another RRProcess
+ * @return Integer compareTo of the times
+ */
@Override
public int compareTo(RRProcess other) {
return time.compareTo(other.getTime());
}
+ /**
+ * Compare 2 processes
+ *
+ * @param a A RRProcess
+ * @param b another RRProcess
+ * @return a.compareTo(b)
+ */
@Override
public int compare(RRProcess a, RRProcess b) {
return a.compareTo(b);
@@ -78,6 +101,11 @@ public class RR extends Scheduler {
prevPid = "";
}
+ /**
+ * Input Constructor
+ *
+ * @param switchProcessTime time to take to switch processes
+ */
public RR(int switchProcessTime) {
super(switchProcessTime);
queue = new ArrayList<>();
@@ -106,6 +134,13 @@ public class RR extends Scheduler {
return queue.isEmpty();
}
+
+ /**
+ * Process the Process at the head of the queue
+ *
+ * @param time current time
+ * @return time the processing operation took
+ */
@Override
public int process(int time) {
if (!queue.isEmpty()) {
@@ -130,11 +165,16 @@ public class RR extends Scheduler {
return 0;
}
+ /**
+ * Switch to a new process
+ *
+ * @param time current time
+ * @return time that the switching operation took
+ */
@Override
protected int switchProcess(int time) {
newProcess = false;
slice = 0;
- queue.get(0).decrementSliceSize();
startTimes += String.format(
"T%d: %s\n",
time + 1,
diff --git a/Scheduler.java b/Scheduler.java
@@ -17,6 +17,9 @@ public abstract class Scheduler {
protected boolean newProcess;
protected String startTimes;
+ /**
+ * Default constructor
+ */
protected Scheduler() {
processed = new ArrayList<>();
switchProcessTime = 0;
@@ -24,6 +27,11 @@ public abstract class Scheduler {
startTimes = "";
}
+ /**
+ * Input constructor
+ *
+ * @param switchProcessTime Time to take to switch processes
+ */
protected Scheduler(int switchProcessTime) {
this();
this.switchProcessTime = switchProcessTime;
@@ -43,9 +51,23 @@ public abstract class Scheduler {
*/
public abstract boolean empty();
+ /**
+ * Process the Process at the head of the queue
+ *
+ * @param time current time
+ * @return time the processing operation took
+ */
public abstract int process(int time);
/**
+ * Switch to a new process
+ *
+ * @param time current time
+ * @return time that the switching operation took
+ */
+ protected abstract int switchProcess(int time);
+
+ /**
* Give the results of the simulation, such as the times the process was
* execute
*
@@ -86,6 +108,4 @@ public abstract class Scheduler {
return String.format("%.2f\t\t\t%.2f", avgTurnaround, avgWaiting);
}
-
- protected abstract int switchProcess(int time);
}
diff --git a/SortbyPid.java b/SortbyPid.java
@@ -1,5 +1,14 @@
import java.util.Comparator;
+/**
+ * <h1>SortbyPid - Comp2240A1</h1>
+ * Sort the processes by their id
+ *
+ * @author Cody Lewis
+ * @version 1
+ * @since 2019-08-17
+ */
+
public class SortbyPid implements Comparator<Process> {
public int compare(Process a, Process b) {
return a.getPid().compareTo(b.getPid());