📄 lru.cpp
字号:
// LRU.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream.h>
void main()
{
int a[10],b[10],c[10];
int i,t,k,j;
int n,m;
cout<<"请输入内存容量m:";
cin>>m;
for(i=0;i<m;i++)
{
a[i]=-1; //-1 表示内存中无页面,0表示初始化
b[i]=0;
}
cout<<"请输入要访问的页面顺序:"<<endl;
for(i=0;i<10;i++)
{
cin>>n;
c[i]=n;
}
i=0;
while(i<10)
{
for(k=0;k<m;k++)
{
if(a[k]==c[i])
{
cout<<"内存中有这个页面,直接访问!"<<endl;
b[k]=1;
break; //内存中有这个页面,直接访问
}
}
if(k==m) //内存中没有要访问的页面,如果内存没满则直接进入
{
if(a[m-1]<0)
{
cout<<"内存未满, ";
for(k=0;k<m;k++)
{
if(a[k]<0)
break;
}
a[k]=c[i];
b[k]=1;
cout<<c[i]<<"进入内存"<<endl;
}
else //内存已满,将最久没有使用的进程置换出来
{
int max=0;
for(j=1;j<m;j++)
{
if(b[max]<b[j])
max=j;
}
cout<<c[i]<<"进入内存, "<<a[max]<<"被置换出内存"<<endl;
a[max]=c[i];
b[max]=1;
}
}
i++;
} // while
} //main
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -