Advertisement
Guest User

Untitled

a guest
May 4th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. public class Lru
  2. {
  3. int numberOfPages; //rozmiar pamięci wirtualnej - ilość stron
  4. int[] pages;
  5. int numberOfFrames; //rozmiar pamięci fizycznej - ilość ramek
  6. int[][] frames;
  7. int[] stringReferences; //ciąg odwołań
  8.  
  9. public Lru(int numberOfPages, int numberOfFrames, int[] stringReferences)
  10. {
  11. this.numberOfPages = numberOfPages;
  12. this.numberOfFrames = numberOfFrames;
  13. this.stringReferences = stringReferences;
  14. this.pages = new int[numberOfPages];
  15. for(int i = 0; i < pages.length; i++)
  16. pages[i] = -1;
  17. this.frames = new int[numberOfFrames][2];
  18. for (int i=0; i<frames.length; i++)
  19. frames[i][0] = -1;
  20. }
  21.  
  22. public void run()
  23. {
  24. int sumOfErrors = 0;
  25. int index = 0;
  26. for (int i=0; i<stringReferences.length; i++)
  27. {
  28. if (frames[numberOfFrames-1][0] == -1 && pages[stringReferences[i]] == -1)
  29. {
  30. sumOfErrors++;
  31. frames[index][0] = stringReferences[i];
  32. frames[index][1] = i;
  33. pages[stringReferences[i]] = index;
  34. index++;
  35. }
  36. else
  37. {
  38. if (pages[stringReferences[i]] == -1)
  39. {
  40. sumOfErrors++;
  41. index = 0;
  42. int lru = frames[0][1];
  43. for(int j=1; j<numberOfFrames; j++)
  44. {
  45. if (frames[j][1] < lru)
  46. {
  47. lru = frames[j][1];
  48. index = j;
  49. }
  50. }
  51. pages[frames[index][0]] = -1;
  52. frames[index][1] = i;
  53. frames[index][0] = stringReferences[i];
  54. pages[stringReferences[i]] = index;
  55. }
  56. else
  57. {
  58. frames[pages[stringReferences[i]]][1] = i;
  59. }
  60. }
  61. }
  62. System.out.println("Ilość błędów braku stron dla algorytmu LRU: "+sumOfErrors);
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement