FIFO和LRU实现(含代码)

#include <iostream>
using namespace std;
 
const int MAX=6;  //主存块数不超过6 
int FS=0;  //缺页中断次数 
double FSL=0.00;  //缺页中断率 
int M[MAX]; //主存中的内容 
int n;  //主存块数 

struct pro
{
	int time;  //时刻 
	int P;  //页面走向
	int F;  //缺页中断表示 
};

//输入信息函数
void input(pro* p,int N)
{ 
	cout<<"请输入主存块数:";
	cin>>n; 
	cout<<"请输入页面走向:";
	for(int i=0; i<N; i++)
	{
		cin>>p[i].P;
	}
} 
//输出信息函数
void output(pro* p,int N)
{
	cout<<"时刻:    ";
	for(int i=0; i<N; i++)
	{
		p[i].time=i+1;
		cout<<p[i].time<<"  ";
	}
	cout<<endl<<"缺页中断:";
	for(int j=0; j<N; j++)
	{
		cout<<p[j].F<<"  ";
	}
	cout<<endl<<"缺页中断次数:"<<FS;
	cout<<endl<<"缺页中断率:" <<FSL<<"%";
} 
//计算缺页中断次数和缺页中断率
void calculate(pro* p,int N)
{
	for(int i=0; i<N; i++)
	{
		FS=p[i].F+FS;
	}
	FSL=double(FS)/N*100;
} 

//FIFO算法
void FIFO(pro* p,int N)
{
	for(int i=0; i<N; i++) //按照页面走向循环进入主存
	{
		if(i<n) //主存未满,新进入的直接存进主存当中 
		{
			M[i]=p[i].P;
			p[i].F=1;
		} 
		else  //主存已满 
		{
			//检查新进的页面在主存中是否存在 
            bool exist = false;
            for (int j=0;j<3;j++)
            {
            	//如果存在和主存中相同的将exist更改为true,并标记没有发生缺页中断 
                if (p[i].P==M[j])
                {
                    exist=true;
                    p[i].F=0; 
                    break;
                }
            }
            //经过上一循环的比对如果exist==false,就更改主存中的信息,并标记发生缺页中断。 
            if (exist==false)
            {
               for(int k=0; k<n-1; k++)
               {
                    M[k]=M[k+1];
			   }
			   M[n-1]=p[i].P;
			   p[i].F=1;
            }

		}
	} 
} 
//LRU算法
void LRU(pro* p,int N)
{
	for(int i=0; i<N; i++) //按照页面走向循环进入主存
	{
		if(i<n) //主存未满,新进入的直接存进主存当中 
		{
			M[i]=p[i].P;
			p[i].F=1;
		} 
		else  //主存已满 
		{
			//检查新进的页面在主存中是否存在 
            bool exist = false;
            for (int j=0;j<3;j++)
            {
            	//如果存在和主存中相同的将exist更改为true,更改主存中页面顺序并标记没有发生缺页中断 
                if (p[i].P==M[j])
                {
                    exist=true;
                    int x=M[j]; 
                    for(int k=0; k<n-1; k++)
                   {   
                        M[k]=M[k+1];
			       }
			       M[n-1]=x; 
                    p[i].F=0; 
                    break;
                }
            }
            //经过上一循环的比对如果没有和主存中内容相同的即exist==false,就更改主存中的信息,并标记发生缺页中断。 
            if (exist==false)
            {
               for(int k=0; k<n-1; k++)
               {
                    M[k]=M[k+1];
			   }
			   M[n-1]=p[i].P;
			   p[i].F=1;
            }

		}
	} 
} 
int main()
{
	int N;
	cout<<"请输入使用算法的时间:";
	cin>>N; 
	pro* p=new pro[N];
	input(p,N);
	
	cout<<endl<<"FIFO算法:"<<endl;
	FIFO(p,N);
	calculate(p,N);
	output(p,N);
	
	FS=0;  //在进行LRU算法之前先重置缺页中断次数。 
	cout<<endl<<"LRU算法:"<<endl;
	LRU(p,N);
	calculate(p,N);
	output(p,N);
	return 0;
} 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现 FIFOLRU 算法的关键在于如何维护队列或链表以及如何在缓存空间满时选择哪个页面进行替换。 FIFO(First In First Out)算法,即先进先出算法,是一种简单的页面置换算法。它的实现方式一般使用队列,将最早进入缓存的页面排在队首,最后进入缓存的页面排在队尾。当缓存空间满时,弹出队首页面,将新页面加入队尾即可。 以下是 C 语言实现 FIFO 算法的示例代码: ```c #include <stdio.h> #include <stdlib.h> #define CACHE_SIZE 5 int cache[CACHE_SIZE]; int head = 0; int tail = 0; void push(int page) { if ((tail + 1) % CACHE_SIZE == head) { printf("Cache is full, page %d is replaced by page %d.\n", cache[head], page); head = (head + 1) % CACHE_SIZE; } cache[tail] = page; tail = (tail + 1) % CACHE_SIZE; } int main() { push(1); push(2); push(3); push(4); push(5); push(6); push(7); push(8); return 0; } ``` LRU(Least Recently Used)算法,即最近最少使用算法,是一种比 FIFO 更高效的页面置换算法。它的实现方式一般使用双向链表,将最近访问的页面排在链表尾部,最早访问的页面排在链表头部。当缓存空间满时,弹出链表头部页面,将新页面加入链表尾部即可。 以下是 C 语言实现 LRU 算法的示例代码: ```c #include <stdio.h> #include <stdlib.h> #define CACHE_SIZE 5 typedef struct node { int page; struct node *prev; struct node *next; } Node; Node *head = NULL; Node *tail = NULL; void push(int page) { Node *new_node = (Node*)malloc(sizeof(Node)); new_node->page = page; new_node->prev = NULL; new_node->next = NULL; if (head == NULL) { head = new_node; tail = new_node; } else { tail->next = new_node; new_node->prev = tail; tail = new_node; } Node *node = head; while (node != NULL && node != tail) { if (node->page == page) { if (node->prev != NULL) { node->prev->next = node->next; } else { head = node->next; } node->next->prev = node->prev; free(node); break; } node = node->next; } if (tail->next != NULL) { Node *old_head = head; head = old_head->next; head->prev = NULL; free(old_head); } } int main() { push(1); push(2); push(3); push(4); push(5); push(6); push(7); push(8); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值