memory-and-process-scheduler

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

Process.java (2479B)


      1 import java.util.Queue;
      2 import java.util.LinkedList;
      3 import java.util.Scanner;
      4 import java.io.File;
      5 import java.io.FileNotFoundException;
      6 
      7 /**
      8  * Process - COMP2240A3
      9  * A process class
     10  *
     11  * @author Cody Lewis (c3283349)
     12  */
     13 
     14 public class Process {
     15     // The Integer specifies the page request
     16     private Queue<Integer> instructions;
     17     private String name;
     18     private int pid;
     19     private FrameBuffer fbuffer;
     20     private int timeBlockedTo;
     21     private LinkedList<Integer> faultTimes;
     22 
     23     public Process(int pid, String filename, int maxFrames, String fbname) throws FileNotFoundException {
     24         name = filename;
     25         this.pid = pid;
     26         faultTimes = new LinkedList<>();
     27         instructions = readInstructionFile(filename);
     28         fbuffer = FBFactory(fbname, maxFrames);
     29         timeBlockedTo = -1;
     30     }
     31 
     32     private Queue<Integer> readInstructionFile(String filename) throws FileNotFoundException {
     33         Scanner fstream = new Scanner(new File(filename));
     34         Queue<Integer> result = new LinkedList<Integer>();
     35         // TODO: Add some errors for incorrect file format
     36         String line = fstream.nextLine();
     37         while (!(line = fstream.nextLine()).equals("end")) {
     38             result.offer(Integer.parseInt(line));
     39         }
     40         return result;
     41     }
     42 
     43     private FrameBuffer FBFactory(String fbname, int maxFrames) {
     44         switch(fbname) {
     45             case "Clock":
     46                 return new Clock(maxFrames);
     47             default:
     48                 return new LRU(maxFrames);
     49         }
     50     }
     51 
     52     public boolean isReady(int time) {
     53         int nextInstruction = instructions.peek();
     54         if (!fbuffer.isLoaded(nextInstruction)) {
     55             fbuffer.replace(nextInstruction);
     56             timeBlockedTo = time + 5;
     57             faultTimes.add(time);
     58         }
     59         return timeBlockedTo < time;
     60     }
     61 
     62     public int run() {
     63         int page = instructions.poll();
     64         fbuffer.use(page);
     65         return 1;
     66     }
     67 
     68     public boolean isFinished() {
     69         return instructions.isEmpty();
     70     }
     71 
     72     public String getName() {
     73         return name;
     74     }
     75 
     76     public Integer getPid() {
     77         return pid;
     78     }
     79 
     80     public int getNumberFaults() {
     81         return faultTimes.size();
     82     }
     83 
     84     public String getFaultTimes() {
     85         return faultTimes.toString().replace("[", "{").replace("]", "}");
     86     }
     87 
     88     public String toString() {
     89         return String.format("{pid: %d, instructions_left: %d}", pid, instructions.size());
     90     }
     91 }