FIFO & LRU

#include <iostream>
using namespace std;

void FIFO(int[], int, int);
void LRU(int[], int, int);

void main() {
	int len;		//序列长度
	int *list;		//序列
	int m;			//页面数

	cout<<"Please input the length of list: ";
	cin>>len;

	list = new int[len];
	cout<<"Please input the list: ";
	for(int i = 0; i < len; i++)
		cin>>list[i];

	cout<<"Please input the number of pages: ";
	cin>>m;

	cout<<endl<<"-----------------The result of FIFO:------------------"<<endl;
	FIFO(list,len,m);
	cout<<"------------------------------------------------------"<<endl;

	cout<<endl<<"-----------------The result of LRU:------------------"<<endl;
	LRU(list,len,m);
	cout<<"-----------------------------------------------------"<<endl<<endl<<endl;

	return ;
}


/************************************************
**函数功能:判断数组Arr中是否存在x
**函数参数:int Arr[]:整型数组
			int Length:数组长度
			int x:待查找元素
**返回值:数组Arr中存在x则返回位置i;否则返回-1
*************************************************/
int isExist(int Arr[], int Length, int x) {
	for(int i = 0; i < Length; i++) {
		if(Arr[i] == x) break;
	}
	if(i >= Length) return -1;
	return i;
}

/************************************************
**函数功能:格式化输出数组Arr[]中的元素
**函数参数:int Arr[]:整型数组
			int Length:数组长度
			bool isMiss:是否缺页
**返回值:无
*************************************************/
void OResult(int Arr[], int Length, bool isMiss) {
	cout<<"*";
	for(int i = 0; i < Length; i++) {
		cout<<' ';

		if(Arr[i] != -1) cout<<Arr[i];
		else cout<<' ';

		cout<<' '<<"*";
	}
	if(isMiss) cout<<" +";
	cout<<endl;

	return ;
}

/************************************************
**函数功能:FIFO算法
**函数参数:int List[]:模拟序列
			int Length:数组长度
			int M:页面数
**返回值:无
*************************************************/
void FIFO(int List[], int Length, int M) {
	
	int miss = 0;			//缺页次数
	bool ismiss = false;	//是否缺页
	int i,j;

	int *page = new int[M];	//页面数组
	for(i = 0; i < M; i++)
		page[i] = -1;

	for(i = 0; i < Length; i++) {
		ismiss = false;
		if(isExist(page,M,List[i]) == -1) {
			ismiss = true;
			miss++;
			for(j = M - 1; j > 0; j--)
				page[j] = page[j - 1];
			page[0] = List[i];
		}
		cout<<"    *************"<<endl<<List[i]<<" ->";
		OResult(page,M,ismiss);	
	}
	cout<<"    *************"<<endl;
	cout<<"The number of missing page is: "<<miss<<endl;
	cout<<"The rate of missing page is: "<<miss*100/Length<<'%'<<endl;

	return ;
}


/************************************************
**函数功能:LRU算法
**函数参数:int List[]:模拟序列
			int Length:数组长度
			int M:页面数
**返回值:无
*************************************************/
void LRU(int List[], int Length, int M) {

	int pos = -1;			//待查找元素位置
	bool ismiss = false;	//是否缺页
	int miss = 0;			//缺页次数
	int i,j;

	int *page = new int[M];	//页面数组
	for(i = 0; i < M; i++)
		page[i] = -1;

	for(i = 0; i < Length; i++) {
		ismiss = false;
		pos = isExist(page,M,List[i]);
		if(pos == -1) {
			ismiss = true;
			miss++;
			for(j = M - 1; j > 0; j--)
				page[j] = page[j - 1];
			page[0] = List[i];
		}
		else if(pos > 0) {
			int temp = page[pos];
			for(j = pos; j > 0; j--)
				page[j] = page[j - 1];
			page[0] = temp;
		}
		cout<<"    *************"<<endl<<List[i]<<" ->";
		OResult(page,M,ismiss);
		
	}
	cout<<"    *************"<<endl;
	cout<<"The number of missing page is: "<<miss<<endl;
	cout<<"The rate of missing page is: "<<miss*100/Length<<'%'<<endl;

	return ;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FIFO (First-In-First-Out) 算法和 LRU (Least Recently Used) 算法都是计算机科学中用于缓存管理和淘汰策略的常用算法。 **FIFO算法**: FIFO 是一种&ldquo;先进先出&rdquo;(First In, First Out)的策略。在这种情况下,当缓存满时,新插入的数据会替换掉最早进入缓存的数据。这是最直观的缓存淘汰策略,因为最先到达的数据如果有访问需求,但又被新的数据挤出,可能会导致数据丢失或访问延迟。 **LRU算法**: LRU 则是&ldquo;最近最少使用&rdquo;(Least Recently Used)的策略。当缓存已满且需要添加新数据时,会选择最近最少使用的数据进行淘汰,即最近一段时间内没有被访问过的数据。这种策略假设如果一个数据不再被频繁访问,那么在未来可能也不太需要,所以倾向于保持最近活跃的数据。 **主要区别**: 1. **淘汰依据**:FIFO 依据数据的进入时间,而 LRU 依据数据的访问时间。 2. **预测能力**:LRU 更具有前瞻性,因为它考虑了数据的最近使用情况,认为&ldquo;最近没用的将来也可能不会用&rdquo;,而 FIFO 则更依赖历史记录。 3. **实现复杂度**:FIFO 实现相对简单,只需要维护一个队列即可;LRU 的实现相对复杂,通常需要额外的数据结构如哈希表和双向链表,以便快速找到最近最少使用的元素。 **相关问题--:** 1. 在什么场景下,FIFO 算法更适合使用? 2. LRU 算法则适用于哪些类型的应用? 3. 当内存资源有限时,选择 LRU 还是 FIFO,哪种策略效率更高?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值