public void addPage(PageTableEntry pte, int step)
        {
            bool pageAdded             = false;
            int  longestDeltaTime      = 0;
            int  longestDeltaTimeIndex = 0;
            int  index = 0;

            pte.setModified(step);

            //see if there is an empty slot (LRU algorithm)
            foreach (PhysicalFrame pf in pfList)
            {
                int newDelta = step - pf.getStepLastReferenced();
                if (newDelta > longestDeltaTime)
                {
                    longestDeltaTime      = newDelta;
                    longestDeltaTimeIndex = index;
                }

                if (pf.isEmpty())
                {
                    pageFaultCounter++;
                    pte.getPageTable().getOwningProcess().incrementPageFaultCounter();
                    pf.fillFrame(pte);
                    pageAdded = true;
                    break;
                }
                index++;
            }

            if (pageAdded)
            {
                return;
            }

            pageFaultCounter++;
            pte.getPageTable().getOwningProcess().incrementPageFaultCounter();
            pfList[longestDeltaTimeIndex].fillFrame(pte);
        }
Esempio n. 2
0
        public void addPage(int frameId, int step)
        {
            bool added = false;

            for (int x = 0; x < 16; x++)
            {
                //an empty valid frame was found in page table
                if (entryList[x].isEmpty())
                {
                    entryList[x].setFrame(frameId);
                    added = true;
                    pm.addPage(entryList[x], owningProcess.getPid());
                    break;
                }
            }

            if (added)
            {
                return;
            }

            //a valid page wasnt found, make room for the frame in valid page
            int leasedUsed      = 0;
            int leasedUsedIndex = 0;
            int index           = 0;

            foreach (PageTableEntry pte in entryList)
            {
                //find the page which was lease recently used
                int timeDelta = step - pte.getLastModified();
                if (timeDelta > leasedUsed)
                {
                    leasedUsed      = timeDelta;
                    leasedUsedIndex = index;
                }
                index++;
            }

            //save a copy of the frame being removed in next open space
            //TODO: if the frame being removed was in memory, do proper computations
            for (int x = 16; x < 64; x++)
            {
                if (entryList[x].isEmpty())
                {
                    //copy page info to new frame
                    PageTableEntry pte = entryList[x];
                    pte.setFrame(entryList[index].getFrame());
                    pte.setModified(step);
                    pte.setResident(false);
                    pte.setValid(false);
                    break;
                }
            }

            //overwrite the old valid page
            PageTableEntry npte = entryList[index];

            npte.setFrame(frameId);
            npte.setModified(step);
            pm.addPage(npte, owningProcess.getPid());
        }