scheduler-simulator

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

FCFS.java (2099B)


      1 import java.util.LinkedList;
      2 import java.util.Queue;
      3 
      4 /**
      5  * <h1>FCFS - Comp2240A1</h1>
      6  *
      7  * The first come fist served scheduler
      8  *
      9  * @author Cody Lewis (c3283349)
     10  * @version 1
     11  * @since 2019-08-17
     12  */
     13 
     14 public class FCFS extends Scheduler {
     15     private Queue<Process> queue;
     16 
     17     /**
     18      * Default constructor
     19      */
     20     public FCFS() {
     21         super();
     22         queue = new LinkedList<>();
     23     }
     24 
     25     /**
     26      * Input constructor
     27      *
     28      * @param switchProcessTime time it take to switch processes
     29      */
     30     public FCFS(int switchProcessTime) {
     31         super(switchProcessTime);
     32         queue = new LinkedList<>();
     33     }
     34 
     35     /**
     36      * Add a process to the tail of the Queue
     37      *
     38      * @param process Process to add to the Queue
     39      */
     40     @Override
     41     public void add(Process process) {
     42         queue.add(process);
     43     }
     44 
     45     /**
     46      * Check whether the queue is empty
     47      *
     48      * @return True if the queue is empy else false
     49      */
     50     @Override
     51     public boolean empty() {
     52         return queue.peek() == null;
     53     }
     54 
     55     /**
     56      * Process the Process at the head of the queue
     57      *
     58      * @param time current time
     59      * @return time the processing operation took
     60      */
     61     @Override
     62     public int process(int time) {
     63         Process head = queue.peek();
     64         if (head != null) {
     65             if (newProcess) {
     66                 return switchProcess(time);
     67             } else {
     68                 int processingTime = head.process(time, 0);
     69                 if (head.finished()) {
     70                     processed.add(queue.poll());
     71                     newProcess = true;
     72                 }
     73                 return processingTime;
     74             }
     75         }
     76         return 0;
     77     }
     78 
     79     /**
     80      * Switch to a new process
     81      *
     82      * @param time current time
     83      * @return time that the switching operation took
     84      */
     85     @Override
     86     protected int switchProcess(int time) {
     87         newProcess = false;
     88         startTimes += String.format(
     89             "T%d: %s\n",
     90             time + 1,
     91             queue.peek().getPid()
     92         );
     93         return switchProcessTime;
     94     }
     95 }