memory-and-process-scheduler

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

Clock.java (1287B)


      1 import java.util.ArrayList;
      2 
      3 /**
      4  * Clock - COMP2240A3
      5  * Clock memory management policy
      6  *
      7  * @author Cody Lewis (c3283349)
      8  */
      9 
     10 public class Clock extends FrameBuffer {
     11     private ArrayList<Boolean> useFlags;
     12     private int current;
     13 
     14     /**
     15      * Input constructor
     16      *
     17      * @param totalFrames Total amount of frames that can be used for the process
     18      */
     19     public Clock(int totalFrames) {
     20         super(totalFrames);
     21         useFlags = new ArrayList<>(totalFrames);
     22         for (int i = 0; i < totalFrames; ++i) {
     23             useFlags.add(false);
     24         }
     25         current = 0;
     26     }
     27 
     28     /**
     29      * Replace a page according to the policy
     30      *
     31      * @param newPage The page the needs to be loaded
     32      */
     33     @Override
     34     public void replace(Integer newPage) {
     35         while (useFlags.get(current)) {
     36             useFlags.set(current, false);
     37             incrementCurrent();
     38         }
     39         loadedPages.remove(buffer.get(current));
     40         buffer.set(current, newPage);
     41         loadedPages.put(buffer.get(current), current);
     42         useFlags.set(current, true);
     43         incrementCurrent();
     44     }
     45 
     46     /**
     47      * Increment the frame that that this current points to
     48      */
     49     private void incrementCurrent() {
     50         current = (current + 1) % totalFrames;
     51     }
     52 }