Advertisement
Guest User

Untitled

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