存储管理---------常用页面置换算法模拟实验

合集下载

《操作系统》实验五页面置换算法模拟

《操作系统》实验五页面置换算法模拟

实验五. 请求页式存储管理的模拟[实验内容]:熟悉虚拟存储管理的各种页面置换算法,并编写模拟程序实现请求页式存储管理的页面置换算法----最近最久未使用算法(LRU),要求在每次产生置换时显示页面分配状态和缺页率。

[实验要求]:1、运行给出的实验程序,查看执行情况,进而分析算法的执行过程,在理解FIFO页面置换算法和最近最久未使用算法(LRU)置换算法后,给出最佳置换算法的模拟程序实现,并集成到参考程序中。

2、执行2个页面置换模拟程序,分析缺页率的情况。

最好页框数和访问序列长度可调节,在使用同一组访问序列数据的情况下,改变页框数并执行2个页面置换模拟程序,查看缺页率的变化。

3、在每次产生置换时要求显示分配状态和缺页率。

程序的地址访问序列通过随机数产生,要求具有足够的长度。

最好页框数和访问序列长度可调节。

实验的执行结果如下图所示(左下图为FIFO执行结果,右下图为LRU执行结果):程序源代码:#include <libio.h>#include "windows.h"#include <conio.h>#include <stdlib.h>#include <fstream.h>#include <io.h>#include <string.h>#include <stdio.h>void initialize(); //初始化相关数据结构void createps(); //随机生成访问序列void displayinfo(); //显示当前状态及缺页情况void fifo(); //先进先出算法int findpage(); //查找页面是否在内存void lru(); //最近最久未使用算法int invalidcount = 0; // 缺页次数int vpoint; //页面访问指针int pageframe[10]; // 分配的页框int pagehistory[10]; //记录页框中数据的访问历史int rpoint; //页面替换指针int inpflag; //缺页标志,0为不缺页,1为缺页struct PageInfo //页面信息结构{int serial[100]; // 模拟的最大访问页面数,实际控制在20以上int flag; // 标志位,0表示无页面访问数据int diseffect; // 缺页次数int total_pf; // 分配的页框数int total_pn; // 访问页面序列长度} pf_info;//////////////////////////////////////////////////////////////////////// //初始化相关数据结构void initialize(){int i,pf;inpflag=0; //缺页标志,0为不缺页,1为缺页pf_info.diseffect =0; // 缺页次数pf_info.flag =0; // 标志位,0表示无页面访问数据printf("\n请输入要分配的页框数:"); // 自定义分配的页框数scanf("%d",&pf);pf_info.total_pf =pf;for(i=0;i<100;i++) // 清空页面序列{pf_info.serial[i]=-1;}///////////////////////////////////////////////////////////////////// 随机生成访问序列void createps(void ){int s,i,pn;initialize(); //初始化相关数据结构printf("\n请输入要随机生成访问序列的长度:"); //自定义随机生成访问序列的长度scanf("%d",&pn);srand(rand()); //初始化随机数队列的"种子"s=((float) rand() / 32767) * 50 + pn; // 随机产生页面序列长度pf_info.total_pn = s;for(i=0;i<s;i++) //产生随机访问序列{pf_info.serial[i]=((float) rand() / 32767) * 16 ; //随机数的大小在0-15之间 }}////////////////////////////////////////////////////////////////////////// 显示当前状态及缺页情况void displayinfo(void){int i,n;if(vpoint==0){printf("\n=============页面访问序列=============\n");for(i=0; i<pf_info.total_pn; i++){printf("%4d",pf_info.serial[i]);if ((i+1) % 10 ==0) printf("\n"); //每行显示10个}printf("\n======================================\n");}printf("访问%3d : 内存<",pf_info.serial[vpoint]);for(n=0;n<pf_info.total_pf;n++) // 页框信息{if (pageframe[n] >=0)printf("%3d",pageframe[n]);elseprintf(" ");}printf(" >");if(inpflag==1) //缺页标志,0为不缺页,1为缺页{printf(" ==>缺页 ");printf("缺页率%3.1f",(float)(pf_info.diseffect)*100.00/vpoint);}printf("\n");}//////////////////////////////////////////////////////////////////////// // 查找页面是否在内存,1为在内存,0为不在即缺页int findpage(int page){int n;for(n=0;n<pf_info.total_pf;n++){pagehistory[n] ++; // 访问历史加1}for(n=0;n<pf_info.total_pf;n++){if (pageframe[n]==page ){inpflag=0 ; //inpflag缺页标志,0为不缺页,1为缺页pagehistory[n]=0; //置访问历史为0return 1;}}inpflag=1; //页面不存在,缺页return 0;}//////////////////////////////////////////////////////////////////////// // FIFO页面置换算法void fifo(void){int n,count,pstate;rpoint=0; // 页面替换指针初始化为0invalidcount = 0; // 缺页数初始化为0createps(); // 随机生成访问序列count=0; // 是否装满是所有的页框for(n=0;n<pf_info.total_pf;n++) // 清除页框信息{pageframe[n]=-1;}inpflag=0; //缺页标志,0为不缺页,1为缺页for(vpoint=0;vpoint<pf_info.total_pn;vpoint++) // 执行算法{pstate=findpage(pf_info.serial[vpoint]); //查找页面是否在内存if(count<pf_info.total_pf) // 开始时不计算缺页{if(pstate==0) // 页不存在则装入页面{pageframe[rpoint]=pf_info.serial[vpoint];rpoint=(rpoint+1) % pf_info.total_pf;count++;}}else // 正常缺页置换{if(pstate==0) // 页不存在则置换页面{pageframe[rpoint]=pf_info.serial[vpoint];rpoint=(rpoint+1) % pf_info.total_pf;pf_info.diseffect++; // 缺页次数加1}}Sleep(10);displayinfo(); // 显示当前状态} // 置换算法循环结束getch();return;}///////////////////////////////////////////////////////////////////// LRU页面置换算法void lru(void){int n,count,pstate,max;rpoint=0; // 页面替换指针invalidcount = 0; // 缺页次数初始化为0createps(); // 随机生成访问序列count=0; // 是否装满所有的页框for(n=0;n<pf_info.total_pf;n++){pageframe[n]=-1; // 清除页框信息pagehistory[n]=0; // 清除页框历史}inpflag=0; //缺页标志,0为不缺页,1为缺页for(vpoint=0;vpoint<pf_info.total_pn;vpoint++) // 执行算法{pstate=findpage(pf_info.serial[vpoint]); //查找页面是否在内存if(count<pf_info.total_pf) // 开始时不计算缺页{if(pstate==0) // 页不存在则装入页面{pageframe[rpoint]=pf_info.serial[vpoint]; //把要调入的页面放入一个空的页框里rpoint=(rpoint+1) % pf_info.total_pf;count++;}}else // 正常缺页置换{if(pstate==0)// 页不存在则置换页面{max=0;for(n=1;n<pf_info.total_pf;n++){if(pagehistory[n]>pagehistory[max]){max=n;}}rpoint=max;pageframe[rpoint]=pf_info.serial[vpoint];pagehistory[rpoint]=0;pf_info.diseffect++; // 缺页次数加1}}Sleep(10);displayinfo(); // 显示当前状态} // 置换算法循环结束_getch();return;}/////////////////////最佳置换算法自己完成///////////////////////////////////////////////////////////////////// 主函数int main(){char ch;system("cls") ;while ( true ){printf("*******************************************\n");printf(" 若要执行FIFO页面置算法请按1\n");printf(" 若要执行LRU 页面置算法请按2\n");printf(" 若要退出请按3\n") ;printf("*******************************************\n");printf( "Enter your choice (1 or 2 or 3): ");do{ //如果输入信息不正确,继续输入ch = (char)getch() ;}while(ch != '1' && ch != '2'&& ch != '3');printf("\n\n你按的是:%c ,现在为你执行对应操作。

页面置换算法模拟实验报告

页面置换算法模拟实验报告
for(int i=0; i<Bsize; i++)
if(block[i].timer >= block[pos].timer)
pos = i;//找到应予置换页面,返回BLOCK中位置
return pos;
}
void PRA::display(void)
{
for(int i=0; i<Bsize; i++)
}
}
int PRA::findSpace(void)
{
for(int i=0; i<Bsize; i++)
if(block[i].content == -1)
return i;//找到空闲内存,返回BLOCK中位置
return -1;
}
int PRA::findExist(int curpage)
{
if(exist != -1)
{
cout<<"不缺页"<<endl;
}
else
{
space = findSpace();
if(space != -1)
{
block[space] = page[i];
display();
}
else
{
for(int k=0; k<Bsize; k++)
for(int j=i; j<Psize; j++)
int findReplace(void); //查找应予置换的页面
void display(void); //显示
void FIFO(void);//FIFO算法

常用页面置换算法模拟实验

常用页面置换算法模拟实验

实验环境
Windows xp 、Microsoft Visual c++ 6.0
实验目的与要求:
通过模拟实现请求页式存储管理的几种基本页面置换算法,了解虚拟存储技术的特 点,掌握虚拟存储请求页式存储管理中几种基本页面置换算法的基本思想和实现过程,并 比较它们的效率。
实验内容:
设计一个虚拟存储区和内存工作区,并使用下述算法计算访问命中率。
struct Pro { int num,time; };
二、 输入实际页数
函数名称:int Input(int m,Pro p[M]) 函数功能:输入实际页数,并输入各页面号
三、 查找页面是否存在
函数名称:int Search(int e,Pro *page1)
四、 找出离现在时间最长的页面
函数名称:int Max(Pro *p Nhomakorabeage1)
五、 比较页面
函数名称:int Compfu(Pro *page1,int i,int t,Pro p[M])
六、 打印当前的页面
函数名称:void print(Pro *page1)
七、 主函数
函数名称:int main() 1、 输入可用内存页面数 2、 初试化页面基本情况 3、 选择置换页面算法:FIFO 页面置换、LRU 页面置换、OPT 页面置换 显示页面置换情况 4、 5、 输出缺页次数和缺页率 6、 按其它键结束程序。
实验总结:
1.最佳淘汰算法(OPT) 2.先进先出的算法(FIFO) 3.最近最久未使用算法(LRU) 命中率=1-页面失效次数/页地址流长度
1、一个算法的实现,要考虑很多的全面,要想写好算法,必须将数据结构和 C 语言学习好。

《FIFO算法来模拟实现页面的置换》实验报告

《FIFO算法来模拟实现页面的置换》实验报告
数据结构
struct pageInfor
{
int content;//页面号
int timer;//被访问标记
};
class PRA
{
public:
PRA(void);
int findSpace(void); //查找是否有空闲内存
int findExist(int curpage); //查找内存中是否有该页面
FIFO算法总是淘汰最先进入内存的页面,即选择在内存中驻留时间最久的页面予以淘汰。该算法实现简单,只需把一个进程已调入内存的页面,按照先后次序连接成一个队列,并设置一个替换指针,使它总指向最老的页面。
pageInfor * page; //页面号串
private:
}
运行界面:
总结
在进程运行过程中,若其所访问的页面不存在内存而需要把它们调入内存,但内存已无空闲时,为了保证该进程能够正常运行,系统必须从内存中调出一页程序或数据送磁盘的对换区中。但应调出哪个页面,需根据一定的算法来确定,算法的好坏,直接影响到系统的性能。
int findReplace(void); //查找应予置换的页面
void display(void); //显示
void FIFO(void); //FIFO算法
void BlockClear(void);//BLOCK清空,以便用另一种方法重新演示
pageInfor * block; //物理块
《FIFO算法来模拟实现页面的置换》实验报告
实验
时间
2011年05月16日
实验人
虎胆英侠
专业
计算机
学号
实验
名称
FIFO算法来模拟实现页面的置换
目的

实验请求页式存储管理页面置换算法

实验请求页式存储管理页面置换算法

操作系统实验报告班级:计科0801班姓名:韩伟伟学号:08407106 时间:2018-5-25实验五请求页式存储管理的页面置换算法一.实验目的通过请求页式存储管理中页面置换算法模拟程序,了解虚拟存储技术的特点,掌握请求页式存储管理的页面置换算法。

二.实验属性设计三.实验内容1.通过随机数产生一个指令序列,共320条指令,指令的地址按下述原则生产:50%的指令是顺序执行的;25%的指令是均匀分布在前地址部分;25%的指令是均匀分布在后地址部分。

2.将指令序列变换成为页地址流设页面大小为1K;用户内存容量为4页到32页;用户虚存容量为32K。

在用户虚存中,按每K存放10条指令排列虚存地址,即320条指令在虚存中的存放方式为:第0条至第9条指令为第0页;第10条至19条指令为第1页;…第310条至319条指令为第31页。

3.计算并输出下述各种算法在不同内存容量下的命中率。

(1>先进先出算法<FIFO)(2>最近最少使用算法<LRU)(3>最佳使用算<OPT)命中率=1-页面失效次数/页地址流长度本实验中,页地址流长度为320,页面失效次数为每次访问相应指令时,该指令所对应的页不在内存的次数。

四.思路关于随机数的产生办法。

首先要初始化设置随机数,产生序列的开始点,例如,通过下列语句实现:srand ( 400 > ;(1>计算随机数,产生320条指令序列m=160;for (i=0;i<80;i++={j=i﹡4;a[j]=m;a[j+1]=m+1;a[j+2]=a[j] ﹡1.0﹡ rand( >/32767;a[j+3]=a[j+2]+1m=a[j+3]+(319-a[j+3]> ﹡1.0﹡rand( >/32767;}(2>将指令序列变换成为页地址流for ( k=0;k<320;k++>{ pt=a[k]/10;pd= a[k]%10;…}(3>计算不同算法的命中率rate=1-1.0﹡U/320 ;其中U为缺页中断次数,320是页地址流长度。

实验三页面置换算法模拟实验

实验三页面置换算法模拟实验

计算机科学系实验报告书课程名:《操作系统》题目:虚拟存储器管理页面置换算法模拟实验班级:学号:姓名:一、实验目的与要求1.目的:请求页式虚存管理是常用的虚拟存储管理方案之一。

通过请求页式虚存管理中对页面置换算法的模拟,有助于理解虚拟存储技术的特点,并加深对请求页式虚存管理的页面调度算法的理解。

2.要求:本实验要求使用C语言编程模拟一个拥有若干个虚页的进程在给定的若干个实页中运行、并在缺页中断发生时分别使用FIFO和LRU算法进行页面置换的情形。

其中虚页的个数可以事先给定(例如10个),对这些虚页访问的页地址流(其长度可以事先给定,例如20次虚页访问)可以由程序随机产生,也可以事先保存在文件中。

要求程序运行时屏幕能显示出置换过程中的状态信息并输出访问结束时的页面命中率。

程序应允许通过为该进程分配不同的实页数,来比较两种置换算法的稳定性。

二、实验说明1.设计中虚页和实页的表示本设计利用C语言的结构体来描述虚页和实页的结构。

在虚页结构中,pn代表虚页号,因为共10个虚页,所以pn的取值范围是0—9。

pfn代表实页号,当一虚页未装入实页时,此项值为-1;当该虚页已装入某一实页时,此项值为所装入的实页的实页号pfn。

time项在FIFO算法中不使用,在LRU中用来存放对该虚页的最近访问时间。

在实页结构中中,pn代表虚页号,表示pn所代表的虚页目前正放在此实页中。

pfn代表实页号,取值范围(0—n-1)由动态指派的实页数n所决定。

next是一个指向实页结构体的指针,用于多个实页以链表形式组织起来,关于实页链表的组织详见下面第4点。

2.关于缺页次数的统计为计算命中率,需要统计在20次的虚页访问中命中的次数。

为此,程序应设置一个计数器count,来统计虚页命中发生的次数。

每当所访问的虚页的pfn项值不为-1,表示此虚页已被装入某实页内,此虚页被命中,count加1。

最终命中率=count/20*100%。

3.LRU算法中“最近最久未用”页面的确定为了能找到“最近最久未用”的虚页面,程序中可引入一个时间计数器countime,每当要访问一个虚页面时,countime的值加1,然后将所要访问的虚页的time项值设置为增值后的当前countime值,表示该虚页的最后一次被访问时间。

存储管理与页面置换算法

存储管理与页面置换算法

实验二存储管理与页面置换算法一、实验目的通过模拟页式虚拟存储管理中地址转换和页面置换,了解页式虚拟存储管理的思想,掌握页式地址转换过程和缺页中断处理过程。

二、实验学时4学时三、实验内容单机模拟页式虚拟存储管理中地址转换和页面置换过程。

首先对页表进行初始化;输入要访问的逻辑地址(可为16进制或10进制),程序分离出逻辑地址的页号,查找页表,根据页表完成地址转换,输出转换后的地址;若缺页则提示中断发生,按某种页面置换算法(FIFO,LRU,LFU)进行页面置换,并修改和输出页表,输出绝对地址。

最后输出置换情况和缺页次数。

四、算法描述1 内存的分配和管理方案在进程创建时必须为它分配一定的内存资源,内存资源的分配与管理有很多方法,从动态性分有静态的和动态的分配方法,从连续性上分有连续的和不连续的分配方案。

连续的分配方案是程序的执行速度加快但会使内存出现碎片而不能得到应用,而不连续的分配方案可以使内存碎片得到充分的应用,但由于访问内存次数的增多使程序执行的速度下降。

2 内存的分配的过程在作业执行前,向系统提供内存的请求表,在系统为作业创建进程时,要为进程分配内存资源。

以分页系统为例,系统首先确定进程需要的页面数量,然后顺序查找位图(系统为每一个页面分配一位内存中的各个页面组成一个数组,如果该位为1说明该位所指示的页正在被使用,如果该位为0说明该位指示的页面空闲)若存在所需要的空闲页面则此次分配成功,否则分配失败,若分配成功系统首先把分配出去的页面所属的位置为1,然后形成进程所需的页表。

3算法思想本程序有两个功能:一是地址转换;二是模拟页面置换情况。

(1)地址转换:add_tran将逻辑地址中的页号分离出来,查找页表,将查找到的块号与逻辑地址中的页内偏移量合成实际地址,若查找不到则产生缺页中断,按FIFO的方法置换页面。

(a)数据结构:array[max][2]为页表,其中array[n][0]为页号,array[n][1]为块号,size_PT表示系统分配给进程的块数,即页表中的页数。

请求页式存储管理中常用页面置换算法模拟

请求页式存储管理中常用页面置换算法模拟

计算机操作系统实验报告济南大学信息科学与技术学院2013年xx月xx日一、实验概述1. 实验名称请求页式存储管理中常用页面置换算法管理2. 实验目的(1)了解内存分页管理策略(2)掌握调页策略(3)掌握一般常用的调度算法(4)学会各种存储分配算法的实现方法。

(5)了解页面大小和内存实际容量对命中率的影响3. 实验内容(1)采用页式分配存储方案,通过分别计算不同算法的命中率来比较算法的优劣,同时也考虑页面大小及内存实际容量对命中率的影响;(2)实现OPT 算法(最优置换算法) 、LRU 算法(Least Recently) 、FIFO 算法(First IN First Out)的模拟;(3)使用某种编程语言模拟页面置换算法。

二、实验环境C语言三、实验过程1. 设计思路和流程图选择置换算法,先输入所有页面号,为系统分配物理块,依次进行置换2. 算法实现(1)OPT基本思想:是用一维数组page[pSIZE]存储页面号序列,memery[mSIZE]是存储装入物理块中的页面。

数组next[mSIZE]记录物理块中对应页面的最后访问时间。

每当发生缺页时,就从物理块中找出最后访问时间最大的页面,调出该页,换入所缺的页面。

(2)FIFO基本思想:是用队列存储内存中的页面,队列的特点是先进先出,与该算法是一致的,所以每当发生缺页时,就从队头删除一页,而从队尾加入缺页。

或者借助辅助数组time[mSIZE]记录物理块中对应页面的进入时间,每次需要置换时换出进入时间最小的页面。

(3)LRU基本思想:是用一维数组page[pSIZE]存储页面号序列,memery[mSIZE]是存储装入物理块中的页面。

数组flag[10]标记页面的访问时间。

每当使用页面时,刷新访问时间。

发生缺页时,就从物理块中页面标记最小的一页,调出该页,换入所缺的页面。

3.源程序并附上注释#include <stdio.h>#include <stdlib.h>/*全局变量*/int mSIZE; /*物理块数*/int pSIZE; /*页面号引用串个数*/static int memery[10]={0}; /*物理块中的页号*/static int page[100]={0}; /*页面号引用串*/static int temp[100][10]={0}; /*辅助数组*//*置换算法函数*/void FIFO();void LRU();void OPT();/*辅助函数*/void print(unsigned int t);void designBy();void download();void mDelay(unsigned int Delay);/*主函数*/void main(){int i,k,code;system("color 0A");designBy();printf("┃请按任意键进行初始化操作... ┃\n");printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");printf(" >>>");getch();system("cls");system("color 0B");printf("请输入物理块的个数(M<=10):");scanf("%d",&mSIZE);printf("请输入页面号引用串的个数(P<=100):");scanf("%d",&pSIZE);puts("请依次输入页面号引用串(连续输入,无需隔开):");for(i=0;i<pSIZE;i++)scanf("%1d",&page[i]);download();system("cls");system("color 0E");do{puts("输入的页面号引用串为:");for(k=0;k<=(pSIZE-1)/20;k++){for(i=20*k;(i<pSIZE)&&(i<20*(k+1));i++){if(((i+1)%20==0)||(((i+1)%20)&&(i==pSIZE-1)))printf("%d\n",page[i]);elseprintf("%d ",page[i]);}}printf("* * * * * * * * * * * * * * * * * * * * * * *\n");printf("* 请选择页面置换算法:\t\t\t *\n");printf("* ----------------------------------------- *\n");printf("* 1.先进先出(FIFO) 2.最近最久未使用(LRU) *\n");printf("* 3.最佳(OPT) 4.退出*\n");printf("* * * * * * * * * * * * * * * * * * * * * * *\n");printf("请选择操作:[ ]\b\b");scanf("%d",&code);switch(code){case 1:FIFO();break;case 2:LRU();break;case 3:OPT();break;case 4:system("cls");system("color 0A");designBy(); /*显示设计者信息后退出*/printf("┃谢谢使用页面置换算法演示器! 正版授权㊣┃\n");printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");exit(0);default:printf("输入错误,请重新输入:");}printf("按任意键重新选择置换算法:>>>");getch();system("cls");}while (code!=4);getch();}/*载入数据*/void download(){int i;system("color 0D");printf("╔════════════╗\n");printf("║正在载入数据,请稍候!!!║\n");printf("╚════════════╝\n");printf("Loading...\n");printf(" O");for(i=0;i<51;i++)printf("\b");for(i=0;i<50;i++){mDelay((pSIZE+mSIZE)/2);printf(">");}printf("\nFinish.\n载入成功,按任意键进入置换算法选择界面:>>>");getch();}/*设置延迟*/void mDelay(unsigned int Delay){unsigned int i;for(;Delay>0;Delay--){for(i=0;i<124;i++){printf(" \b");}}}/*显示设计者信息*/void designBy(){printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");printf("┃课题三:页面置换算法┃\n");printf("┃学号:20111214034 ┃\n");printf("┃姓名:韩瑶┃\n");printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━┫\n"); }void print(unsigned int t){int i,j,k,l;int flag;for(k=0;k<=(pSIZE-1)/20;k++){for(i=20*k;(i<pSIZE)&&(i<20*(k+1));i++){if(((i+1)%20==0)||(((i+1)%20)&&(i==pSIZE-1)))printf("%d\n",page[i]);elseprintf("%d ",page[i]);}for(j=0;j<mSIZE;j++){for(i=20*k;(i<mSIZE+20*k)&&(i<pSIZE);i++){if(i>=j)printf(" |%d|",temp[i][j]);elseprintf(" | |");}for(i=mSIZE+20*k;(i<pSIZE)&&(i<20*(k+1));i++){for(flag=0,l=0;l<mSIZE;l++)if(temp[i][l]==temp[i-1][l])flag++;if(flag==mSIZE)/*页面在物理块中*/printf(" ");elseprintf(" |%d|",temp[i][j]);}/*每行显示20个*/if(i%20==0)continue;printf("\n");}}printf("----------------------------------------\n");printf("缺页次数:%d\t\t",t+mSIZE);printf("缺页率:%d/%d\n",t+mSIZE,pSIZE);printf("置换次数:%d\t\t",t);printf("访问命中率:%d%%\n",(pSIZE-(t+mSIZE))*100/pSIZE);printf("----------------------------------------\n");}/*计算过程延迟*/void compute(){int i;printf("正在进行相关计算,请稍候");for(i=1;i<20;i++){mDelay(15);if(i%4==0)printf("\b\b\b\b\b\b \b\b\b\b\b\b");elseprintf("Θ");}for(i=0;i++<30;printf("\b"));for(i=0;i++<30;printf(" "));for(i=0;i++<30;printf("\b"));}/*先进先出页面置换算法*/void FIFO(){int memery[10]={0};int time[10]={0}; /*记录进入物理块的时间*/int i,j,k,m;int max=0; /*记录换出页*/int count=0; /*记录置换次数*//*前mSIZE个数直接放入*/for(i=0;i<mSIZE;i++){memery[i]=page[i];time[i]=i;for(j=0;j<mSIZE;j++)temp[i][j]=memery[j];}for(i=mSIZE;i<pSIZE;i++){/*判断新页面号是否在物理块中*/for(j=0,k=0;j<mSIZE;j++){if(memery[j]!=page[i])k++;}if(k==mSIZE) /*如果不在物理块中*/{count++;/*计算换出页*/max=time[0]<time[1]?0:1;for(m=2;m<mSIZE;m++)if(time[m]<time[max])max=m;memery[max]=page[i];time[max]=i; /*记录该页进入物理块的时间*/for(j=0;j<mSIZE;j++)temp[i][j]=memery[j];}else{for(j=0;j<mSIZE;j++)temp[i][j]=memery[j];}}compute();print(count);}/*最近最久未使用置换算法*/void LRU(){int memery[10]={0};int flag[10]={0}; /*记录页面的访问时间*/int i,j,k,m;int max=0; /*记录换出页*/int count=0; /*记录置换次数*//*前mSIZE个数直接放入*/for(i=0;i<mSIZE;i++){memery[i]=page[i];flag[i]=i;for(j=0;j<mSIZE;j++)temp[i][j]=memery[j];}for(i=mSIZE;i<pSIZE;i++){/*判断新页面号是否在物理块中*/for(j=0,k=0;j<mSIZE;j++){if(memery[j]!=page[i])k++;elseflag[j]=i; /*刷新该页的访问时间*/ }if(k==mSIZE) /*如果不在物理块中*/{count++;/*计算换出页*/max=flag[0]<flag[1]?0:1;for(m=2;m<mSIZE;m++)if(flag[m]<flag[max])max=m;memery[max]=page[i];flag[max]=i; /*记录该页的访问时间*/for(j=0;j<mSIZE;j++)temp[i][j]=memery[j];}else{for(j=0;j<mSIZE;j++)temp[i][j]=memery[j];}}compute();print(count);}/*最佳置换算法*/void OPT(){int memery[10]={0};int next[10]={0}; /*记录下一次访问时间*/int i,j,k,l,m;int max; /*记录换出页*/int count=0; /*记录置换次数*//*前mSIZE个数直接放入*/for(i=0;i<mSIZE;i++){memery[i]=page[i];for(j=0;j<mSIZE;j++)temp[i][j]=memery[j];}for(i=mSIZE;i<pSIZE;i++){/*判断新页面号是否在物理块中*/for(j=0,k=0;j<mSIZE;j++){if(memery[j]!=page[i])k++;}if(k==mSIZE) /*如果不在物理块中*/{count++;/*得到物理快中各页下一次访问时间*/for(m=0;m<mSIZE;m++){for(l=i+1;l<pSIZE;l++)if(memery[m]==page[l])break;next[m]=l;}/*计算换出页*/max=next[0]>=next[1]?0:1;for(m=2;m<mSIZE;m++)if(next[m]>next[max])max=m;/*下一次访问时间都为pSIZE,则置换物理块中第一个*/memery[max]=page[i];for(j=0;j<mSIZE;j++)temp[i][j]=memery[j];}else {for(j=0;j<mSIZE;j++)temp[i][j]=memery[j];}}compute();print(count);}6. 程序运行时的初值和运行结果1. 按任意键进行初始化:2. 载入数据:3. 进入置换算法选择界面:4.运算中延迟操作5.三种算法演示结果:四、实验体会掌握了一般的调度算法,了解了页面大小和内存实际容量对命中率的影响(英文版)Two regulations promulgated for implementation is in the party in power for a long time and the rule of law conditions, the implementation of comprehensive strictly strategic plan, implementation in accordance with the rules and discipline to manage the party, strengthen inner-party supervision of major initiatives. The two regulations supporting each other, the < code > adhere to a positive advocate, focusing on morality is of Party members and Party leading cadres can see, enough to get a high standard; < rule > around the party discipline, disciplinary ruler requirements, listed as "negative list, focusing on vertical gauge, draw the party organizations and Party members do not touch the" bottom line ". Here, the main from four square face two party rules of interpretation: the first part introduces two party Revised regulations the necessity and the revision process; the second part is the interpretation of the two fundamental principles of the revision of laws and regulations in the party; the third part introduces two party regulations modified the main changes and needs to grasp several key problems; the fourth part on how to grasp the implementation of the two regulations of the party. < code > and < Regulations > revised the necessity and revisedhistory of the CPC Central Committee the amendment to the Chinese Communist Party members and leading cadres honest politics several guidelines > and < Chinese Communist Party discipline and Punishment Regulations > column 1 by 2015 to strengthen party laws and regulations focus. Two party regulations revision work lasted a Years, pooling the wisdom of the whole party, ideological consensus, draw historical experience, respect for the wisdom of our predecessors, which reflects the unity of inheritance and innovation; follow the correct direction, grasp the limited goals, adhere to the party's leadership, to solve the masses of the people reflect a focus on the problem. The new revision of the < code > and < rule >, reflects the party's 18 and the eighth session of the third, the spirit of the fourth plenary session, reflecting the experience of studying and implementing the General Secretary Xi Jinping series of important speech, reflects the party's eighteen years comprehensive strictly practice. (a) revised two regulations of the party need of < the ICAC guidelines > in < in 1997 Leaders as members of the Communist Party of China clean politics certain criteria (Trial) > based on revised, the promulgation and implementation of January 2010, to strengthen the construction of the contingent of leading cadres play animportant role. But with the party to manage the party strictly administering the deepening, has not been able to fully meet the actual needs. Content is too complicated, "eight prohibition, 52 are not allowed to" hard to remember, and also difficult to put into practice; the second is concisely positive advocated by the lack of prohibited provisions excessive, no autonomy requirements; the third is banned terms and discipline law, both with the party discipline, disciplinary regulations repeat and Criminal law and other laws and regulations repeat; the fourth is to "clean" the theme is not prominent, not for the existing problems, and is narrow, only needle of county-level leading cadres above. < rule > is in 1997 < Chinese Communist Party disciplinary cases (Trial) > based on revision, in December 2003 the promulgation and implementation, to strengthen the construction of the party play very important role. Along with the development of the situation, which many provisions have been unable to fully meet the comprehensive strictly administering the practice needs. One is Ji law, more than half of the provisions and criminal law and other countries laws and regulations Repetition; two is the political discipline regulations is not prominent, not specific, for violation of the party constitution, damage theauthority of Party Constitution of misconduct lack necessary and serious responsibility to pursue; third is the main discipline for the leading cadres, does not cover all Party members. Based on the above situation, need to < the criterion of a clean and honest administration > and < rule > the two is likely to be more relevant regulations first amendment. By revising, really put the authority of Party discipline, the seriousness in the party tree and call up the majority of Party members and cadres of the party constitution of party compasses party consciousness. (II) two party regulations revision process the Central Committee of the Communist Party of China attaches great importance to two regulations revision . Xi Jinping, general books recorded in the Fifth Plenary Session of the eighth session of the Central Commission for Discipline Inspection, on the revised regulations < > made clear instructions. According to the central deployment, the Central Commission for Discipline Inspection from 2014 under six months begin study two regulations revision. The Standing Committee of the Central Commission for Discipline Inspection 4 review revised. Comrade Wang Qishan 14 times held a special meeting to study two regulations revision, amendment clarifies the direction, major issues of principle, path and target,respectively held a forum will listen to part of the province (area) secretary of the Party committee, Secretary of the Discipline Inspection Commission, part of the central ministries and state organs DepartmentThe first party committee is mainly responsible for people, views of experts and scholars and grassroots party organizations and Party members. Approved by the Central Committee of the Communist Party of China, on 7 September 2015, the general office of the Central Committee of the Party issued a notice to solicit the provinces (autonomous regions, municipalities) Party, the central ministries and commissions, state ministries and commissions of the Party (party), the General Political Department of the military, every 3 people organization of Party of two regulations revision opinion. Central Commission for Discipline Inspection of extensive solicitation of opinions, careful study, attracting, formed a revised sent reviewers. In October 8 and October 12, Central Committee Political Bureau Standing Committee and the Political Bureau of the Central Committee After consideration of the two regulations revised draft. On October 18, the Central Committee of the Communist Party of China formally issued two regulations. Can say, two laws amendment concentrated thewisdom of the whole party, embodies the party. Second, < code > and < Regulations > revision of the basic principles of two party regulations revision work and implement the party's eighteen, ten eight plenary, the spirit of the Fourth Plenary Session of the Eleventh Central Committee and General Secretary Xi Jinping important instructions on the revised < low political criterion > and < Regulations >, highlighting the ruling party characteristics, serious discipline, the discipline quite in front of the law, based on the current, a long-term, advance as a whole, with Bu Xiuding independent < rule > and < rule >. Main principle is: first, adhere to the party constitution to follow. The constitution about discipline and self-discipline required specific, awaken the party constitution of party compasses party consciousness, maintaining the authority of the constitution. General Secretary Xi Jinping pointed out that "no rules, no side round. Party constitution is the fundamental law, the party must follow the general rules. In early 2015 held the eighth session of the Central Commission for Discipline Inspection Fifth Plenary Session of the 16th Central Committee, Xi Jinping again pointed out that constitution is the party must follow the general rules, but also the general rules." the revision of the< code > and < rule > is Method in adhere to the regulations established for the purpose of combining rule of virtue is to adhere to the party constitution as a fundamental to follow, the constitution authority set up, wake up the party constitution and party rules the sense of discipline, the party constitution about discipline and self-discipline specific requirements. 4 second is to adhere to in accordance with the regulations governing the party and the party. The Party of rule of virtue "de", mainly refers to the party's ideals and beliefs, excellent traditional style. The revised the < code > closely linked to the "self-discipline", insisting on the positive initiative, for all members, highlight the "vital few", emphasized self-discipline, focusing on the morality, and the majority of Party members and the ideological and moral standards. The revised < > Ji method separately, Ji, Ji Yan to Method, as a "negative list", emphasizing the heteronomy, focusing on vertical gauge. Is this one high and one low, a positive reaction, the strict party discipline and practice results transformation for the integration of the whole party to observe moral and discipline requirements, for the majority of Party members and cadres provides benchmarking and ruler. Third, insist on to. In view of the problems existing in theparty at the present stage, the main problems of Party members and cadres in the aspect of self-discipline and abide by the discipline to make clearly defined, especially the party's eighteen years strict political discipline and political rules, organization and discipline and to implement the central eight provisions of the spirit against the four winds and other requirements into Disciplinary provisions. Not one pace reachs the designated position, focusing on in line with reality, pragmatic and effective. After the revision of major changes, major changes in the < code > and < rule > modified and needs to grasp several key problems (a) < code > < code > adhere to according to regulations governing the party and party with morals in combination, for at the present stage, the leadership of the party members and cadres and Party members in existing main problems of self-discipline, put forward principles, requirements and specifications, showing Communists noble moral pursuit, reflected at all times and in all over the world ethics from high from low 5 common requirements. One is closely linked to the "self-discipline", removal and no direct relation to the provisions of . the second is adhere to a positive advocate, "eight prohibition" 52 are not allowed to "about the content of the" negative list moved into synchronizationamendment < cases >. Three is for all the party members, will apply object from the leadership of the party members and cadres to expand to all Party members, fully embodies the comprehensive strictly required. The fourth is prominent key minority, seize the leadership of the party members and cadres is the key, and put forward higher requirements than the ordinary Party members. Five is to simplify, and strive to achieve concise, easy to understand, easy to remember. The revised < code > is the ruling Party since the first insists on a positive advocate forAll Party members and the self-discipline norms, moral declaration issued to all members of the party and the National People's solemn commitment. > < criterion of a clean and honest administration consists of 4 parts, 18, more than 3600 words. After the revision of the < code >, a total of eight, 281 words, including lead, specification and Party member cadre clean fingered self-discipline norms, etc. Part 3 members low-cost clean and self-discipline, the main contents can be summarized as "four must" "eight code". Lead part, reiterated on ideal and faith, fundamental purpose, the fine traditions and work style, noble sentiments, such as "four must" the principle of requirements, strong tone of self-discipline, The higher request for 6 andsupervised tenet, the foothold in permanent Bao the party's advanced nature and purity, to reflect the revised standards requirements. Members of self-discipline norms around the party members how to correctly treat and deal with the "public and private", "cheap and rot" thrifty and extravagance "bitter music", put forward the "four norms". Party leader cadre clean fingered self-discipline norms for the leadership of the party members and cadres of the "vital few", around the "clean politics", from civil servant of the color, the exercise of power, moral integrity, a good family tradition and other aspects of the leadership of the party members and cadres of the "four norms" < > < norm norm. "The Party member's self-discipline norms" and "party members and leading cadre clean fingered self-discipline norms," a total of eight, collectively referred to as the "eight". "Four must" and "eight" of the content from the party constitution and Party's several generation of leaders, especially Xi Jinping, general secretary of the important discussion, refer to the "three discipline and eight points for attention" statements, and reference some embody the Chinese nation excellent traditional culture essence of epigrams. (2) the revised regulations, the main changes in the revised Regulations > to fully adapt to thestrictly requirements, reflects the according to the regulations governing the law of recognition of deepening, the realization of the discipline construction and Jin Ju. < rule > is party a ruler, members of the basic line and follow. And the majority of Party members and cadres of Party organizations at all levels should adhere to the bottom line of thinking, fear discipline, hold the bottom line, as a preventive measure, to keep the party's advanced nature and purity. 1, respect for the constitution, refinement and discipline. Revised < rule > from comprehensive comb physical constitution began, the party constitution and other regulations of the Party of Party organizations and Party discipline requirements refinement, clearly defined in violation of the party constitution will be in accordance with regulations to give the corresponding disciplinary action. The original 10 categories of misconduct, integration specification for political discipline, discipline, honesty and discipline masses Ji Law and discipline and discipline and other six categories, the content of < rule > real return to Party discipline, for the majority of Party members and listed a "negative list. 7 2, highlighting the political discipline and political rules. > < Regulations according to the stage of the discipline of outstandingperformance, emphasizing political discipline and political rules, organization and discipline, in opposition to the party's leadership and the party's basic theory, basic line, basic program and basic experience, the basic requirement of behavior made prescribed punishment, increase the cliques, against the organization such as violation of the provisions, to ensure that the central government decrees and the Party of centralized and unified. 3, adhere to strict discipline in the law and discipline In front, Ji separated. Revised < Regulations > adhere to the problem oriented, do Ji separated. Any national law existing content, will not repeat the provisions, the total removal of 79 and criminal law, repeat the content of the public security management punishment law, and other laws and regulations. In the general reiterated that party organizations and Party members must conscientiously accept the party's discipline, die van comply with national laws and regulations; at the same time, to investigate violations of Party members and even criminal behavior of Party discipline and responsibility, > < Regulations distinguish five different conditions, with special provisions were made provisions, so as to realize the connection of Party discipline and state law. 4, reflect Wind building and anti-corruptionstruggle of the latest achievements. < rule > the party's eighteen years implement the spirit of the central provisions of the eight, against the requirements of the "four winds" and transformation for disciplinary provisions, reflecting the style construction is always on the road, not a gust of wind. In the fight against corruption out of new problems, increase the trading rights, the use of authority relatives profit and other disciplinary terms. Prominent discipline of the masses, the new against the interests of the masses and ignore the demands of the masses and other disciplinary terms and make provisions of the disposition and the destruction of the party's close ties with the masses.Discipline to protect the party's purpose. 8 of these regulations, a total of three series, Chapter 15, 178, more than 24000 words, after the revision of the regulations a total of 3 series, Chapter 11, 133, 17000 words, divided into "general" and "special provisions" and "Supplementary Provisions" Part 3. Among them, add, delete, modify the provisions of the proportion of up to nearly 90%. 1, the general general is divided into five chapters. The first chapter to the regulations of the guiding ideology, principles and scope of application of the provisions, highlight the strengthening ofthe party constitution consciousness, maintenance the authority of Party Constitution, increase the party organizations and Party members must abide by the party constitution, Yan Centralized centralized, would examine at all levels of the amended provisions implementing and maintaining Party discipline, and consciously accept the party discipline, exemplary compliance with national laws and regulations. The second chapter of discipline concept, disciplinary action types and effects of the regulations, will be a serious warning from the original a year for a year and a half; increase the Party Congress representative, by leaving the party above (including leave probation) punishment, the party organization should be terminated its representative qualification provisions. The third chapter of the disciplinary rules of use prescribed in the discipline rectifying process, non convergence, not close hand classified as severely or heavier punishment. "Discipline straighten "At least eighteen years of five years, these five years is to pay close attention to the provisions of the central eight implementation and anti -" four winds ". The fourth chapter on suspicion of illegal party disciplinary distinguish five different conditions, with special provisions were madeprovisions, to achieve effective convergence of Party and country 9 method. < rule > the provisions of Article 27, Party organizations in the disciplinary review found that party members have committed embezzlement, bribery, dereliction of duty dereliction of duty and other criminal law act is suspected of committing a crime shall give cancel party posts, probation or expelled from the party. The second is < Regulations > Article 28 the provisions of Party organizations in the disciplinary review But found that party members are stipulated in the criminal law, although not involved in a crime shall be investigated for Party discipline and responsibility should be depending on the specific circumstances shall be given a warning until expelled punishment. This situation and a difference is that the former regulation behavior has been suspected of a crime, the feeling is quite strict, and the latter for the behavior not involving crime, only the objective performance of the provisions of the criminal code of behavior, but the plot is a crime to slightly. < Regulations > the 29 provisions, Party organizations in the discipline review found that party members and other illegal behavior, affect the party's image, the damage to the party, the state and the people's interests, we should depend on the situation。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验七存储管理---------常用页面置换算法模拟实验实验目的通过模拟实现请求页式存储管理的几种基本页面置换算法,了解虚拟存储技术的特点,掌握虚拟存储请求页式存储管理中几种基本页面置换算法的基本思想和实现过程,并比较它们的效率。

实验内容设计一个虚拟存储区和内存工作区,并使用下述算法计算访问命中率。

1、最佳淘汰算法(OPT)2、先进先出的算法(FIFO)3、最近最久未使用算法(LRU)4、最不经常使用算法(LFU)5、最近未使用算法(NUR)命中率=1-页面失效次数/页地址流长度实验准备本实验的程序设计基本上按照实验内容进行。

即首先用srand( )和rand( )函数定义和产生指令序列,然后将指令序列变换成相应的页地址流,并针对不同的算法计算出相应的命中率。

(1)通过随机数产生一个指令序列,共320条指令。

指令的地址按下述原则生成:A:50%的指令是顺序执行的B:25%的指令是均匀分布在前地址部分C:25%的指令是均匀分布在后地址部分具体的实施方法是:A:在[0,319]的指令地址之间随机选取一起点mB:顺序执行一条指令,即执行地址为m+1的指令C:在前地址[0,m+1]中随机选取一条指令并执行,该指令的地址为m’D:顺序执行一条指令,其地址为m’+1E:在后地址[m’+2,319]中随机选取一条指令并执行F:重复步骤A-E,直到320次指令(2)将指令序列变换为页地址流设:页面大小为1K;用户内存容量4页到32页;用户虚存容量为32K。

在用户虚存中,按每K存放10条指令排列虚存地址,即320条指令在虚存中的存放方式为:第0 条-第9 条指令为第0页(对应虚存地址为[0,9])第10条-第19条指令为第1页(对应虚存地址为[10,19])………………………………第310条-第319条指令为第31页(对应虚存地址为[310,319])按以上方式,用户指令可组成32页。

实验指导一、虚拟存储系统UNIX中,为了提高内存利用率,提供了内外存进程对换机制;内存空间的分配和回收均以页为单位进行;一个进程只需将其一部分(段或页)调入内存便可运行;还支持请求调页的存储管理方式。

当进程在运行中需要访问某部分程序和数据时,发现其所在页面不在内存,就立即提出请求(向CPU发出缺中断),由系统将其所需页面调入内存。

这种页面调入方式叫请求调页。

为实现请求调页,核心配置了四种数据结构:页表、页框号、访问位、修改位、有效位、保护位等。

二、页面置换算法当CPU接收到缺页中断信号,中断处理程序先保存现场,分析中断原因,转入缺页中断处理程序。

该程序通过查找页表,得到该页所在外存的物理块号。

如果此时内存未满,能容纳新页,则启动磁盘I/O将所缺之页调入内存,然后修改页表。

如果内存已满,则须按某种置换算法从内存中选出一页准备换出,是否重新写盘由页表的修改位决定,然后将缺页调入,修改页表。

利用修改后的页表,去形成所要访问数据的物理地址,再去访问内存数据。

整个页面的调入过程对用户是透明的。

常用的页面置换算法有1、最佳置换算法(Optimal)2、先进先出法(Fisrt In First Out)3、最近最久未使用(Least Recently Used)4、最不经常使用法(Least Frequently Used)5、最近未使用法(No Used Recently)三、参考程序:view plaincopy to clipboardprint? .........10........20........30........40........50........60........70........80........90.. (100).......110.......120.......130.......140. (150)#define TRUE 1#define FALSE 0#define INV ALID -1#define NULL 0#define total_instruction 320 /*指令流长*/#define total_vp 32 /*虚页长*/#define clear_period 50 /*清0周期*/typedef struct /*页面结构*/{int pn,pfn,counter,time;}pl_type;pl_type pl[total_vp]; /*页面结构数组*/struct pfc_struct{ /*页面控制结构*/int pn,pfn;struct pfc_struct *next;};typedef struct pfc_struct pfc_type;pfc_type pfc[total_vp],*freepf_head,*busypf_head,*busypf_tail;int diseffect, a[total_instruction];int page[total_instruction], offset[total_instruction];int initialize(int);int FIFO(int);int LRU(int);int LFU(int);int NUR(int);int OPT(int);int main( ){int s,i,j;srand(10*getpid()); /*由于每次运行时进程号不同,故可用来作为初始化随机数队列的“种子”*/s=(float)319*rand( )/32767/32767/2+1; //for(i=0;i<total_instruction;i+=4) /*产生指令队列*/{if(s<0||s>319){printf("When i==%d,Error,s==%d\n",i,s);exit(0);}a[i]=s; /*任选一指令访问点m*/a[i+1]=a[i]+1; /*顺序执行一条指令*/a[i+2]=(float)a[i]*rand( )/32767/32767/2; /*执行前地址指令m' */a[i+3]=a[i+2]+1; /*顺序执行一条指令*/s=(float)(318-a[i+2])*rand( )/32767/32767/2+a[i+2]+2;if((a[i+2]>318)||(s>319))printf("a[%d+2],a number which is :%d and s==%d\n",i,a[i+2],s);}for (i=0;i<total_instruction;i++) /*将指令序列变换成页地址流*/{page[i]=a[i]/10;offset[i]=a[i]%10;}for(i=4;i<=32;i++) /*用户内存工作区从4个页面到32个页面*/{printf("---%2d page frames---\n",i);FIFO(i);LRU(i);LFU(i);NUR(i);OPT(i);}return 0;}int initialize(total_pf) /*初始化相关数据结构*/int total_pf; /*用户进程的内存页面数*/{int i;diseffect=0;for(i=0;i<total_vp;i++){pl[i].pn=i;pl[i].pfn=INV ALID; /*置页面控制结构中的页号,页面为空*/ pl[i].counter=0;pl[i].time=-1; /*页面控制结构中的访问次数为0,时间为-1*/ }for(i=0;i<total_pf-1;i++){pfc[i].next=&pfc[i+1];pfc[i].pfn=i;} /*建立pfc[i-1]和pfc[i]之间的链接*/pfc[total_pf-1].next=NULL;pfc[total_pf-1].pfn=total_pf-1;freepf_head=&pfc[0]; /*空页面队列的头指针为pfc[0]*/return 0;}int FIFO(total_pf) /*先进先出算法*/int total_pf; /*用户进程的内存页面数*/{int i,j;pfc_type *p;initialize(total_pf); /*初始化相关页面控制用数据结构*/busypf_head=busypf_tail=NULL; /*忙页面队列头,队列尾链接*/for(i=0;i<total_instruction;i++){if(pl[page[i]].pfn==INV ALID) /*页面失效*/{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; /*按FIFO方式调新页面入内存页面*/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; /*free页面减少一个*/busypf_tail=freepf_head;}freepf_head=p;}}printf("FIFO:%6.4f\n",1-(float)diseffect/320);return 0;}int LRU (total_pf) /*最近最久未使用算法*/int total_pf;{int min,minj,i,j,present_time;initialize(total_pf);present_time=0;for(i=0;i<total_instruction;i++){if(pl[page[i]].pfn==INV ALID) /*页面失效*/{diseffect++;if(freepf_head==NULL) /*无空闲页面*/{min=32767;for(j=0;j<total_vp;j++) /*找出time的最小值*/if(min>pl[j].time&&pl[j].pfn!=INV ALID){min=pl[j].time;minj=j;}freepf_head=&pfc[pl[minj].pfn]; //腾出一个单元pl[minj].pfn=INV ALID;pl[minj].time=-1;freepf_head->next=NULL;}pl[page[i]].pfn=freepf_head->pfn; //有空闲页面,改为有效pl[page[i]].time=present_time;freepf_head=freepf_head->next; //减少一个free 页面}elsepl[page[i]].time=present_time; //命中则增加该单元的访问次数present_time++;}printf("LRU:%6.4f\n",1-(float)diseffect/320);return 0;}int NUR(total_pf) /*最近未使用算法*/int total_pf;{ int i,j,dp,cont_flag,old_dp;pfc_type *t;initialize(total_pf);dp=0;for(i=0;i<total_instruction;i++){ if (pl[page[i]].pfn==INV ALID) /*页面失效*/{diseffect++;if(freepf_head==NULL) /*无空闲页面*/{ cont_flag=TRUE;old_dp=dp;while(cont_flag)if(pl[dp].counter==0&&pl[dp].pfn!=INV ALID)cont_flag=FALSE;else{dp++;if(dp==total_vp)dp=0;if(dp==old_dp)for(j=0;j<total_vp;j++)pl[j].counter=0;}freepf_head=&pfc[pl[dp].pfn];pl[dp].pfn=INV ALID;freepf_head->next=NULL;}pl[page[i]].pfn=freepf_head->pfn;freepf_head=freepf_head->next;}elsepl[page[i]].counter=1;if(i%clear_period==0)for(j=0;j<total_vp;j++)pl[j].counter=0;}printf("NUR:%6.4f\n",1-(float)diseffect/320);return 0;}int OPT(total_pf) /*最佳置换算法*/int total_pf;{int i,j, max,maxpage,d,dist[total_vp];pfc_type *t;initialize(total_pf);for(i=0;i<total_instruction;i++){ //printf("In OPT for 1,i=%d\n",i); //i=86;i=176;206;250;220,221;192,193,194;258;274,275,276,277,278;if(pl[page[i]].pfn==INV ALID) /*页面失效*/{diseffect++;if(freepf_head==NULL) /*无空闲页面*/{for(j=0;j<total_vp;j++)if(pl[j].pfn!=INV ALID) dist[j]=32767; /* 最大"距离" */else dist[j]=0;d=1;for(j=i+1;j<total_instruction;j++){if(pl[page[j]].pfn!=INV ALID)dist[page[j]]=d;d++;}max=-1;for(j=0;j<total_vp;j++)if(max<dist[j]){max=dist[j];maxpage=j;}freepf_head=&pfc[pl[maxpage].pfn];freepf_head->next=NULL;pl[maxpage].pfn=INV ALID;}pl[page[i]].pfn=freepf_head->pfn;freepf_head=freepf_head->next;}}printf("OPT:%6.4f\n",1-(float)diseffect/320);return 0;}int LFU(total_pf) /*最不经常使用置换法*/int total_pf;{int i,j,min,minpage;pfc_type *t;initialize(total_pf);for(i=0;i<total_instruction;i++){ if(pl[page[i]].pfn==INV ALID) /*页面失效*/ { diseffect++;if(freepf_head==NULL) /*无空闲页面*/{ min=32767;for(j=0;j<total_vp;j++){if(min>pl[j].counter&&pl[j].pfn!=INV ALID){min=pl[j].counter;minpage=j;}pl[j].counter=0;}freepf_head=&pfc[pl[minpage].pfn];pl[minpage].pfn=INV ALID;freepf_head->next=NULL;}pl[page[i]].pfn=freepf_head->pfn; //有空闲页面,改为有效pl[page[i]].counter++;freepf_head=freepf_head->next; //减少一个free 页面}elsepl[page[i]].counter++;}printf("LFU:%6.4f\n",1-(float)diseffect/320);return 0;}#define TRUE 1#define FALSE 0#define INV ALID -1#define NULL 0#define total_instruction 320 /*指令流长*/#define total_vp 32 /*虚页长*/#define clear_period 50 /*清0周期*/typedef struct /*页面结构*/{int pn,pfn,counter,time;}pl_type;pl_type pl[total_vp]; /*页面结构数组*/struct pfc_struct{ /*页面控制结构*/ int pn,pfn;struct pfc_struct *next;};typedef struct pfc_struct pfc_type;pfc_type pfc[total_vp],*freepf_head,*busypf_head,*busypf_tail;int diseffect, a[total_instruction];int page[total_instruction], offset[total_instruction];int initialize(int);int FIFO(int);int LRU(int);int LFU(int);int NUR(int);int OPT(int);int main( ){int s,i,j;srand(10*getpid()); /*由于每次运行时进程号不同,故可用来作为初始化随机数队列的“种子”*/s=(float)319*rand( )/32767/32767/2+1; //for(i=0;i<total_instruction;i+=4) /*产生指令队列*/{if(s<0||s>319){printf("When i==%d,Error,s==%d\n",i,s);exit(0);}a[i]=s; /*任选一指令访问点m*/a[i+1]=a[i]+1; /*顺序执行一条指令*/a[i+2]=(float)a[i]*rand( )/32767/32767/2; /*执行前地址指令m' */a[i+3]=a[i+2]+1; /*顺序执行一条指令*/s=(float)(318-a[i+2])*rand( )/32767/32767/2+a[i+2]+2;if((a[i+2]>318)||(s>319))printf("a[%d+2],a number which is :%d and s==%d\n",i,a[i+2],s);}for (i=0;i<total_instruction;i++) /*将指令序列变换成页地址流*/{page[i]=a[i]/10;offset[i]=a[i]%10;}for(i=4;i<=32;i++) /*用户内存工作区从4个页面到32个页面*/{printf("---%2d page frames---\n",i);FIFO(i);LRU(i);LFU(i);NUR(i);OPT(i);}return 0;}int initialize(total_pf) /*初始化相关数据结构*/int total_pf; /*用户进程的内存页面数*/{int i;diseffect=0;for(i=0;i<total_vp;i++){pl[i].pn=i;pl[i].pfn=INV ALID; /*置页面控制结构中的页号,页面为空*/pl[i].counter=0;pl[i].time=-1; /*页面控制结构中的访问次数为0,时间为-1*/}for(i=0;i<total_pf-1;i++){pfc[i].next=&pfc[i+1];pfc[i].pfn=i;} /*建立pfc[i-1]和pfc[i]之间的链接*/pfc[total_pf-1].next=NULL;pfc[total_pf-1].pfn=total_pf-1;freepf_head=&pfc[0]; /*空页面队列的头指针为pfc[0]*/return 0;}int FIFO(total_pf) /*先进先出算法*/int total_pf; /*用户进程的内存页面数*/{int i,j;pfc_type *p;initialize(total_pf); /*初始化相关页面控制用数据结构*/busypf_head=busypf_tail=NULL; /*忙页面队列头,队列尾链接*/for(i=0;i<total_instruction;i++){if(pl[page[i]].pfn==INV ALID) /*页面失效*/{diseffect+=1; /*失效次数*/if(freepf_head==NULL) /*无空闲页面*/{p=busypf_head->next;pl[busypf_head->pn].pfn=INV ALID;freepf_head=busypf_head; /*释放忙页面队列的第一个页面*/freepf_head->next=NULL;busypf_head=p;}p=freepf_head->next; /*按FIFO方式调新页面入内存页面*/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; /*free页面减少一个*/busypf_tail=freepf_head;}freepf_head=p;}}printf("FIFO:%6.4f\n",1-(float)diseffect/320);return 0;}int LRU (total_pf) /*最近最久未使用算法*/int total_pf;{int min,minj,i,j,present_time;initialize(total_pf);present_time=0;for(i=0;i<total_instruction;i++){if(pl[page[i]].pfn==INV ALID) /*页面失效*/{diseffect++;if(freepf_head==NULL) /*无空闲页面*/{min=32767;for(j=0;j<total_vp;j++) /*找出time的最小值*/if(min>pl[j].time&&pl[j].pfn!=INV ALID){min=pl[j].time;minj=j;}freepf_head=&pfc[pl[minj].pfn]; //腾出一个单元pl[minj].pfn=INV ALID;pl[minj].time=-1;freepf_head->next=NULL;}pl[page[i]].pfn=freepf_head->pfn; //有空闲页面,改为有效pl[page[i]].time=present_time;freepf_head=freepf_head->next; //减少一个free 页面}elsepl[page[i]].time=present_time; //命中则增加该单元的访问次数present_time++;}printf("LRU:%6.4f\n",1-(float)diseffect/320);return 0;}int NUR(total_pf) /*最近未使用算法*/int total_pf;{ int i,j,dp,cont_flag,old_dp;pfc_type *t;initialize(total_pf);dp=0;for(i=0;i<total_instruction;i++){ if (pl[page[i]].pfn==INV ALID) /*页面失效*/{diseffect++;if(freepf_head==NULL) /*无空闲页面*/{ cont_flag=TRUE;old_dp=dp;while(cont_flag)if(pl[dp].counter==0&&pl[dp].pfn!=INV ALID)cont_flag=FALSE;else{dp++;if(dp==total_vp)dp=0;if(dp==old_dp)for(j=0;j<total_vp;j++)pl[j].counter=0;}freepf_head=&pfc[pl[dp].pfn];pl[dp].pfn=INV ALID;freepf_head->next=NULL;}pl[page[i]].pfn=freepf_head->pfn;freepf_head=freepf_head->next;}elsepl[page[i]].counter=1;if(i%clear_period==0)for(j=0;j<total_vp;j++)pl[j].counter=0;}printf("NUR:%6.4f\n",1-(float)diseffect/320);return 0;}int OPT(total_pf) /*最佳置换算法*/int total_pf;{int i,j, max,maxpage,d,dist[total_vp];pfc_type *t;initialize(total_pf);for(i=0;i<total_instruction;i++){ //printf("In OPT for 1,i=%d\n",i); //i=86;i=176;206;250;220,221;192,193,194;258;274,275,276,277,278;if(pl[page[i]].pfn==INV ALID) /*页面失效*/{diseffect++;if(freepf_head==NULL) /*无空闲页面*/{for(j=0;j<total_vp;j++)if(pl[j].pfn!=INV ALID) dist[j]=32767; /* 最大"距离" */else dist[j]=0;d=1;for(j=i+1;j<total_instruction;j++){if(pl[page[j]].pfn!=INV ALID)dist[page[j]]=d;d++;}max=-1;for(j=0;j<total_vp;j++)if(max<dist[j]){max=dist[j];maxpage=j;}freepf_head=&pfc[pl[maxpage].pfn];freepf_head->next=NULL;pl[maxpage].pfn=INVALID;}pl[page[i]].pfn=freepf_head->pfn;freepf_head=freepf_head->next;}}printf("OPT:%6.4f\n",1-(float)diseffect/320);return 0;}int LFU(total_pf) /*最不经常使用置换法*/int total_pf;{int i,j,min,minpage;pfc_type *t;initialize(total_pf);for(i=0;i<total_instruction;i++){ if(pl[page[i]].pfn==INV ALID) /*页面失效*/{ diseffect++;if(freepf_head==NULL) /*无空闲页面*/{ min=32767;for(j=0;j<total_vp;j++){if(min>pl[j].counter&&pl[j].pfn!=INVALID){min=pl[j].counter;minpage=j;}pl[j].counter=0;}freepf_head=&pfc[pl[minpage].pfn];pl[minpage].pfn=INV ALID;freepf_head->next=NULL;}pl[page[i]].pfn=freepf_head->pfn; //有空闲页面,改为有效pl[page[i]].counter++;freepf_head=freepf_head->next; //减少一个free 页面}elsepl[page[i]].counter++;}printf("LFU:%6.4f\n",1-(float)diseffect/320);return 0;}五、分析1、从几种算法的命中率看,OPT最高,其次为NUR相对较高,而FIFO与LRU相差无几,最低的是LFU。

相关文档
最新文档