Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Lru
- {
- int numberOfPages; //rozmiar pamięci wirtualnej - ilość stron
- int[] pages;
- int numberOfFrames; //rozmiar pamięci fizycznej - ilość ramek
- int[][] frames;
- int[] stringReferences; //ciąg odwołań
- public Lru(int numberOfPages, int numberOfFrames, int[] stringReferences)
- {
- this.numberOfPages = numberOfPages;
- this.numberOfFrames = numberOfFrames;
- this.stringReferences = stringReferences;
- this.pages = new int[numberOfPages];
- for(int i = 0; i < pages.length; i++)
- pages[i] = -1;
- this.frames = new int[numberOfFrames][2];
- for (int i=0; i<frames.length; i++)
- frames[i][0] = -1;
- }
- public void run()
- {
- int sumOfErrors = 0;
- int index = 0;
- for (int i=0; i<stringReferences.length; i++)
- {
- if (frames[numberOfFrames-1][0] == -1 && pages[stringReferences[i]] == -1)
- {
- sumOfErrors++;
- frames[index][0] = stringReferences[i];
- frames[index][1] = i;
- pages[stringReferences[i]] = index;
- index++;
- }
- else
- {
- if (pages[stringReferences[i]] == -1)
- {
- sumOfErrors++;
- index = 0;
- int lru = frames[0][1];
- for(int j=1; j<numberOfFrames; j++)
- {
- if (frames[j][1] < lru)
- {
- lru = frames[j][1];
- index = j;
- }
- }
- pages[frames[index][0]] = -1;
- frames[index][1] = i;
- frames[index][0] = stringReferences[i];
- pages[stringReferences[i]] = index;
- }
- else
- {
- frames[pages[stringReferences[i]]][1] = i;
- }
- }
- }
- System.out.println("Ilość błędów braku stron dla algorytmu LRU: "+sumOfErrors);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement