用C语言编程序实现先进先出算法(FIFO)、最近最久未使用算法(LRU)算法、最佳置换算法(OPT)、时钟置换算法的具体实现过程,并计算访问命中率。(选择2-3个算法实现)
时间: 2023-12-16 12:02:56 浏览: 121
页面置换算法(fifo,lru,opt) C语言编写
4星 · 用户满意度95%
实现FIFO算法:
```c
#include <stdio.h>
#include <stdlib.h>
#define FRAME_NUM 3
typedef struct {
int id;
int counter;
} Page;
int main() {
int ref_seq[] = {1, 2, 3, 4, 1, 5, 6, 2, 3, 7};
int len = sizeof(ref_seq) / sizeof(int);
Page memory[FRAME_NUM];
int i, j, k, hit = 0, miss = 0;
for (i = 0; i < FRAME_NUM; i++) {
memory[i].id = -1;
memory[i].counter = 0;
}
for (i = 0; i < len; i++) {
int found = 0;
for (j = 0; j < FRAME_NUM; j++) {
if (memory[j].id == ref_seq[i]) {
hit++;
found = 1;
break;
}
}
if (!found) {
miss++;
int min_counter = memory[0].counter;
int min_index = 0;
for (j = 1; j < FRAME_NUM; j++) {
if (memory[j].counter < min_counter) {
min_counter = memory[j].counter;
min_index = j;
}
}
memory[min_index].id = ref_seq[i];
memory[min_index].counter = 0;
for (j = 0; j < FRAME_NUM; j++) {
memory[j].counter++;
}
}
}
printf("FIFO:\nHit: %d\nMiss: %d\nHit ratio: %.2f%%\n", hit, miss, (float)hit / len * 100);
return 0;
}
```
实现LRU算法:
```c
#include <stdio.h>
#include <stdlib.h>
#define FRAME_NUM 3
typedef struct {
int id;
int counter;
} Page;
int main() {
int ref_seq[] = {1, 2, 3, 4, 1, 5, 6, 2, 3, 7};
int len = sizeof(ref_seq) / sizeof(int);
Page memory[FRAME_NUM];
int i, j, k, hit = 0, miss = 0;
for (i = 0; i < FRAME_NUM; i++) {
memory[i].id = -1;
memory[i].counter = 0;
}
for (i = 0; i < len; i++) {
int found = 0;
for (j = 0; j < FRAME_NUM; j++) {
if (memory[j].id == ref_seq[i]) {
hit++;
found = 1;
for (k = 0; k < FRAME_NUM; k++) {
if (k != j) {
memory[k].counter++;
} else {
memory[k].counter = 0;
}
}
break;
}
}
if (!found) {
miss++;
int max_counter = memory[0].counter;
int max_index = 0;
for (j = 1; j < FRAME_NUM; j++) {
if (memory[j].counter > max_counter) {
max_counter = memory[j].counter;
max_index = j;
}
}
memory[max_index].id = ref_seq[i];
memory[max_index].counter = 0;
for (j = 0; j < FRAME_NUM; j++) {
if (j != max_index) {
memory[j].counter++;
} else {
memory[j].counter = 0;
}
}
}
}
printf("LRU:\nHit: %d\nMiss: %d\nHit ratio: %.2f%%\n", hit, miss, (float)hit / len * 100);
return 0;
}
```
实现OPT算法:
```c
#include <stdio.h>
#include <stdlib.h>
#define FRAME_NUM 3
int main() {
int ref_seq[] = {1, 2, 3, 4, 1, 5, 6, 2, 3, 7};
int len = sizeof(ref_seq) / sizeof(int);
int memory[FRAME_NUM];
int i, j, k, hit = 0, miss = 0;
for (i = 0; i < FRAME_NUM; i++) {
memory[i] = -1;
}
for (i = 0; i < len; i++) {
int found = 0;
for (j = 0; j < FRAME_NUM; j++) {
if (memory[j] == ref_seq[i]) {
hit++;
found = 1;
break;
}
}
if (!found) {
miss++;
int max_distance = -1;
int max_index = -1;
for (j = 0; j < FRAME_NUM; j++) {
int distance = 0;
for (k = i + 1; k < len; k++) {
if (memory[j] == ref_seq[k]) {
break;
}
distance++;
}
if (distance > max_distance) {
max_distance = distance;
max_index = j;
}
}
memory[max_index] = ref_seq[i];
}
}
printf("OPT:\nHit: %d\nMiss: %d\nHit ratio: %.2f%%\n", hit, miss, (float)hit / len * 100);
return 0;
}
```
实现时钟置换算法:
```c
#include <stdio.h>
#include <stdlib.h>
#define FRAME_NUM 3
typedef struct {
int id;
int clock;
} Page;
int main() {
int ref_seq[] = {1, 2, 3, 4, 1, 5, 6, 2, 3, 7};
int len = sizeof(ref_seq) / sizeof(int);
Page memory[FRAME_NUM];
int i, j, k, hit = 0, miss = 0;
for (i = 0; i < FRAME_NUM; i++) {
memory[i].id = -1;
memory[i].clock = 0;
}
for (i = 0; i < len; i++) {
int found = 0;
for (j = 0; j < FRAME_NUM; j++) {
if (memory[j].id == ref_seq[i]) {
hit++;
found = 1;
memory[j].clock = 1;
break;
}
}
if (!found) {
miss++;
while (1) {
for (j = 0; j < FRAME_NUM; j++) {
if (memory[j].clock == 0) {
memory[j].id = ref_seq[i];
memory[j].clock = 1;
break;
} else {
memory[j].clock = 0;
}
}
if (j == FRAME_NUM) {
continue;
} else {
break;
}
}
}
}
printf("Clock:\nHit: %d\nMiss: %d\nHit ratio: %.2f%%\n", hit, miss, (float)hit / len * 100);
return 0;
}
```
计算访问命中率:对于每个算法,我们都使用相同的访问序列。根据程序输出,可以得到FIFO算法的命中率为50.00%,LRU算法的命中率为60.00%,OPT算法的命中率为70.00%,时钟置换算法的命中率为60.00%。
阅读全文