scheduler-simulator

git clone git://git.codymlewis.com/scheduler-simulator.git
Log | Files | Refs | README

FB.java (4876B)


      1 import java.util.ArrayList;
      2 import java.util.Comparator;
      3 import java.util.Collections;
      4 
      5 /**
      6  * <h1>FB - Comp2240A1</h1>
      7  *
      8  * The Feedback scheduler
      9  *
     10  * @author Cody Lewis (c3283349)
     11  * @version 1
     12  * @since 2019-08-17
     13  */
     14 
     15 public class FB extends Scheduler {
     16     private static final int SLICE_SIZE = 4;
     17     private String prevPid;
     18 
     19     /**
     20      * Process wrapper class that includes priorities for feedback
     21      */
     22     private class FBProcess implements Comparable<FBProcess>,
     23             Comparator<FBProcess> {
     24         private Process process;
     25         private Integer priority;
     26 
     27         /**
     28          * Default constuctor
     29          */
     30         public FBProcess() {
     31             priority = 0;
     32         }
     33 
     34         /**
     35          * Input constuctor
     36          *
     37          * @param process Process to compose this of
     38          */
     39         public FBProcess(Process process) {
     40             this();
     41             this.process = process;
     42         }
     43 
     44         /**
     45          * Lower the priority of this
     46          */
     47         public void incrementPriority() {
     48             if (priority < 5) {
     49                 priority++;
     50             }
     51         }
     52 
     53         /**
     54          * Get the process that this is composed of
     55          *
     56          * @return process contained in this
     57          */
     58         public Process getProcess() {
     59             return process;
     60         }
     61 
     62         /**
     63          * Get the priority of this
     64          *
     65          * @return Integer showing the priority of this
     66          */
     67         public Integer getPriority() {
     68             return priority;
     69         }
     70 
     71         /**
     72          * Compare the priority value of the to the other
     73          *
     74          * @param other other FBProcess to compare this to
     75          * @return this.compareTo(other) where the compared values are Integer
     76          */
     77         @Override
     78         public int compareTo(FBProcess other) {
     79             return priority.compareTo(other.getPriority());
     80         }
     81 
     82         /**
     83          * Compare two FBProcesses
     84          *
     85          * @param a a FBProcess
     86          * @param b another FBProcess
     87          * @return a.compareTo(b)
     88          */
     89         @Override
     90         public int compare(FBProcess a, FBProcess b) {
     91             return a.compareTo(b);
     92         }
     93     }
     94 
     95     private ArrayList<FBProcess> queue;
     96     private int slice;
     97 
     98     /**
     99      * Default constructor
    100      */
    101     public FB() {
    102         super();
    103         queue = new ArrayList<>();
    104         slice = 0;
    105         prevPid = "";
    106     }
    107 
    108     /**
    109      * Input constructor
    110      *
    111      * @param switchProcessTime time it takes to switch processes
    112      */
    113     public FB(int switchProcessTime) {
    114         super(switchProcessTime);
    115         queue = new ArrayList<>();
    116         slice = 0;
    117         prevPid = "";
    118     }
    119 
    120     /**
    121      * Add a process to the tail of the Queue
    122      *
    123      * @param process Process to add to the Queue
    124      */
    125     @Override
    126     public void add(Process process) {
    127         queue.add(new FBProcess(process));
    128     }
    129 
    130     /**
    131      * Add a wrapped process to the queue and sort by priority
    132      *
    133      * @param process a wrapped process
    134      */
    135     private void add(FBProcess process) {
    136         queue.add(process);
    137         Collections.sort(queue, new FBProcess());
    138     }
    139 
    140     /**
    141      * Check whether the queue is empty
    142      *
    143      * @return True if the queue is empy else false
    144      */
    145     @Override
    146     public boolean empty() {
    147         return queue.isEmpty();
    148     }
    149 
    150     /**
    151      * Process the Process at the head of the queue
    152      *
    153      * @param time current time
    154      * @return time the processing operation took
    155      */
    156     @Override
    157     public int process(int time) {
    158         if (!queue.isEmpty()) {
    159             FBProcess head = queue.get(0);
    160             if (newProcess) {
    161                 Collections.sort(queue, new FBProcess());
    162                 return switchProcess(time);
    163             } else {
    164                 int processingTime = head.getProcess()
    165                     .process(time, SLICE_SIZE);
    166                 if (head.getProcess().finished()) {
    167                     processed.add(queue.remove(0).getProcess());
    168                     newProcess = true;
    169                 } else {
    170                     head.incrementPriority();
    171                     add(queue.remove(0));
    172                     newProcess = true;
    173                 }
    174                 return processingTime;
    175             }
    176         }
    177         return 0;
    178     }
    179 
    180     /**
    181      * Switch to a new process
    182      *
    183      * @param time current time
    184      * @return time that the switching operation took
    185      */
    186     @Override
    187     protected int switchProcess(int time) {
    188         newProcess = false;
    189         slice = 0;
    190         String curPid = queue.get(0).getProcess().getPid();
    191         if (!prevPid.equals(curPid)) {
    192             startTimes += String.format(
    193                 "T%d: %s\n",
    194                 time + 1,
    195                 curPid
    196             );
    197         }
    198         prevPid = curPid;
    199         return switchProcessTime;
    200     }
    201 }