The operating system page replacement simulation programming _LFU

Introduction

  1. Suppose a job allocated to the memory block number 4, 10 can be stored in each page instructions.

  2. A C language program design, performing a simulation job. A total of 320 instruction set of the job, that is, its address space is 32, now it's all the pages have not yet loaded into memory. During the simulation, if the instruction has been accessed in the memory, it displays the physical address, and transfers the next instruction. If the accessed instruction has not been loaded into memory, a page fault occurs, then the number of missing page to be recorded, and the corresponding page into memory. If the four memory blocks have been loaded into the job virtual page, then for page replacement. Finally display its physical address, and transfers the next instruction. After all the 320 instruction execution is complete, calculates and displays the page fault rate occurred during the job is.

  3. Replacement algorithm: Consider LFU algorithm.

  4. Access order of the job requirement command is generated according to the following principles:

    • 50% of the instructions are executed sequentially.
    • 25% of the previous instruction address is uniformly distributed (i.e., a low address) portion.
    • 25% of the instructions are evenly distributed across the address (i.e., the upper address) portion.

    Specific implementation methods are:
    ① a randomly selected starting instruction execution between [0,319], which is a number m;
    execute the next sequential instruction ②, i.e., m + 1 of the instruction sequence number;
    ③ random number, hop go to the front portion of the address [0, m1] of an instruction at which number of M1;
    ④ next instruction is executed, i.e., the instruction number of m1 + 1;
    ⑤ using a random number, jump to the address portion [m1 + 2,319] in an instruction at which M2 is a number;
    the next instruction ⑥ sequence, i.e., the instruction number of m2 + 1;
    ⑦ repeat "before branching to the address part, the order of execution, jumping process address portion, the order of execution "to the rear, until full execution instruction section 320.

  5. Page access instruction sequence into a sequence.
    Of 0 - 9 0 instruction for the first page (corresponding to the virtual memory address [0,9])
    Article 10 - page 19 of instruction 1 (virtual memory address corresponding to [10, 19])
    ...... ..............................
    of 310 - 319 for the first instruction 31 (corresponding to the virtual memory address [310,319])

Least Recently Used (LFU)

Based on the "data if a number of uses in the most recent period rarely, then the possibility of being used in a future period of time is very small," the idea. LFU is based on the number of visits.

Code

//页面置换_LFU
#include<bits/stdc++.h>
using namespace std; 
int s[320];
int cnt = 0;
int lost = 0;
typedef struct node {
	int order; //页面序号 
	int sum;	//页面访问次数 
}cache_;//页面元素 
list<cache_> cache ;

//初始化 
void init()
{ 
	cnt = 0;	//访问次数为 0 
	lost = 0;	//缺页次数为 0  
	for(int i=0;i<320;i++)
		s[i] = 220+i;	//设置指令的物理地址 
}
//寻找当前指令是否在内存块中
bool find (int x)
{
	bool judge = false;
	for(list<cache_>::iterator iter = cache.begin() ; iter != cache.end() ; iter++)
    {
        if((*iter).order == x ){
        	judge = true; 
			(*iter).sum ++;  
		} 
    } 
    return judge;
} 
//当内存空间已满的时候,进行页面置换 
void delete_cache(){
	//找到最少使用的页面
	int minn = 10000; 
	list<cache_>::iterator it ;
	for(list<cache_>::iterator iter = cache.begin() ; iter != cache.end() ; iter++)
    {
        if((*iter).sum < minn ){
        	it = iter; 
        	minn = (*iter).sum;
		} 
    } 
     //将该页面从内存块中删除 
    cache.erase( it);
}
//执行每一条指令 
int process(int x)
{
	cout<<"第"<< cnt<<" 条指令  " ;  
	cnt++; 
	cache_ y;
	y.order = x/10;
	y.sum = 0;
	//判断当前指令是否在内存块中 
	bool judge = find(x/10);
	//如果当前指令在内存块中,显示其物理地址 
	if(judge == true) {
		cout <<"  物理地址:"<<s[x]<<endl;
	}
	//如果当前指令不在内存块中 
	else{ 
		lost ++;
		//如果内存块还有空余,直接将该指令所在页面调入内存块 
		if(cache.size() < 4 ){
			cache.push_back(y);
		}
		//如果内存块没有空余,进行页面置换 
		else {
			delete_cache();
			cache.push_back(y);
		} 
		cout <<"  物理地址:"<<s[x]<<endl;
	}
	return 1;
}
int main()
{
	init();
	int m=rand()%320;//在[0,319]之间随机选取一条起始执行指令,其序号为m 
	process(m);//执行指令,即序号为 m的指令
	process(m+1);//顺序执行下一条指令,即序号为m+1的指令 
	//	重复"跳转到前地址部分、顺序执行、跳转到后地址部分、顺序执行"的过程,159次 
	for(int i=0;i<316;i+=4)
	{ 
		int m1=rand()%m;//通过随机数,跳转到前地址部分[0,m-1]中的某条指令处,其序号为m1
		process(m1);//执行指令,即序号为m1的指令
		process(m1+1);//顺序执行下一条指令,即序号为m1+1的指令
		int m2=rand()%(320-(m1+2)) + m1 + 2;//通过随机数,跳转到后地址部分[m1+2,319]中的某条指令处,其序号为m2
		process(m2);//执行指令,即序号为m1的指令
		process(m2+1);//顺序执行下一条指令,即序号为m2+1的指令
	}
	int m1=rand()%m;//通过随机数,跳转到前地址部分[0,m-1]中的某条指令处,其序号为m1
	process(m1);//执行指令,即序号为m1的指令
	process(m1+1);//顺序执行下一条指令,即序号为m1+1的指令
	cout<<endl<<"缺页率为"<<lost/320.0  <<endl; 
	return 0;
} 

Test Results

Here Insert Picture Description

Published 99 original articles · won praise 16 · views 9851

Guess you like

Origin blog.csdn.net/weixin_42664622/article/details/103775095