scheduler-simulator

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

NRR.java (5214B)


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