Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Opt
- {
- int numberOfPages; //rozmiar pamięci wirtualnej - ilość stron
- Page[] pages;
- int numberOfFrames; //rozmiar pamięci fizycznej - ilość ramek
- int[] frames;
- int[] stringReferences; //ciąg odwołań
- public Opt(int numberOfPages, int numberOfFrames, int[] stringReferences)
- {
- this.numberOfPages = numberOfPages;
- this.numberOfFrames = numberOfFrames;
- this.stringReferences = stringReferences;
- this.pages = new Page[numberOfPages];
- for(int i = 0; i < pages.length; i++)
- pages[i] = new Page(i);
- this.frames = new int[numberOfFrames];
- }
- public void run()
- {
- int sumOfErrors = 0;
- int index = -1;
- int countPage = 0;
- for (int i=0; i<stringReferences.length; i++)
- {
- //pierwsze uzupełnienie tablicy ramek stronami
- if (countPage < frames.length && !pages[stringReferences[i]].isInFrames)
- {
- index++;
- frames[index] = stringReferences[i];
- pages[stringReferences[i]].isInFrames = true;
- sumOfErrors++;
- countPage++;
- }
- else if (!pages[stringReferences[i]].isInFrames)
- {
- int countFrames = 0;
- for (int j=i; j<stringReferences.length && countFrames < numberOfFrames; j++)
- {
- for(int k=0; k<numberOfFrames && countFrames < numberOfFrames; k++)
- {
- if (stringReferences[j] == frames[k])
- {
- pages[stringReferences[j]].counter++;
- }
- countFrames++;
- }
- }
- int maxCounter = 0;
- index = 0;
- for (int j = numberOfFrames-1; j>=0; j--)
- if(pages[frames[j]].counter>=maxCounter)
- {
- maxCounter = pages[frames[j]].counter;
- index = j;
- }
- for (int j = 0; j<numberOfFrames; j++)
- pages[frames[j]].counter = 0;
- pages[frames[index]].isInFrames = false;
- frames[index] = stringReferences[i]; //dodaję do ramki
- pages[stringReferences[i]].isInFrames = true; //zgłoszoną stronę
- sumOfErrors++;
- }
- }
- System.out.println("Ilość błędów braku stron dla algorytmu OPT: "+sumOfErrors);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement