LRU算法

//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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值