内存管理FIFO算法(实验7)

编程实验任务
编写一个程序,用rand()生成一个随机的页访问序列,该序列长度为320;采用FIFO的页面置换策略,计算当页数为32,帧数为4~32时的页面访问命中率。(命中率=1-页面失效次数/页面访问序列长度。)

注:在计算过程中,需要构造一个页面数组,一个页面访问序列,一个空闲帧队列,一个被占用帧队列。

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

struct pfc_struct
{
	int pn,pfn;
	struct pfc_struct * next;
};

int s[320];					
struct pl_type pl[32];	
struct pfc_struct pfc[32];	
struct pfc_struct * freepf_head,* busypf_head,* busypf_tail;

void initial(int pf);
void fifo(int pf);

int main()
{
	int i,total_pf;

	srand(getpid());			
	for(i=0;i<320;i++)
	{
		s[i]=rand()%32;
	}

	for(total_pf=4;total_pf<=32;total_pf++)		
	{
		initial(total_pf);		
		fifo(total_pf);			
	}
	return 0;
}

void initial(int pf)
{
	int i;

	for(i=0;i<32;i++)			
	{
		pl[i].pn=i;
		pl[i].pfn=-1;
	}

	for(i=0;i<pf-1;i++)			
	{
		pfc[i].pfn=i;
		pfc[i].next=&pfc[i+1];
	}
	pfc[pf-1].pfn=pf-1;
	pfc[pf-1].next=NULL;

	freepf_head=&pfc[0];
	busypf_head=NULL;
	busypf_tail=NULL;
}

void fifo(int pf)
{
	int i,diseffect=0;			
	struct pfc_struct *p;

	for(i=0;i<320;i++)				
	{
		if(pl[s[i]].pfn==-1)		
		{
			diseffect+=1;			

			if(freepf_head==NULL)		
			{
				p=busypf_head->next;
				pl[busypf_head->pn].pfn=-1;
				freepf_head=busypf_head;
				freepf_head->next=NULL;
				busypf_head=p;
			}

			p=freepf_head->next;		
			freepf_head->next=NULL;
			freepf_head->pn=s[i];
			pl[s[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;
				busypf_tail->next=NULL;
			}
			freepf_head=p;
		}
	}
	printf("%d frames %f\n",pf,1-(float)diseffect/320);
}

猜你喜欢

转载自blog.csdn.net/YZ_TONGXIE/article/details/106799320
今日推荐