【操作系统】内存管理设计性实验报告

7 篇文章 1 订阅

操作系统#内存管理设计性实验报告

正文

一、 实验目的

  1. 加深对虚拟存储器的理解。
  2. 熟练掌握常用页面置换算法的实现原理。

二、 实验设备

实验机房虚拟机里的中linux系统。

三、 实验要求

1、在FIFO置换算法的基础上,编写实现LRU页面置换算法;
按如下的访问序列:12560365365604270435,

2、在物理块数分别为3和4时,分别调用FIFO和LRU算法,计算并输出相应的命中率。

附代码:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef  struct
{  
	int  pn, pfn, counter, time;
} pl_type ;


struct  pfc_struct
{  
	int  pn, pfn;
	struct  pfc_struct  *next;
};
typedef  struct pfc_struct  pfc_type;
#define INVALID -1

//total_vp页面个数
//pfc[total_vp]定义用户进程的页帧控制结构数组
static int a[320];			//指令流数组
int diseffect=0;					//页面失效次数
int page[320]={1,2,5,6,0,3,6,5,3,6,5,6,0,4,2,7,0,4,3,5};		// 每条指令所属页面号
int offset[320]={-1};		//每页装入10条指令后取模运算得出的页内偏移地址
int total_pf=0;						//用户进程的内存页帧数=块数
int total_vp=32;
int total_instruction;
pl_type pl[32];
pfc_type   *freepf_head, *busypf_head, *busypf_tail;//pfc[total_vp],

