scheduler-simulator

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

Process.java (3776B)


      1 /**
      2  * <h1>Process - Comp2240A1</h1>
      3  *
      4  * A simple glorified struct style class for a process.
      5  *
      6  * @author Cody Lewis (c3283349)
      7  * @version 1
      8  * @since 2019-08-16
      9  */
     10 
     11 public class Process {
     12     private String pid; // ID
     13     private Integer arrivalTime; // Arrive
     14     private Integer serviceTime; // ExecSize
     15     private Integer turnaroundTime;
     16     private Integer waitingTime;
     17     private Integer lastProcessedTime;
     18     private Integer startTime;
     19 
     20     /**
     21      * The default constructor
     22      */
     23     public Process() {
     24         pid = "";
     25         arrivalTime = 0;
     26         serviceTime = 0;
     27         turnaroundTime = 0;
     28         waitingTime = 0;
     29         lastProcessedTime = 0;
     30         startTime = 0;
     31     }
     32 
     33     /**
     34      * Member setting constructor
     35      *
     36      * @param pid process id
     37      * @param arrivalTime time the the process arrived at the processor
     38      * @param serviceTime time the process takes to execute
     39      */
     40     public Process(String pid, int arrivalTime, int serviceTime) {
     41         this();
     42         this.pid = pid;
     43         this.arrivalTime = arrivalTime;
     44         this.serviceTime = serviceTime;
     45         this.lastProcessedTime = arrivalTime;
     46     }
     47 
     48     /**
     49      * Get the process id
     50      *
     51      * @return process id
     52      */
     53     public String getPid() {
     54         return pid;
     55     }
     56 
     57     /**
     58      * Get the time that the process arrived at the processor
     59      *
     60      * @return time that the process arrived at the processor
     61      */
     62     public Integer getArrivalTime() {
     63         return arrivalTime;
     64     }
     65 
     66     /**
     67      * Get the time that the process takes to execute
     68      *
     69      * @return time that the process takes to execute
     70      */
     71     public Integer getServiceTime() {
     72         return serviceTime;
     73     }
     74 
     75     /**
     76      * Set the process id
     77      *
     78      * @param pid process id
     79      */
     80     public void setPid(String pid) {
     81         this.pid = pid;
     82     }
     83 
     84     /**
     85      * Set the time that the process arrived at the processor
     86      *
     87      * @param arrivalTime time that the process arrived at the processor
     88      */
     89     public void setArrivalTime(Integer arrivalTime) {
     90         this.arrivalTime = arrivalTime;
     91     }
     92 
     93     /**
     94      * Set the time that the process takes to execute
     95      *
     96      * @param serviceTime time that the process takes to execute
     97      */
     98     public void setServiceTime(Integer serviceTime) {
     99         this.serviceTime = serviceTime;
    100     }
    101 
    102     /**
    103      * Process for the slice of time
    104      *
    105      * @param time current time
    106      * @param slice Time slice allocate to this, 0 means no slicing
    107      * @return amount of time taken
    108      */
    109     public int process(int time, int slice) {
    110         if (lastProcessedTime == arrivalTime) {
    111             startTime = time;
    112         }
    113         waitingTime += (time - lastProcessedTime);
    114         int timeTaken = 0;
    115         if (slice == 0) {
    116             timeTaken = serviceTime;
    117         } else {
    118             timeTaken = Integer.min(slice, serviceTime);
    119         }
    120         turnaroundTime += (time - lastProcessedTime + timeTaken);
    121         serviceTime -= timeTaken;
    122         lastProcessedTime = time + timeTaken;
    123         return timeTaken;
    124     }
    125 
    126     /**
    127      * Get the turnaround time
    128      *
    129      * @return Time taken waiting and excuting
    130      */
    131     public int getTurnaroundTime() {
    132         return turnaroundTime;
    133     }
    134 
    135     /**
    136      * Get the waiting time
    137      *
    138      * @return Time taken waiting
    139      */
    140     public int getWaitingTime() {
    141         return waitingTime;
    142     }
    143 
    144     /**
    145      * Get the start time
    146      *
    147      * @return time when execution started
    148      */
    149     public int getStartTime() {
    150         return startTime;
    151     }
    152 
    153     /**
    154      * Query whether this has finished processing
    155      *
    156      * @return true if finished else false
    157      */
    158     public boolean finished() {
    159         return serviceTime == 0;
    160     }
    161 }