Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author Mustafa
- */
- public class SecondChance extends PageReplacement {
- ArrayList<PageTableEntry> validPages = new ArrayList<PageTableEntry>(); // store all the pages in memory
- ArrayList<PageTableEntry> inValidPages = new ArrayList<PageTableEntry>(); // store all the not in memory
- public SecondChance(List<PageTableEntry> pageTable, boolean[] memoryFrames) {
- super(pageTable, memoryFrames);
- // add all the valid pages
- for(PageTableEntry pte : super.pageTable) {
- if(pte.isValid()) {
- validPages.add(pte);
- }
- }
- // add all the invalid pages
- for(PageTableEntry pte : super.pageTable) {
- if(!pte.isValid()) {
- inValidPages.add(pte);
- }
- }
- }
- @Override
- public int getTargetPage(int numberNewPTE) {
- PageTableEntry topPage = super.pageTable.get(numberNewPTE);
- if(validPages.size() > 0) { // check if there is pages in memory
- topPage = validPages.get(0); // get the top page of the memeory
- while(topPage.isReferenced()) { // loop until you find a non refrenced
- //take the top page out of memeory
- topPage = validPages.remove(0);
- //change its refrence bit
- topPage.setReferenced(false);
- // now add it to the tail
- validPages.add(topPage);
- topPage = validPages.get(0); // get the top page again for checking.
- }
- // we a got page that is not refrenced Remove it now
- topPage = validPages.remove(0);
- //now add it to the tail of virtual pages
- inValidPages.add(topPage);
- // now add the page number in virtual space to memory tail
- if(inValidPages.size() > 0) {
- validPages.add(inValidPages.remove(0)); // take page on top of virtual memory and add it to tail of memory pages
- }
- }
- return topPage.getPageFrameNumber();
- }
- }
Add Comment
Please, Sign In to add comment