scheduler-simulator

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

RR.java (4653B)


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