//LRU algorithm
//author:chillyCreator
//in the page_information, the bigger number means the newer page.
//if the number in the page_information is -1, it means no page is in this position.
#include<iostream>
using namespace std;
bool isNotIn(int p_page_num);
bool isNotFull(int& position);
void insert(int p_page_num,int position);
int delOldPage();
const int MAX_INT = 32767;
int process_num;//the total number of process pages
int page_size;//the total number of pages
int page_num=0;//the number of page which in the page array
int miss=0;//
int* page_information;//the page information array
int* page;//the page array
int old=0;//how old is a page?
int main()
{
cout << "how many pages of processes ?"<<endl;
cin >> process_num ;
cout << "how many pages in the memory?"<<endl;
cin >> page_size;
page_information = new int[page_size];
page = new int[page_size];
int i;
for(i=0; i<page_size; i++)
{
page[i] = -1;
page_information[i] = -1;
}
int user_test;
cout << "input 1 is user test, 2 is auto test"<<endl;
cin >> user_test;
switch(user_test)
{
case 1:
for(i=0; i < process_num; i++)
{
int process_page_num;
cin >> process_page_num;
cout <<"input process page num is :" <<process_page_num<<endl;
if(isNotIn(process_page_num))
{
miss++;
int temp_position;
if(isNotFull(temp_position))
{
insert(process_page_num,temp_position);
page_num++;
}
else
{
temp_position = delOldPage();
insert(process_page_num,temp_position);
}
}
}
break;
default:
for(i=0; i < process_num; i++)
{
int process_page_num = rand();
cout << process_page_num<<endl;
if(isNotIn(process_page_num))
{
miss++;
int temp_position;
if(isNotFull(temp_position))
{
insert(process_page_num,temp_position);
page_num++;
}
else
{
temp_position = delOldPage();
insert(process_page_num,temp_position);
}
}
}
break;
}
cout <<"miss is " <<miss << "/t the total process num is "<<process_num<<endl;
cout << endl;
cout <<"the miss percentage is miss/process_num = "<<double(miss)/process_num<<endl;
return 0;
}
//the process page is in the page array?;
bool isNotIn(int p_page_num)
{
for(int i=0; i<page_size; i++)
{
if(page[i]==p_page_num)
return false;
}
return true;
}
//the page array is full or not.
//and return the empty position
bool isNotFull(int& position)
{
if(page_size <= page_num)
return false;
int i;
for(i=0; i<page_size; i++)
{
if(page_information[i]==-1)
break;
}
position = i;
return true;
}
//insert a process page to the page in an empty position.
//and the function protect the primite: old from flowover error.
void insert(int p_page_num,int position)
{
old++;
if(old==MAX_INT)
{
old = 1;
for(int i=0; i<page_size; i++)
{
if(page_information[i]!=-1)
page_information[i] = old;
}
}
old++;
page[position] = p_page_num;
page_information[position] = old;
}
int delOldPage()
{
int i;
int min=page_information[0],min_i=0;
for(i=1; i<page_size; i++)
{
if(min > page_information[i])
{
min = page_information[i];
min_i = i;
}
}
return min_i;
}
分享到:
相关推荐
LRU算法LRU算法LRU算法LRU算法
LRU算法实现LRU算法实现LRU算法实现LRU算法实现LRU算法实现
使用c语言是实现的LRU算法,带测试用例,供大家学习参考使用
这个是关于lru算法实验报告,大家欢迎使用
该资源是Java实现LRU算法的相关代码,将页面序号和进程分配的模块数,运行出具体的变化过程,真实可靠,可实现。
1.资源包含LRU算法整个项目,可直接在vs2019上运行项目,如果版本不对可选择把项目中cpp文件复制到自己的vs中运行 2.LRU 算法的设计原则是:如果一个数据在最近一段时间没有被访问到,那么在将来它被访问的可能性也...
编写程序实现LRU算法及其近似算法,并分析各算法的时间复杂度、空间复杂度和实现难度;通过随机生成页面访问序列,测试所实现算法的页错误率,并加以比较和分析。 此资源含完整代码和完整实验报告(加上你的学号姓名...
LRU算法 1.cpp 1.exe LRU FIFO REPORTMENT
基于C语言的FIFO和LRU算法的实现。
操作系统中的一个算法 C++实现的,Lru算法
用C语言实现的OPT和LRU算法,下载后直接用VC++6.0打开即可编译运行。亲测可用。
LRU算法 1. LRU算法的实现 2. 模拟进程调度算法的实现 3. 通过编写程序实现LRU算法及模拟进程调度程序,并调试运行出来。加深对以上两种算法的理解,培养动手实践的能力。,进一步熟悉LRU算法和进程调度的过程. 进程...
LRU 算法(c语言简单的实现)利用队列等形式!
LRU算法和FIFO算法合体
近期最少使用算法
根据这个LRU算法的基本原理设计一段程序来模拟实现LRU算法对页面进行置换的过程与结果。程序运行时首先输入页的架数,然后根据架数输入访问序列,然后将每一步的结果打印出来。
LRU算法的java实现
操作系统之LRU算法(java)(csdn)————程序
lru算法操作系统实验lru算法c?c++shiyan
模拟页式虚拟存储管理中硬件地址变换和缺页中断,并用LRU算法处理缺页中断