Guest User

Untitled

a guest
Jan 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. /*
  6.  * To change this template, choose Tools | Templates
  7.  * and open the template in the editor.
  8.  */
  9.  
  10. /**
  11.  *
  12.  * @author Mustafa
  13.  */
  14. public class SecondChance extends PageReplacement {
  15.     ArrayList<PageTableEntry> validPages = new ArrayList<PageTableEntry>(); // store all the pages in memory
  16.     ArrayList<PageTableEntry> inValidPages = new ArrayList<PageTableEntry>(); // store all the not in memory
  17.  
  18.     public SecondChance(List<PageTableEntry> pageTable, boolean[] memoryFrames) {
  19.         super(pageTable, memoryFrames);
  20.        
  21.         // add all the valid pages
  22.         for(PageTableEntry pte : super.pageTable) {
  23.             if(pte.isValid()) {
  24.                 validPages.add(pte);
  25.             }
  26.         }
  27.         // add all the invalid pages
  28.         for(PageTableEntry pte : super.pageTable) {
  29.             if(!pte.isValid()) {
  30.                 inValidPages.add(pte);
  31.             }
  32.         }
  33.     }
  34.    
  35.    
  36.  
  37.     @Override
  38.     public int getTargetPage(int numberNewPTE) {
  39.         PageTableEntry topPage = super.pageTable.get(numberNewPTE);
  40.        
  41.         if(validPages.size() > 0) { // check if there is pages in memory
  42.             topPage = validPages.get(0); // get the top page of the memeory
  43.             while(topPage.isReferenced()) { // loop until you find a non refrenced
  44.                
  45.                 //take the top page out of memeory
  46.                 topPage = validPages.remove(0);
  47.                
  48.                 //change its refrence bit
  49.                 topPage.setReferenced(false);
  50.                
  51.                 // now add it to the tail
  52.                 validPages.add(topPage);
  53.                
  54.                 topPage = validPages.get(0); // get the top page again for checking.
  55.             }
  56.            
  57.             // we a got page that is not refrenced Remove it now
  58.             topPage = validPages.remove(0);
  59.            
  60.             //now add it to the tail of virtual pages
  61.             inValidPages.add(topPage);
  62.            
  63.             // now add the page number in virtual space to memory tail
  64.             if(inValidPages.size() > 0) {
  65.                 validPages.add(inValidPages.remove(0)); // take page on top of virtual memory and add it to tail of memory pages
  66.             }
  67.            
  68.         }
  69.         return topPage.getPageFrameNumber();
  70.     }
  71.    
  72. }
Add Comment
Please, Sign In to add comment