当前位置:   article > 正文

FIFO、LRU和最优页面置换算法的C++实现_c语言编写fifo算法和lru算法

c语言编写fifo算法和lru算法
  1. #include<iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5. int arr[10];
  6. void getrandom();
  7. int FIFO(int length);
  8. int OPA(int length);
  9. int LRU(int length);
  10. int main()
  11. {
  12. getrandom();
  13. for (int i = 0; i < 10; i++)
  14. {
  15. cout << arr[i]<<' ';
  16. }
  17. cout << endl;
  18. int n;
  19. cin >> n;
  20. cout << FIFO(n) << endl;
  21. cout << OPA(n) << endl;
  22. cout << LRU(n) << endl;
  23. return 0;
  24. }
  25. void getrandom()
  26. {
  27. for (int i = 0; i < 10; i++)
  28. {
  29. arr[i]= rand() % 9 + 1;
  30. }
  31. }
  32. int FIFO(int length)
  33. {
  34. int* page;
  35. int* sequence;
  36. page = new int[length];
  37. sequence = new int[length];
  38. for (int i = 0; i < length; i++)
  39. {
  40. page[i] = 10;
  41. sequence[i] = 0;
  42. }
  43. int j = 0;
  44. int num = 0;
  45. while (j < 10)
  46. {
  47. int flag = 0;
  48. for (int i = 0; i < length; i++)
  49. {
  50. if (page[i] == arr[j])
  51. {
  52. flag++;
  53. break;
  54. }
  55. }
  56. if (flag == 0)
  57. {
  58. int fl = 0;
  59. int first = sequence[0];
  60. for (int i = 0; i < length; i++)
  61. {
  62. if (sequence[i] < first)
  63. {
  64. first = sequence[i];
  65. fl = i;
  66. }
  67. }
  68. page[fl] = arr[j];
  69. sequence[fl]++;
  70. num++;
  71. }
  72. j++;
  73. }
  74. return num;
  75. }
  76. int OPA(int length)
  77. {
  78. int* page;
  79. page = new int[length];
  80. for (int i = 0; i < length; i++)
  81. {
  82. page[i] = 10;
  83. }
  84. int* note;
  85. note = new int[length];
  86. for (int i = 0; i < length; i++)
  87. {
  88. note[i] = 0;
  89. }
  90. int num=0;
  91. int j = 0;
  92. while (j < 10)
  93. {
  94. int flag = 0;
  95. for (int i = 0; i < length; i++)
  96. {
  97. if (page[i] == arr[j])
  98. {
  99. flag++;
  100. break;
  101. }
  102. }
  103. if (flag == 0)
  104. {
  105. for (int i = 0; i < length; i++)
  106. {
  107. for (int t = j; t < 10; t++)
  108. {
  109. if (page[i] == arr[t])
  110. {
  111. note[i] = t;
  112. break;
  113. }
  114. }
  115. }
  116. int fl = 0;
  117. for (int i = 0; i < length; i++)
  118. {
  119. if (note[i] == 0)
  120. {
  121. fl++;
  122. page[i] = arr[j];
  123. num++;
  124. break;
  125. }
  126. }
  127. if (fl == 0)
  128. {
  129. int fg = note[0];
  130. int a = 0;
  131. for (int i = 0; i < length; i++)
  132. {
  133. if (note[i] > note[0])
  134. {
  135. fg = note[i];
  136. a = i;
  137. }
  138. }
  139. page[a] = arr[j];
  140. num++;
  141. }
  142. }
  143. j++;
  144. }
  145. return num;
  146. }
  147. int LRU(int length)
  148. {
  149. int* page;
  150. page = new int[length];
  151. int* count;
  152. count = new int[length];
  153. for (int i = 0; i < length; i++)
  154. {
  155. page[i] = 10;
  156. count[i] = 0;
  157. }
  158. int num = 0;
  159. int j = 0;
  160. while (j < 10)
  161. {
  162. int flag = 0;
  163. for (int i = 0; i < length; i++)
  164. {
  165. if (page[i] == arr[j])
  166. {
  167. flag++;
  168. break;
  169. }
  170. }
  171. if (flag == 0)
  172. {
  173. int min = count[0];
  174. int fl = 0;
  175. for (int i = 0; i < length; i++)
  176. {
  177. if (count[i] < min)
  178. {
  179. min = count[i];
  180. fl = i;
  181. }
  182. }
  183. page[fl] = arr[j];
  184. count[fl] = j;
  185. num++;
  186. }
  187. j++;
  188. }
  189. return num;
  190. }

这只是最简单的实现,不一定完全正确,有误之处,多多指教

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/163650
推荐阅读
相关标签
  

闽ICP备14008679号