project environment
Development software: Visual Studio 2019
Programming language: C
Project source code
#include <stdio.h>
#include <stdlib.h>
#define N 50
#define M 10
int PageList[N]; //存放页面访问序列
int BlockList[M]; //当前内存块存放页面
int BlockPage[M][N]; //内存块每次置换后相应存放的序列
char MissingPage[N]; //记录每次置换后的缺页标志状态
int DieOut[N]; //记录淘汰页面
char flag; //缺页标志
int n; //页面访问序列
int m; //内存块数
int count; //缺页次数
//初始化
void Init()
{
int i;
printf("请输入内存的块数:");
scanf("%d", &m);
printf("请输入页面访问序列的长度:");
scanf("%d", &n);
printf("请输入页面访问序列:");
for (i = 0; i < n; i++)
scanf("%d", &PageList[i]);
}
//输出结果
void ShowPrintf()
{
int i, j;
printf("\n结果为下表(*代表有缺页):\n");
printf("---------------------------------------------------------------------------------------\n");
printf("页面序列:");
for (i = 0; i < n; i++)
printf("%3d", PageList[i]);
printf("\n");
printf("---------------------------------------------------------------------------------------\n");
for (i = 0; i < m; i++)//每块内的页面变化
{
printf(" %d号块:", i+1);
for (j = 0; j < n; j++)
printf("%3d", BlockPage[i][j]);
printf("\n");
}
printf("---------------------------------------------------------------------------------------\n");
printf("缺页情况:");
for (i = 0; i < n; i++)
printf("%3c", MissingPage[i]);
printf("\n");
printf("---------------------------------------------------------------------------------------\n");
printf("淘汰页面:");
for (i = 0; i < n; i++) {
if (DieOut[i] == -1 && i <= 2) {
printf("%3d", DieOut[i]);
}
if(DieOut[i] != -1 && i > 2){
printf("%3d", DieOut[i]);
}
if (DieOut[i] == -1 && i > 2)
{
printf("%3s", "无");
}
}
printf("\n");
printf("---------------------------------------------------------------------------------------\n");
printf("\n发生缺页的次数=%d\n", count);
printf("\n缺页中断率=%.2f%%\n", (float)count / n * 100);
}
void FIFO(){
int i, j, q, p;
count = 0; //记录缺页次数
for (i = 0;i < n;i++) {
MissingPage[i] = 0 ;
DieOut[i] = -1;
}
for (i = 0;i < m;i++) {
BlockList[i] = -1 ;
}
for (i = 0; i < n; i++)
{
q = 0;
while ((PageList[i] != BlockList[q]) && (q != m))
{
q++;
}
if (q == m)
{
flag = '*';
count++;
}
else
flag = ' ';
if (flag == '*')
{
DieOut[i] = BlockList[m - 1]; //记录被淘汰的页面
for (j = m - 1; j > 0; j--) {
BlockList[j] = BlockList[j - 1]; //后移
}
BlockList[0] = PageList[i]; //新进入的页面在第一块
}
for (j = 0; j < m; j++) {
BlockPage[j][i] = BlockList[j];
}
MissingPage[i] = flag;
}
ShowPrintf();
}
void LRU() {
int i, j;
count = 0; //缺页次数初始化
int count_page = 0; //置换页面次数
int time[N]; //各个物理块最近一次访问至现在的时间
int DisplacedPages[N];
for (i = 0;i < n;i++) {
MissingPage[i] = 0;
DieOut[i] = -1;
DisplacedPages[i] = -1;
}
for (i = 0;i < m;i++) {
BlockList[i] = -1;
}
for (i = 0;i < m;i++) {
time[i] = 0;
}
for (i = 0;i < n; i++)
{
for (j = 0; j < m; j++) //更新时间记录
if (BlockList[j] != 0)
time[j]++;
for (j = 0; j < m; j++)
if (BlockList[j] == PageList[i]) //命中
{
time[j] = 0;
break;
}
else if (BlockList[j] == -1) //未命中但块中为空
break;
if (j < m && BlockList[j] == -1)//未命中且物理块未满,直接存入
{
BlockList[j] = PageList[i];
time[j] = 0;
flag = '*';
count++;
}
else if (j == m)//需要替换
{
int max = 0;
for (int k = 0;k < m;k++)//寻找最久未访问的地址所在的物理块的位置
{
if (time[max] < time[k])
max = k;
}
DisplacedPages[count_page] = BlockList[max];
count_page++;
count++;
flag = '*';
BlockList[max] = PageList[i];
time[max] = 0;
}
else {
flag = ' ';
}
if (j == m) {
DieOut[i] = DisplacedPages[count_page - 1];
}
for (j = 0; j < m; j++) {
BlockPage[j][i] = BlockList[j];
}
MissingPage[i] = flag; //记录缺页
}
ShowPrintf();
}
void main()
{
int choice;
bool flag = true;
while (flag) {
printf("\n");
printf("----------------------页面置换算法-------------------\n");
printf(" (1)输入数据 \n");
printf(" (2)先进先出算法(FIFO) \n");
printf(" (3)最近最久未使用算法(LRU) \n");
printf(" (0)退出 \n");
printf("-----------------------------------------------------\n");
printf("请选择:");
scanf("%d", &choice);
switch (choice)
{
case 1:
Init();break;
case 2:
if (PageList[0] != NULL) {
FIFO();
break;
}
else
{
printf("请先录入数据!");
system("pause");
system("cls");
break;
}
case 3:
if (PageList[0] != NULL) {
LRU();
break;
}
else
{
printf("请先录入数据!");
system("pause");
system("cls");
break;
}
case 0:
flag = false;
break;
default:
printf("请输入正确的指令");
}
}
}
result
![](https://img-blog.csdnimg.cn/img_convert/3215f6ac693145bf9f9dd09e59fbfb75.png)
![](https://img-blog.csdnimg.cn/img_convert/24f620171fe644b5adc60f4b1e2cd74f.png)