-
Notifications
You must be signed in to change notification settings - Fork 0
/
task3.cpp
280 lines (258 loc) · 5.34 KB
/
task3.cpp
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include<stdlib.h>
#include<iostream>
#include<time.h>
#include<stdio.h>
using namespace std;
#define total_instruction 100 /*指令流长*/
#define M 100 /*实际页数*/
#define N 5 //可用页面数
struct Pro
{
int num, time;
};
int a[total_instruction];
int page[N];
void Input(Pro p[total_instruction])
{
int m, i, m1, m2;
srand((unsigned int)time(NULL));
m = rand() % 400; //
for (i = 0; i<total_instruction;) /*产生指令队列*/
{
if (m<0 || m>399)
{
printf("When i==%d,Error,m==%d\n", i, m);
exit(0);
}
a[i] = m; /*任选一指令访问点m*/
a[i + 1] = a[i] + 1;
a[i + 2] = a[i] + 2; /*顺序执行两条指令*/
int m1 = rand() % m; /*执行前地址指令m1 */
a[i + 3] = m1;
a[i + 4] = m1 + 1;
a[i + 5] = m1 + 2;/*顺序执行两条指令*/
// s=(158-a[i+5])*rand( )/32767/32767/2+a[i+5]+2;
m2 = rand() % (157 - m1) + m1 + 3;
a[i + 6] = m2;
if ((m2 + 2) > 159)
{
a[i + 7] = m2 + 1;
i += 8;
}
else
{
a[i + 7] = m2 + 1;
a[i + 8] = m2 + 2;
i = i + 9;
}
m = rand() % m2;
}
for (i = 0; i<total_instruction; i++) /*将指令序列变换成页地址流*/
{
p[i].num = a[i] / 10;
p[i].time = 0;
}
}
void print(Pro *page1)//打印当前的页面
{
Pro *page = new Pro[N];
page = page1;
for (int i = 0; i<N; i++)
printf("%-4d", page[i].num);
//cout<<page[i].num<<" ";
cout << endl;
//free(page);
}
int Search(int e, Pro *page1)
{
Pro *page = new Pro[N];
page = page1;
for (int i = 0; i<N; i++)if (e == page[i].num)return i;
return -1;
}
int Searchtime(int e, Pro *page1)
{
Pro *page = new Pro[N];
page = page1;
for (int i = 0; i<N; i++)if (e == page[i].time)return i;
return -1;
}
int Max(Pro *page1)
{
Pro *page = new Pro[N];
page = page1;
int e = page[0].time, i = 0;
while (i<N)//找出离现在时间最长的页面
{
if (e<page[i].time)e = page[i].time;
i++;
}
for (i = 0; i<N; i++)if (e == page[i].time)return i;
return -1;
}
int Compfu(Pro *page1, int i, int t, Pro p[M])
{
Pro *page = new Pro[N];
page = page1;
int count = 0;
for (int j = i; j<M; j++)
{
if (page[t].num == p[j].num)break;
else count++;
}
return count;
}
int main()
{
Pro p[total_instruction];
Pro *page = new Pro[N];
char c;
int t = 0, i;
float n = 0;
Input(p);
do {
for (i = 0; i<N; i++)//初试化页面基本情况
{
page[i].num = -1;
page[i].time = 2 - i;
}
printf("系统产生的随机数为:");
for (int e = 0; e<M; e++)
cout << p[e].num << " ";
cout << endl;
i = 0;
cout << "f:FIFO页面置换" << endl;
cout << "l:LRU页面置换" << endl;
cout << "o:OPT页面置换" << endl;
cout << "n:NUR页面置换" << endl;
cout << "按其它键结束" << endl;
cin >> c;
if (c == 'f')//FIFO页面置换
{
n = 0;
cout << "页面置换情况: " << endl;
while (i< total_instruction)
{
if (Search(p[i].num, page) >= 0)
i++;//找到相同的页面
else
{
if (t == N)t = 0;
else
{
n++;//
page[t].num = p[i].num;
print(page);
t++;
}
}
}
cout << "缺页次数:" << n << " 命中率:" << 1 - n / total_instruction << endl;
}
if (c == 'n')//NUR页面置换
{
n = 0;
cout << "设CLEAR_PERIOD为5" << endl;
cout << "页面置换情况: " << endl;
int period = 0;
int time_set;
while (i < total_instruction)
{
if (period % 10 == 0) {
for (int q = 0; q < N; q++)
page[q].time = 0;
}
t = Search(p[i].num, page);
if (t >= 0) {
page[t].time = 1;
}
else {
time_set = Searchtime(0, page);
if (time_set == -1){
page[0].num = p[i].num;
n++;
}
else
{
page[time_set].num = p[i].num;
page[time_set].time = 1;
n++;
}
}
print(page);
i++;
period++;
}
cout << "缺页次数:" << n << " 命中率:" << 1 - n / total_instruction << endl;
}
if (c == 'l')//LRU页面置换
{
n = 0;
cout << "页面置换情况: " << endl;
while (i<total_instruction)
{
int k;
k = t = Search(p[i].num, page);
if (t >= 0)
page[t].time = 0;
else
{
n++;
t = Max(page);
page[t].num = p[i].num;
page[t].time = 0;
}
for (int j = 0; j<N; j++)
{
if (j != t)page[j].time++;
}
/*if(t==0){page[t+1].time++;page[t+2].time++;}
if(t==1){page[2].time++;page[0].time++;}
if(t==2){page[1].time++;page[0].time++;}*/
if (k == -1) print(page);
i++;
}
cout << "缺页次数:" << n << " 命中率:" << 1 - n / total_instruction << endl;
}
if (c == 'o')//OPT页面置换
{
n = 0;
while (i<total_instruction)
{
if (Search(p[i].num, page) >= 0)i++;
else
{
if (page[N - 1].num == -1)
{
for (int g = 0; g<N; g++)
if (page[g].num == -1)
{
page[g].num = p[i].num;
i++;
n++;
print(page);
break;
}
}
else {
int temp = -1, cn;
for (t = 0; t<N; t++)
{
if (temp<Compfu(page, i, t, p))
{
temp = Compfu(page, i, t, p);
cn = t;
}
}
page[cn] = p[i];
n++;
print(page);
i++;
}
}
}
cout << "缺页次数:" << n << " 命中率:" << 1 - n / total_instruction << endl;
}
} while (c == 'f' || c == 'l' || c == 'o'|| c == 'n');
return 0;
}