int initialize(int total_pf)
{
	int i;
	pfc_type *pf;
	for(i=0;i<total_vp;i++)
	{
		pl[i].pn=i;
		pl[i].pfn=INVALID;
		pl[i].counter=0;
		pl[i].time=-1;
	}
	freepf_head=(pfc_type *) malloc(sizeof(pfc_type));
	if(freepf_head==NULL)
		return -1;
	else
	{
		freepf_head->pfn=0;
		freepf_head->next=NULL;
		pf=freepf_head;
		for(i=1;i<total_pf;i++)
		{
			pf->next=(pfc_type *) malloc(sizeof(pfc_type));
			pf=pf->next;
			pf->pfn=i;
			pf->next=NULL;
		}
	}
	busypf_head=busypf_tail=NULL;
	return 0;
}
void LRU(int total_pf)
{
	int i,farthest,j;
	pfc_type *p,*q,*a;
	initialize(total_pf);
	for(i=0;i<total_instruction;i++)
	{
	    if(pl[page[i]].pfn==INVALID) /*页面失效*/
		{  
	        diseffect=diseffect+1;
			if(freepf_head==NULL) /*无空闲页帧则把换出页面作为空闲页帧*/
			{  
				
				p=busypf_head;
				q=p;
				farthest=p->pn;
				while(p!=NULL)
				{
					if(pl[p->pn].time<pl[farthest].time)
						farthest=p->pn;
					p=p->next;
				}
		
				p=busypf_head;
				q=p;
				while(p->pn!=farthest&&p!=NULL)
				{
					q=p;
					p=p->next;
				}
				if(p==busypf_head)
				{
					busypf_head=busypf_head->next;
					pl[p->pn].pfn=INVALID;  //将忙页帧队首原来页面作为换出页面
					freepf_head=p; 
					freepf_head->next=NULL;
				}
				else if(p!=busypf_tail)
				{
					q->next=p->next;
					pl[p->pn].pfn=INVALID;  //将忙页帧上次被调用时间最早的页面作为换出页面
					freepf_head=p; 
					freepf_head->next=NULL;
				}
				else
				{
					busypf_tail=q;
					pl[p->pn].pfn=INVALID;  //将忙页帧队尾的页面作为换出页面
					freepf_head=p; 
					freepf_head->next=NULL;
				}
			}
			p=freepf_head->next;   //有空闲页帧
			freepf_head->next=NULL;	//将这个将被征用的空闲页帧和后面的断开
			freepf_head->pn=page[i]; 	/* 将所需页面调入空闲页帧 */
			pl[page[i]].pfn=freepf_head->pfn;//标注页表中此页对应的块号
			pl[page[i]].time=time(NULL)+i;
			if(busypf_head==NULL)  /* 若忙页帧队列为空,则将其头尾指针都指向刚调入页面所在的页帧 */	
			{
				busypf_head=busypf_tail=freepf_head;
				busypf_tail->next=NULL;
			}
			else
			{ //否则,将刚调入页面所在的页帧挂在忙页帧队列尾部
				busypf_tail->next=freepf_head;
				busypf_tail=busypf_tail->next;
				busypf_tail->next=NULL;
			}
			freepf_head=p; //空闲页帧头指针后移
	    }
		else
		{
			pl[page[i]].time=time(NULL)+i;
		}
    }
	printf("未命中数:%d\n\n",diseffect);
	printf("LRU命中率:%6.4f \n",1-(float)diseffect/20);
}
void FIFO(int total_pf) /*先进先出页面置换算法*/
{    
	int i;
    pfc_type *p;
    initialize(total_pf);
    busypf_head=busypf_tail=NULL;
	for(i=0;i<total_instruction;i++)
	{
	    if(pl[page[i]].pfn==INVALID) /*页面失效*/
		{  
	        diseffect=diseffect+1;
			if(freepf_head==NULL) /*无空闲页帧则把换出页面作为空闲页帧*/
			{  
				p=busypf_head->next;
				pl[busypf_head->pn].pfn=INVALID;  //将忙页帧队首原来页面作为换出页面
				freepf_head=busypf_head; 
				freepf_head->next=NULL;
				busypf_head=p; //忙页帧头指针后移
			}
			p=freepf_head->next;   //有空闲页帧
			freepf_head->next=NULL;	//将这个将被征用的空闲页帧和后面的断开
			freepf_head->pn=page[i]; 	/* 将所需页面调入空闲页帧 */
			pl[page[i]].pfn=freepf_head->pfn;		//标注页表中此页对应的块号
			if(busypf_tail==NULL)  /* 若忙页帧队列为空,则将其头尾指针都指向刚调入页面所在的页帧 */
				busypf_head=busypf_tail=freepf_head;
			else
			{ //否则,将刚调入页面所在的页帧挂在忙页帧队列尾部
				busypf_tail->next=freepf_head;
				busypf_tail=freepf_head;
			}
			freepf_head=p; //空闲页帧头指针后移
	    }
    }
	printf("未命中数:%d\n",diseffect);
   printf("FIFO命中率:%6.4f \n",1-(float)diseffect/20);
}
int main()
{
	int i,ch;
	printf("请输入指令个数:\n");
	scanf("%d",&total_instruction);
	printf("指令序列所在页面号如下\n");
	for(i=0;i<total_instruction;i++)
		printf("->  %d",page[i]);
	printf("\n请输入内存块数:\n");
	scanf("%d",&total_pf);
	printf("************Page replacement algorithm************\n\n");
	printf("\t\t\t1.FIFO\n\n");
	printf("\t\t\t2.LRU\n\n");
	printf("choose the algorithm:\n");
	scanf("%10d",&ch);
	switch(ch)
	{
		case 1:
				FIFO(total_pf);
				break;
		case 2:
				LRU(total_pf);
				break;
		default:printf("enter error data!\n");
	}
}

实验结果:在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
对于FIFO页面置换算法,每次置换时只需换出忙队列对手的页面,新加入的页面放到队尾;对于LRU页面置换算法,每次都要找到忙队列中上次调用距今最久的页面,因此要被换出的页面在队头、队尾和队中的操作不同。
从实验结果来看,在一定情况下,增加内存块数可以降低缺页率(当然FIFO算法除外),同时,在相同条件下,从两个算法的缺页率来看,LRU算法缺页率更低。由此可以看到,由局部性原理可知,LRU算法相对FIFO算法而言,更接近最佳算法

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

君问归期魏有期

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值