1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
#include <iostream> #include <stdlib.h> using namespace std; void LFU_Agorithm(); double Page_Loss_Rate(int, int); int Find_Exist(int*, int, int); int Find_LeastInteviewTime(int, int, int*, int); void Update_InHereTime(int*, int, int); int Find_LeastNotUseTime(int, int, int*); int Find_LeastNotInterviewTime(int, int*); void Print_Frame(int*, int); void Print_Menu();
int main() {
LFU_Agorithm(); return 0; }
int Find_Exist(int* save_Frame, int n, int addr) { for (int i = 0; i < n; i++) { if (save_Frame[i] == addr) { return i; } } return -1; }
void Print_Menu() { cout << "+---------------------------------------+" << endl; cout << "|\t***算法清单***\t\t\t|" << endl; cout << "|\t1.最佳置换算法(OPT)\t\t|" << endl << "|\t2.先进先出算法(FIFO)\t\t|" << endl; cout << "|\t3.最近最久未使用算法(LRU)\t|" << endl << "|\t4.最不经常使用算法(LFU)\t\t|" << endl; cout << "|\t0.退出\t\t\t\t|" << endl; cout << "+---------------------------------------+" << endl; }
void Print_Frame(int* save_Frame, int n) { cout << "\t"; for (int i = 0; i < n; i++) { if (i == 0) { if (save_Frame[i] == -999) cout << "/ /"; else cout << "/" << save_Frame[i] << "/"; } else { if (save_Frame[i] == -999) cout << " /"; else cout << save_Frame[i] << "/"; } } cout << endl; } void Init(int* n, int* len, int*& save_Frame, int*& interview_Array) { cout << "请输入页帧数 n :"; cin >> *n; save_Frame = new int[*n]; for (int i = 0; i < *n; i++) save_Frame[i] = -999;
cout << "请输入引用页数:"; cin >> *len; cout << "请输入引用串:"; interview_Array = new int[*len]; for (int i = 0; i < *len; i++) cin >> interview_Array[i]; }
double Page_Loss_Rate(int S, int F) { double ans = 1.0 * F / (1.0 * S + 1.0 * F) * 100; return ans; }
int Find_LeastInteviewTime(int sta, int addr, int* interview_Array, int len) { for (int i = sta; i < len; i++) { if (interview_Array[i] == addr) { return i - sta; } } return 99999; }
void Update_InHereTime(int* in_HereTime, int n, int ind) { for (int i = 0; i < n; i++) { in_HereTime[i]++; } if (ind != -1) in_HereTime[ind] = 0; }
int Find_LeastNotUseTime(int end, int addr, int* interview_Array) { for (int i = end - 1; i >= 0; i--) { if (interview_Array[i] == addr) { return end - i; } } return 99999; }
int Find_LeastNotInterviewTime(int n, int* interview_Time) { int min_Time = 99999; int ind; for (int i = 0; i < n; i++) { if (interview_Time[i] < min_Time) { min_Time = interview_Time[i]; ind = i; } } return ind; }
void LFU_Agorithm() { int n, len, * save_Frame = NULL, * interview_Array = NULL; Init(&n, &len, save_Frame, interview_Array);
int* interview_Time = new int[n]; for (int i = 0; i < n; i++) interview_Time[i] = 0;
int addr; int cnt = 0; int score = 0; int fail_time = 0; int iter = 0; while (iter < len) { cout << endl << "第" << iter << "轮:"; addr = interview_Array[iter]; iter++; if (cnt < n) { if (Find_Exist(save_Frame, cnt, addr) != -1) { score++; cout << "\"" << addr << "\" 被命中了\t\t------->"; Print_Frame(save_Frame, n); int ind = Find_Exist(save_Frame, cnt, addr); interview_Time[ind]++; } else { fail_time++; cout << "未命中," << "\"" << addr << "\" 被装入 \t------->"; save_Frame[cnt] = addr; Print_Frame(save_Frame, n); cnt++; } } else { if (Find_Exist(save_Frame, n, addr) != -1) { score++; cout << "\"" << addr << "\" 被命中了\t\t------->"; Print_Frame(save_Frame, n); int ind = Find_Exist(save_Frame, n, addr); interview_Time[ind]++; } else { fail_time++; int index = Find_LeastNotInterviewTime(n, interview_Time); cout << "\"" << addr << "\" 替换了 \"" << save_Frame[index] << "\"\t\t------->"; save_Frame[index] = addr; interview_Time[index] = 0; Print_Frame(save_Frame, n); } } } cout << endl; cout << "缺页次数为:" << fail_time << endl; cout << "缺页中断率 R = " << Page_Loss_Rate(score, fail_time) << "%" << endl; delete[] save_Frame; delete[] interview_Array; delete[] interview_Time; }
|