三种页面置换算法的C++模拟

Posted yuxi-blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了三种页面置换算法的C++模拟相关的知识,希望对你有一定的参考价值。

  1 #include<iostream>
  2 using namespace std;
  3 int page[] = { 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1,-1 };
  4 void FIFO();
  5 void OPT();
  6 void RLU();
  7 bool inArray(int* a, int n, int p);
  8 int main(void) {
  9     FIFO();
 10     OPT();
 11     RLU();
 12     system("pause");
 13 }
 14 void FIFO() {
 15     int temp[3] = { -1,-1,-1 };
 16     int time[3] = { 0,0,0 };
 17     int num = 0;
 18     int error = 0;
 19     cout << "FIFO:" << endl;
 20     while (page[num] != -1) {
 21         if (inArray(temp, 3, page[num])) {
 22             std::cout << page[num] << ,;
 23             std::cout << endl;
 24         }
 25         else {
 26             error++;
 27             bool hasChanged = false;
 28             for (int i = 0; i < 3; i++) {
 29                 if (time[i] == 0 && hasChanged == false) {
 30                     time[i] = 2;
 31                     temp[i] = page[num];
 32                     hasChanged = true;
 33                 }
 34                 if (time[i] != 0) {
 35                     time[i]--;
 36                 }
 37             }
 38             std::cout << page[num] << , <<  ;
 39             for (size_t i = 0; i < 3; i++) {
 40                 if (temp[i] == -1) {
 41                     std::cout << * <<  ;
 42                 }
 43                 else {
 44                     std::cout << temp[i] <<  ;
 45                 }
 46             }
 47             std::cout << endl;
 48         }
 49         num++;
 50     }
 51     cout << "错误率:" << error << endl;
 52 }
 53 bool inArray(int* a, int n, int p) {
 54     for (int i = 0; i < n; i++) {
 55         if (p == a[i]) {
 56             return true;
 57         }
 58     }
 59     return false;
 60 }
 61 void OPT() {
 62     int temp[3] = { -1,-1,-1 };
 63     int num = 0;
 64     int error = 0;
 65     //OPT已知未来的页数为20
 66     cout << "OPT:" << endl;
 67     while (page[num] != -1) {
 68         int a = page[num];
 69         if (inArray(temp, 3, page[num])) {
 70             std::cout << page[num] << ,;
 71             std::cout << endl;
 72         }
 73         else {
 74             error++;
 75             bool fuck = false;
 76             for (size_t i = 0; i < 3; i++){
 77                 if (temp[i] == -1) {
 78                     temp[i] = page[num];
 79                     fuck = true;
 80                     break;
 81                 }
 82             }
 83             
 84             if (fuck == false) {
 85                 int distance[3] = { 20,20,20 };
 86                 for (int i = 19; i >= num; i--) {
 87                     for (int j = 0; j < 3; j++) {
 88                         if (temp[j] == page[i] && (i - num) < distance[j]) {
 89                             distance[j] = i - num;
 90                         }
 91                     }
 92                 }
 93                 int k = 0;
 94                     int max = -1;
 95                 for (size_t i = 0; i < 3; i++) {
 96                     if (max < distance[i]) {
 97                         max = distance[i];
 98                         k = i;
 99                     }
100                 }
101                 temp[k] = page[num];
102             }
103             std::cout << page[num] << , <<  ;
104             for (size_t i = 0; i < 3; i++) {
105                 if (temp[i] == -1) {
106                     std::cout << * <<  ;
107                 }
108                 else {
109                     std::cout << temp[i] <<  ;
110                 }
111             }
112             std::cout << endl;
113         }
114         num++;
115     }
116     cout << "错误率:" << error << endl;
117 }
118 void RLU(){
119     int temp[3] = { -1,-1,-1 };
120     int time[3] = { -1,-1,-1 };
121     int num = 0;
122     int error = 0;
123     cout << "RLU:" << endl;
124     while (page[num] != -1) {
125         int a = page[num];
126         if (inArray(temp, 3, page[num])) {
127             std::cout << page[num] << ,;
128             std::cout << endl;
129             //bool Changed = false;
130             for (int i = 0; i < 3; i++) {
131                 if (temp[i] == page[num]) {
132                     time[i] = 2;
133                     //Changed = true;
134                 }
135                 if (temp[i] != page[num]&&time[i]!=0) {
136                     time[i]--;
137                 }                
138             }
139         }
140         else {
141             error++;
142             //bool hasChange = false;
143             for (size_t i = 0; i < 3; i++){
144                 if (temp[i] == -1) {
145                     temp[i] = page[num];
146                     time[i] = 2;
147                     break;
148                 }
149                 if(time[i] == 0) {
150                     temp[i] = page[num];
151                     time[i] = 2;
152                 }
153                 else {
154                     time[i]--;
155                 }
156             }
157             std::cout << page[num] << , <<  ;
158             for (size_t i = 0; i < 3; i++) {
159                 if (temp[i] == -1) {
160                     std::cout << * <<  ;
161                 }
162                 else {
163                     std::cout << temp[i] <<  ;
164                 }
165             }
166             std::cout << endl;
167         }
168         num++;
169     }
170     cout << "错误率:" << error << endl;
171 }

 

以上是关于三种页面置换算法的C++模拟的主要内容,如果未能解决你的问题,请参考以下文章

操作系统概念 页面置换算法:分别使用FIFOOPTLRU三种置换算法来模拟页面置换的过程。

C#窗体模拟三种页面置换算法(OPT,FIFO,LRU)

C++编程,clock置换算法

先进先出算法(FIFO)——页面置换

LRU和FIFO页面置换算法模拟实战

操作系统课程设计