最近最久未使用

最近最久未使用(LRU)的页面置换算法是根据页面调入内存后的使用情况做出决策的。由于无法预测各页面将来的使用情况,只能利用“最近的过去”作为“最近的将来”的近似,因此,LRU置换算法是选择最近最久未使用的页面予以淘汰。该算法赋予每个页面一个访问字段,用来记录一个页面自上次被访问以来所经历的时间t。当需要淘汰一个页面时,选择现有也面中t值最大的,即最近最久未使用的页面予以淘汰。

访问页面70120304230321201701
物理块17772 2 4440  1 1 1  
物理块2 000 0 0032  3 0 0  
物理块3  11 3 3223  2 2 7  
#include<iostream>
#include<list>
#include<vector>
#include<iterator>
#include<fstream>
#include<algorithm>
using namespace std;

class Optimal
{
public:
	int index;
	int dis;
public:
	friend istream & operator>>(istream & i, Optimal&o);
	friend ostream & operator<<(ostream &s, const Optimal&o);
	Optimal(int i)
	{
		this->index = i;
		dis = 0;
	}
	Optimal()
	{
		dis = 0;
	}
	~Optimal()
	{

	}
	bool operator<(Optimal&s)
	{
		if (this->dis < s.dis)
			return true;
		return false;
	}
	bool operator>(Optimal&s)
	{
		if (this->dis > s.dis)
			return true;
		return false;
	}
	bool operator==(const Optimal&s)
	{
		if (this->dis == s.dis)
			return true;
		return false;
	}
};
istream & operator>>(istream & i, Optimal&o)
{
	i >> o.index;
	return i;
}
ostream & operator<<(ostream &s, const  Optimal&o)
{

	s << "[" << "***" << o.index << "***" << "\t" << o.dis << "]";

	return s;
}

void main()
{
	fstream out("data.txt");
	list<Optimal> v;
	list<Optimal> w;
	list<Optimal> l;
	copy(istream_iterator<Optimal>(out), istream_iterator<Optimal>(), back_inserter(v));
	//copy(v.begin(),v.end(),ostream_iterator<Optimal>(cout,"\t"));

	while (v.size())
	{
		if (w.size() < 3)
		{
			Optimal p = v.front();
			v.pop_front();
			w.push_front(p);
			l.push_back(p);
			copy(w.begin(), w.end(), ostream_iterator<Optimal>(cout, "\t"));
			cout << endl;
		}
		else
		{
			bool istrue = false;
			for (auto ib = w.begin(); ib != w.end(); ib++)
			{
				if (ib->index == v.front().index)
				{
					istrue = true;
				}
			}
			if (istrue == false)
			{
				for (auto ib = w.begin(); ib != w.end(); ib++)
				{
					int x = 0;
					for (auto vib = l.rbegin(); vib != l.rend(); vib++)
					{
						if ((*ib).index == (*vib).index)
						{
							break;
						}
						(*ib).dis = ++x;
					}
				}
				list<Optimal>::iterator s = max_element(w.begin(), w.end());
				w.remove(*s);
				Optimal p = v.front();
				v.pop_front();
				l.push_back(p);
				w.push_front(p);
				copy(w.begin(), w.end(), ostream_iterator<Optimal>(cout, "\t"));
				cout << endl;
			}
			else
			{
				Optimal p = v.front();
				v.pop_front();
				l.push_back(p);
			}
		}
	}
	cin.get();
}

data.txt文件

测试结果:

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值