memory-and-process-scheduler

git clone git://git.codymlewis.com/memory-and-process-scheduler.git
Log | Files | Refs | README

Scheduler.java (3833B)


      1 import java.util.LinkedList;
      2 import java.util.ArrayList;
      3 import java.util.Iterator;
      4 import java.util.Collections;
      5 
      6 /**
      7  * Scheduler - COMP2240A3
      8  * The round robin Scheduler
      9  *
     10  * @author Cody Lewis (c3283349)
     11  */
     12 
     13 public class Scheduler {
     14     private class ScheduledProcess implements Comparable<ScheduledProcess> {
     15         private Process process;
     16         private int slicePoint;
     17         private int turnaroundTime;
     18 
     19         public ScheduledProcess(Process process, int slicePoint) {
     20             this.process = process;
     21             this.slicePoint = slicePoint;
     22             turnaroundTime = 0;
     23         }
     24 
     25         public Process getProcess() {
     26             return process;
     27         }
     28 
     29         public void setSlice(int slicePoint) {
     30             this.slicePoint = slicePoint;
     31         }
     32 
     33         public int getCurSlice() {
     34             return slicePoint;
     35         }
     36 
     37         public void decrementSlice() {
     38             slicePoint--;
     39         }
     40 
     41         public void finish(int time) {
     42             turnaroundTime = time;
     43         }
     44 
     45         public String summary() {
     46             return String.format(
     47                 "%d\t%s\t%d\t%d\t%s",
     48                 process.getPid(),
     49                 process.getName(),
     50                 turnaroundTime,
     51                 process.getNumberFaults(),
     52                 process.getFaultTimes()
     53             );
     54         }
     55 
     56         @Override
     57         public int compareTo(ScheduledProcess other) {
     58             return process.getPid().compareTo(other.getProcess().getPid());
     59         }
     60 
     61         public String toString() {
     62             return String.format("{process: %s, slice: %d}", process.toString(), slicePoint);
     63         }
     64     }
     65 
     66     private LinkedList<ScheduledProcess> processes;
     67     private int quantum;
     68     private int time;
     69     private LinkedList<ScheduledProcess> finishedProcesses;
     70 
     71     public Scheduler(LinkedList<Process> processes, int quantum) {
     72         this.processes = new LinkedList<>();
     73         finishedProcesses = new LinkedList<>();
     74         processes.stream().forEach(p -> this.processes.add(new ScheduledProcess(p, quantum)));
     75         this.quantum = quantum;
     76         time = 0;
     77     }
     78 
     79     public void run() {
     80         while (!processes.isEmpty()) {
     81             Iterator<ScheduledProcess> it = processes.iterator();
     82             ScheduledProcess current = null;
     83             while (it.hasNext()) {
     84                 current = it.next();
     85                 if (current.getProcess().isReady(time)) {
     86                     break;
     87                 } else {
     88                     current = null;
     89                 }
     90             }
     91             if (current == null) {
     92                 time++;
     93             } else {
     94                 while (!current.getProcess().isFinished() &&
     95                         current.getProcess().isReady(time) &&
     96                         current.getCurSlice() > 0) {
     97                     current.getProcess().run();
     98                     time++;
     99                     current.decrementSlice();
    100                 }
    101                 if (current.getProcess().isFinished() ||
    102                         current.getProcess().isReady(time) ||
    103                         current.getCurSlice() == 0) {
    104                     it.remove();
    105                     if (!current.getProcess().isFinished()) {
    106                         current.setSlice(quantum);
    107                         processes.add(current);
    108                     } else {
    109                         current.finish(time);
    110                         finishedProcesses.add(current);
    111                     }
    112                 }
    113             }
    114         }
    115     }
    116 
    117     public String summary() {
    118         String result = "PID\tProcess Name\tTurnaround Time\t# Faults\tFault Times\n";
    119         Collections.sort(finishedProcesses);
    120         for (ScheduledProcess finishedProcess : finishedProcesses) {
    121             result += finishedProcess.summary() + "\n";
    122         }
    123         return result;
    124     }
    125 }