请求调页存储管理方式的模拟NRU

合集下载

页式虚拟存储管理FIFO、LRU和OPT页面置换算法

页式虚拟存储管理FIFO、LRU和OPT页面置换算法

目录1 需求分析 (2)1.1 目的和要求 (2)1.2 研究内容 (2)2 概要设计 (2)2.1 FIFO算法 (3)2.2 LRU算法 (3)2.3 OPT算法 (3)2.4 输入新的页面引用串 (3)3 详细设计 (4)3.1 FIFO(先进先出)页面置换算法: (4)3.2 LRU(最近最久未使用)置换算法: (4)3.3 OPT(最优页)置换算法 (4)4 测试 (5)5 运行结果 (5)6 课程设计总结 (9)7 参考文献 (10)8 附录:源程序清单 (10)1 需求分析1.1 目的和要求在熟练掌握计算机虚拟存储技术的原理的基础上,利用一种程序设计语言模拟实现几种置换算法,一方面加深对原理的理解,另一方面提高学生通过编程根据已有原理解决实际问题的能力,为学生将来进行系统软件开发和针对实际问题提出高效的软件解决方案打下基础。

1.2 研究内容模拟实现页式虚拟存储管理的三种页面置换算法(FIFO(先进先出)、LRU (最近最久未使用)和OPT(最长时间不使用)),并通过比较性能得出结论。

前提:(1)页面分配采用固定分配局部置换。

(2)作业的页面走向和分得的物理块数预先指定。

可以从键盘输入也可以从文件读入。

(3)置换算法的置换过程输出可以在显示器上也可以存放在文件中,但必须清晰可读,便于检验。

2 概要设计本程序主要划分为4个功能模块,分别是应用FIFO算法、应用LRU算法、应用OPT算法和页面引用串的插入。

1.1各模块之间的结构图2.1 FIFO 算法该模块的主要功能是对相应页面引用串进行处理,输出经过FIFO 算法处理之后的结果。

2.2 LRU 算法该模块的主要功功能是对相应的页面引用串进行处理,输出经过LRU 算法处理之后的结果。

2.3 OPT 算法该模块的主要功功能是对相应的页面引用串进行处理,输出经过OPT 算法处理之后的结果。

2.4 输入新的页面引用串该模块的主要功能是用户自己输入新的页面引用串,系统默认的字符串是0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0,用户可以自定义全新的20个数字页面引用串。

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

请求页式存储管理中常用页面置换算法模拟
if( find ) continue; // 块中有该数据,判断下一个数据 // 块中没有该数据 ChangeTimes++; // 缺页次数++ if( (i+1) > M ) // 因为i是从0开始记,而BlockNum指的是个数, 从1开始,所以i+1 { //获得要替换的块指针 temp = 0; for(j=0;j<M;j++) { if( temp < count[j] ) { temp = count[j]; point = j; // 获得离的最远的指针 } } } else point = i; // 替换 Block[point] = Data[i]; count[point] = 0; // 保存要显示的数据 for(j=0;j<M;j++) { DataShow[j][i] = Block[j]; DataShowEnable[i<M?(j<=i?j:i):j][i] = true; // 设置显示数据 } } // 输出信息 cout<<endl; cout<<"内存状态:\n"<<"--------------------------------"<< endl; DataOutput(); }
断次数以及缺页率。 2. 在上机环境中输入程序,调试,编译。 3. 设计输入数据,写出程序的执行结果。 4. 根据具体实验要求,填写好实验报告。 五、实 验 结 果 及 分 析: 实验结果截图如下:
利用一个特殊的栈来保存当前使用的各个页面的页面号。当进程访问某 页面时,便将该页面的页面号从栈中移出,将它压入栈顶。因此,栈顶 始终是最新被访问页面的编号,栈底是最近最久未被使用的页面号。 当访问第5个数据“5”时发生了缺页,此时1是最近最久未被访问的 页,应将它置换出去。同理可得,调入队列为:1 2 3 4 5 6 7 1 3 2 0 5,缺页次数为12次,缺页率为80%。 六、实 验 心 得: 本次实验实现了对请求页式存储管理中常用页面置换算法LRU算法的 模拟。通过实验,我对内存分页管理策略有了更多的了解。 最近最久未使用(LRU)置换算法的替换规则:是根据页面调入内存 后的使用情况来进行决策的。该算法赋予每个页面一个访问字段,用来

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

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

else { cout<<"---输入有误,物理块数请选择1-10的数---"<<endl<<"请输入分配的物理块数的大小:"; cin>>Nsize; } } break; case 2: cout<<"请输入分配的物理块数的大小: "; cin>>Nsize; while(1) { if(Nsize>0&&Nsize<=10) { Init(QString,Nsize); cout<<"页面号引用串: "; for(i=0;i<Psize;i++) { cout<<QString[i]<<" "; } cout<<endl; cout<<"LRU算法结果如下:"<<endl; LRU(Nsize); BlockClear(Nsize); cout<<"----------------------"<<endl; system("pause"); system("cls");
void BlockClear(int Nsize) {//块清除 for(int i=0; i<Nsize; i++) { block[i].yemian = -1; block[i].biaoji = 0; } } /*主程序*/ void main(void) { int i,select,Nsize,QString[Psize]={0}; while(select) { cout<<"页面号引用串: "; for(i=0;i<20;i++) { cout<<QString[i]<<" "; } cout<<endl; cout<<"+******************************+"<<endl; cout<<"+------------欢迎--------------+"<<endl; cout<<"+--------页面置换算法----------+"<<endl; cout<<"+-----选择<1>应用FIFO算法------+"<<endl; cout<<"+-----选择<2>应用LRU算法-------+"<<endl; cout<<"+-----选择<3>应用OPT算法-------+"<<endl; cout<<"+---选择<4>插入新的页面号引用串+"<<endl; cout<<"+-------选择<0>退出------------+"<<endl;

操作系统实验4-请求分页存储管理模拟实验

操作系统实验4-请求分页存储管理模拟实验

实验四请求分页存储管理模拟实验一:实验目得通过对页面、页表、地址转换与页面置换过程得模拟,加深对请求分页存储管理系统得原理与实现技术得理解.二:实验内容假设每个页面可以存放10条指令,分配给进程得存储块数为4。

用C语言或Pascal语言模拟一进程得执行过程。

设该进程共有320条指令,地址空间为32个页面,运行前所有页面均没有调入内存。

模拟运行时,如果所访问得指令已经在内存,则显示其物理地址,并转下一条指令;如果所访问得指令还未装入内存,则发生缺页,此时需要记录缺页产生次数,并将相应页面调入内存,如果4个内存块已满,则需要进行页面置换。

最后显示其物理地址,并转下一条指令。

在所有指令执行完毕后,显示进程运行过程中得缺页次数与缺页率.页面置换算法:分别采用OPT、FIFO、LRU三种算法。

进程中得指令访问次序按如下原则生成:50%得指令就是顺序执行得。

25%得指令就是均匀分布在低地址部分.25%得指令就是均匀分布在高地址部分.三:实验类别分页存储管理四:实验类型模拟实验五:主要仪器计算机六:结果OPT:LRU:FIFO:七:程序# i nclude 〈stdio 、h 〉# incl ude 〈stdlib 、h 〉# include 〈conio 、h># def ine blockn um 4//页面尺寸大小int m; //程序计数器,用来记录按次序执行得指令对应得页号s ta ti c in t num [320]; //用来存储320条指令typedef s truct BLOCK //声明一种新类型—-物理块类型{ﻩint pagenum; //页号ﻩint acces sed; //访问量,其值表示多久未被访问}BLOCK ;BLOCK bl ock [bl ocknum ]; //定义一大小为8得物理块数组void init () //程序初始化函数,对bl ock 初始化{for (int i=0;i <blo cknum;i++)block[i]、pagenum=—1;block[i]、accessed=0;ﻩm=0;}}int pageExist(int curpage)//查找物理块中页面就是否存在,寻找该页面curpage就是否在内存块block中,若在,返回块号{ﻩfor(int i=0;i<blocknum; i++)ﻩ{ﻩﻩif(block[i]、pagenum == curpage )ﻩﻩreturn i; //在内存块block中,返回块号ﻩ}return -1;}int findSpace()//查找就是否有空闲物理块,寻找空闲块block,返回其块号{for(int i=0;i<blocknum;i++)ﻩ{if(block[i]、pagenum==-1)ﻩreturn i;//找到了空闲得block,返回块号}ﻩreturn -1;}int findReplace()//查找应予置换得页面{ﻩint pos = 0;ﻩfor(int i=0;i〈blocknum;i++){if(block[i]、accessed 〉block[pos]、accessed)ﻩpos = i; //找到应该置换页面,返回BLOCK中位置ﻩ}return pos;}void display()//显示物理块中得页面号{ﻩﻩfor(int i=0; i〈blocknum; i++)ﻩ{ﻩif(block[i]、pagenum != -1)ﻩ{ﻩﻩprintf(” %02d ",block[i]、pagenum);ﻩﻩﻩprintf("%p |”,&block[i]、pagenum);ﻩﻩ}printf("\n");}void randam()//产生320条随机数,显示并存储到num[320]{int flag=0;printf(”请为一进程输入起始执行指令得序号(0~320):\n”);ﻩscanf("%d",&m);//用户决定得起始执行指令printf("******进程中指令访问次序如下:(由随机数产生)*******\n");for(int i=0;i〈320;i++){//进程中得320条指令访问次序得生成ﻩﻩnum[i]=m;//当前执行得指令数,ﻩﻩif(flag%2==0)ﻩm=++m%320;//顺序执行下一条指令ﻩﻩif(flag==1)ﻩﻩm=rand()%(m-1);//通过随机数,跳转到低地址部分[0,m—1]得一条指令处,设其序号为m1if(flag==3)ﻩﻩm=m+1+(rand()%(320-(m+1)));//通过随机数,跳转到高地址部分[m1+2,319]得一条指令处,设其序号为m2ﻩﻩflag=++flag%4;ﻩprintf(” %03d”,num[i]);//输出格式:3位数ﻩﻩif((i+1)%10==0)//控制换行,每个页面可以存放10条指令,共32个页面ﻩprintf(”\n”);}}void pagestring() //显示调用得页面序列,求出此进程按次序执行得各指令所在得页面号并显示输出{for(int i=0;i〈320;i++)ﻩ{printf(”%02d",num[i]/10);//输出格式:2位数if((i+1)%10==0)//控制换行,每个页面可以存放10条指令,共32个页面ﻩﻩprintf("\n”);}}void OPT() //最佳替换算法{ﻩint n=0;//记录缺页次数int exist,space,position;ﻩintcurpage;//当前指令得页面号ﻩfor(int i=0;i<320;i++)ﻩ{ﻩm=num[i];ﻩcurpage=m/10;ﻩﻩexist=pageExist(curpage);ﻩif(exist==-1)ﻩﻩ{ //当前指令得页面号不在物理块中space=findSpace();ﻩﻩif(space != -1)ﻩﻩ{//当前存在空闲得物理块ﻩﻩblock[space]、pagenum= curpage;//将此页面调入内存ﻩﻩﻩdisplay();//显示物理块中得页面号ﻩﻩn++;//缺页次数+1ﻩ}ﻩﻩelseﻩﻩ{ //当前不存在空闲得物理块,需要进行页面置换for(intk=0;k<blocknum;k++)ﻩﻩﻩﻩ{for(int j=i;j〈320;j++)ﻩ{//找到在最长(未来)时间内不再被访问得页面ﻩﻩﻩﻩif(block[k]、pagenum!= num[j]/10)ﻩﻩﻩ{ﻩﻩblock[k]、accessed = 1000;ﻩﻩﻩ} //将来不会被访问,设置为一个很大数ﻩﻩﻩelseﻩﻩﻩ{ //将来会被访问,访问量设为jﻩﻩﻩblock[k]、accessed = j;ﻩﻩﻩﻩﻩbreak;ﻩﻩﻩﻩ}ﻩﻩﻩ}ﻩ}ﻩposition = findReplace();//找到被置换得页面,淘汰ﻩblock[position]、pagenum = curpage;// 将新页面调入display();ﻩﻩn++; //缺页次数+1ﻩ}}ﻩ}ﻩprintf(”缺页次数:%d\n",n);printf("缺页率:%f%%\n",(n/320、0)*100);}void LRU() //最近最久未使用算法{int n=0;//记录缺页次数ﻩint exist,space,position ;ﻩint curpage;//当前指令得页面号ﻩfor(int i=0;i<320;i++)ﻩ{ﻩm=num[i];ﻩﻩcurpage=m/10;ﻩexist = pageExist(curpage);ﻩif(exist==-1)ﻩﻩ{ //当前指令得页面号不在物理块中space = findSpace();ﻩﻩif(space!= —1)ﻩ{ //当前存在空闲得物理块ﻩﻩblock[space]、pagenum = curpage;//将此页面调入内存ﻩﻩdisplay();//显示物理块中得页面号ﻩn++;//缺页次数+1ﻩﻩ}else{ //当前不存在空闲得物理块,需要进行页面置换ﻩﻩposition= findReplace();ﻩblock[position]、pagenum = curpage;ﻩﻩdisplay();ﻩn++;//缺页次数+1ﻩ}ﻩﻩ}elseﻩﻩblock[exist]、accessed = -1;//恢复存在得并刚访问过得BLOCK中页面accessed为-1for(int j=0; j<blocknum; j++)ﻩﻩ{//其余得accessed++ﻩﻩblock[j]、accessed++;}ﻩ}printf("缺页次数:%d\n”,n);ﻩprintf("缺页率:%f%%\n",(n/320、0)*100);}void FIFO(){int n=0;//记录缺页次数int exist,space,position ;ﻩ int curpage;//当前指令得页面号int blockpointer=-1;for(int i=0;i<320;i++)ﻩ{ﻩ m=num[i];curpage=m/10;ﻩexist = pageExist(curpage);ﻩ if(exist==-1){//当前指令得页面号不在物理块中ﻩ space = findSpace();ﻩﻩif(space !=-1)ﻩ { //当前存在空闲得物理块ﻩﻩ blockpointer++;ﻩﻩﻩblock[space]、pagenum=curpage; //将此页面调入内存ﻩ n++;//缺页次数+1ﻩﻩﻩ display();//显示物理块中得页面号ﻩ}ﻩ elseﻩ { //没有空闲物理块,进行置换ﻩﻩﻩﻩposition = (++blockpointer)%4;ﻩ block[position]、pagenum = curpage;//将此页面调入内存ﻩﻩn++;ﻩﻩ display();ﻩ}ﻩ }}printf("缺页次数:%d\n",n);printf("缺页率:%f%%\n",(n/320、0)*100);}void main(){ﻩint choice;ﻩprintf("************请求分页存储管理模拟系统*************\n");ﻩrandam();printf("************此进程得页面调用序列如下**************\n”);pagestring();ﻩwhile(choice!= 4){ﻩﻩprintf("********1:OPT 2:LRU 3:FIFO 4:退出*********\n”);ﻩprintf("请选择一种页面置换算法:”);ﻩscanf("%d",&choice);ﻩinit();ﻩswitch(choice)ﻩ{ﻩcase 1:ﻩﻩﻩprintf(”最佳置换算法OPT:\n");ﻩprintf("页面号物理地址页面号物理地址页面号物理地址页面号物理地址\n");ﻩﻩﻩOPT();ﻩbreak;ﻩcase 2:ﻩﻩprintf("最近最久未使用置换算法LRU:\n");ﻩprintf("页面号物理地址页面号物理地址页面号物理地址页面号物理地址\n");ﻩLRU();ﻩﻩﻩbreak;ﻩﻩcase 3:ﻩprintf("先进先出置换算法FIFO:\n");ﻩprintf("页面号物理地址页面号物理地址页面号物理地址页面号物理地址\n");FIFO();ﻩﻩbreak;ﻩ}}}。

实验--编程实现请求分页存储管理页面Optimal、FIFO、LRU置换算法

实验--编程实现请求分页存储管理页面Optimal、FIFO、LRU置换算法
int index=0;
for(int i=0;i<fS;i++){
if(((Couple)(i)).time<temp){
temp=((Couple)(i)).time;
index=i;
}
}
for(int i=0;i<fS;i++){
if(i!=index){
((Couple)(i)).time--;
for(int i=0;i<;i++){
"Frame size: "+frameSize[i]+"\n");
(frameSize[i]);
(frameSize[i]);
"Total errors found: "+errorCount);
"\n************************************\n");
(frameSize[i]);
(frameSize[i]);
"Total errors found: "+errorCount);
"\n************************************\n");
errorCount=0;
}
"----------------Using LRU----------------");
(new Couple(0));
}
}
public void LRUReplace(int fS){
boolean findThesame=false;
int pre=-1;alue==digitalArray[j]){

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

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

信息工程学院实验报告课程名称:操作系统Array实验项目名称:请求页式存储管理中常用页面置换算法模拟实验时间:班级姓名:学号:一、实验目的:1.了解内存分页管理策略2.掌握调页策略3.掌握一般常用的调度算法4.学会各种存储分配算法的实现方法。

5.了解页面大小和内存实际容量对命中率的影响。

二、实验环境:PC机、windows2000 操作系统、VC++6.0三、实验要求:本实验要求4学时完成。

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

实验前应复习实验中所涉及的理论知识和算法,针对实验要求完成基本代码编写、实验中认真调试所编代码并进行必要的测试、记录并分析实验结果。

实验后认真书写符合规范格式的实验报告,按时上交。

四、实验内容和步骤:1.编写程序,实现请求页式存储管理中常用页面置换算法LRU算法的模拟。

要求屏幕显示LRU算法的性能分析表、缺页中断次数以及缺页率。

2.在上机环境中输入程序,调试,编译。

3.设计输入数据,写出程序的执行结果。

4.根据具体实验要求,填写好实验报告。

五、实验结果及分析:实验结果截图如下:利用一个特殊的栈来保存当前使用的各个页面的页面号。

当进程访问某页面时,便将该页面的页面号从栈中移出,将它压入栈顶。

因此,栈顶始终是最新被访问页面的编号,栈底是最近最久未被使用的页面号。

当访问第5个数据“5”时发生了缺页,此时1是最近最久未被访问的页,应将它置换出去。

同理可得,调入队列为:1 2 3 4 5 6 7 1 3 2 0 5,缺页次数为12次,缺页率为80%。

六、实验心得:本次实验实现了对请求页式存储管理中常用页面置换算法LRU算法的模拟。

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

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

计算机操作系统实验报告济南大学信息科学与技术学院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。

操作系统 页式虚拟存储管理程序模拟

操作系统 页式虚拟存储管理程序模拟
{switch(choice)
{case 1:
cout<<"----------------------------------------------------------"<<endl;
cout<<"FIFO算法结果如下:"<<endl;
FIቤተ መጻሕፍቲ ባይዱO();
break;
case 2:
cout<<"----------------------------------------------------------"<<endl;
int Simulate[MaxNum][M];//页面访问过程
int PageCount[M],LackNum;//PageCount用来记录LRU算法中最久未使用时间,LackNum记录缺页数
float PageRate;//命中率
int PageCount1[32];
bool IsExit(int i)//FIFO算法中判断新的页面请求是否在内存中
算法特点:每个页面都有属性来表示有多长时间未被CPU使用的信息。
结果分析
#include<iostream>
#include<time.h>
using namespace std;
const int MaxNum=320;//指令数
const int M=5;//内存容量
int PageOrder[MaxNum];//页面请求
for(int i=0;i<MaxNum;i++)//初始化页面访问过程
{ for(int j=0;j<M;j++)

操作系统课程设计模拟请求页式存储管理

操作系统课程设计模拟请求页式存储管理

操作系统课程设计报告项目:模拟请求页式存储管理一、目的和要求1、实训目的(1)通过编写和调试存储管理的模拟程序以加深对存储管理方案的理解。

熟悉虚存管理的各种页面淘汰算法(2)通过编写和调试地址转换过程的模拟程序以加强对地址转换过程的了解。

2、实训要求编写并调试完成请求页式存储管理程序。

页面置换算法:最佳置换算法(OPT)、先进先出算法(FIFO)和最近最少用算法(LRU)。

要求打印每个页面置换算法的页面置换变化示意图、缺页中断次数和缺页中断率,以比较各种算法的优缺点。

二、设计思路及过程1、概要设计1.1 问题概述根据三种不同的置换算法(FIFO、LRU、OPT),依据其不同的算法方式,分别计算该页面引用串在不同算法下的缺页次数与缺页率,并显示各页面的变化情况。

1.2 内容分析对于该课程设计中模拟的请求页式存储管理的页面置换过程,只要掌握其中最基本的三种算法,包括FIFO、LRU及OPT。

另外,对于同一个页面引用串,要求能够调用不同的算法对它进行操作。

2、过程设计2.1模块设计在下图的主模块设计图中,只注重描绘了请求页式存储管理的三种主要算法,未描绘出细节部分。

图2.1 请求页式存储管理的主模块设计图2.2 算法原理分析要成功实现算法,首先要知道各个方法是怎么做的,即原理是怎样的,下面是三种算法的原理。

(1)FIFO算法:该算法认为刚被调入的页面在最近的将来被访问的可能性很大,而在主存中滞留时间最长的页面在最近的将来被访问的可能性很小。

因此。

FIFO算法总是淘汰最先进入主存的页面,即淘汰在主存中滞留时间最长的页面。

(2)LRU算法是最近最久未使用,当当前内存中没有正要访问的页面时,置换出在当前页面中最近最久没有使用的页面。

该算法总是选择最近一段时间内最长时间没有被访问过的页面调出。

它认为那些刚被访问过的页面可能在最近的将来还会经常访问他们,而那些在较长时间里未被访问的页面,一般在最近的将来再被访问的可能性较小。

请求页式存储管理的模拟实现_参考代码_

请求页式存储管理的模拟实现_参考代码_
break;
case REQUEST_EXECUTE:
ptr_pageTabIt->count++;
if(!(ptr_pageTabIt->proType&EXECUTABLE)){
do_error(ERROR_EXECUTE_DENY);
return ;
}
info.Format(_T("执行成功\n"));
out->SetSel(-1, -1);
out->ReplaceSel(info);
if(pageTable[page].modified){
info.Format(_T("有修改,写回至外存\n"));
out->SetSel(-1, -1);
out->ReplaceSel(info);
do_page_out(&pageTable[page]);
do_init(){
int i,j;
srand(time(NULL));
for(i=0;i<PAGE_SUM;i++){
pageTable[i].pageNum=i;
pageTable[i].effective=mFALSE;
pageTable[i].modified=mFALSE;
pageTable[i].count=0;
ptr_pageTabIt->blockNum=j;//修改调入页的页表项
ptr_pageTabIt->effective=TRUE;
ptr_pageTabIt->count=0;
info.Format(_T("页面替换成功\n"));

3编程实现请求分页存储管理页面Optimal、FIFO、LRU置换算法

3编程实现请求分页存储管理页面Optimal、FIFO、LRU置换算法
(1)通过编写程序实现请求分页存储管理页面 Optimal、FIFO、LRU 调度算法,使学生 掌握虚拟存储管理中有关缺页处理方法等内容,巩固有关虚拟存储管理的教学内容。
(2)了解 Windows2000/XP 中内存管理机制,掌握页式虚拟存储技术。 (3)理解内存分配原理,特别是以页面为单位的虚拟内存分配方法
{
for(j=0;j<n;j++)
printf("%3d",table[i][j]);
printf("\n");
}
for(i=0;i<n;i++)
printf("%3c",f[i]);
}
2、LRU 算法
#include "stdio.h"
#define n 20
#define m 5
void main()
【实验内容】
在 Windows XP、Windows 2000 等操作系统下,使用的 VC、VB、java 或 C 等编程语言, 实现请求分页存储管理页面 Optimal、FIFO、LRU 调度算法。
【实验环境】(含主要设计设备、器材、软件等) 一台计算机 及 c++软件
【实验步骤、过程】(含原理图、流程图、关键代码,或实验过程中的记录、数据等)
{
for(j=m-1;j>0;j--)
//淘汰最先调入的页面调入当前访问的
mem[j]=mem[j-1];
mem[0]=ym[i];
}
for(j=0;j<m;j++)
table[j][i]=mem[j];
f[i]=flag;
}

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

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

计算机操作系统实验报告济南大学信息科学与技术学院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。

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

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

实验七请求页式存储管理中常用页面置换算法模拟实验学时:4实验类型:设计实验要求:必修一、实验目的(1)了解内存分页管理策略(2)掌握调页策略(3)掌握一般常用的调度算法(4)学会各种存储分配算法的实现方法。

(5)了解页面大小和内存实际容量对命中率的影响。

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

三、实验原理分页存储管理将一个进程的逻辑地址空间分成若干大小相等的片,称为页面或页。

在进程运行过程中,若其所要访问的页面不在内存而需把它们调入内存,但内存已无空闲空间时,为了保证该进程能正常运行,系统必须从内存中调出一页程序或数据,送磁盘的对换区中。

但应将哪个页面调出,须根据一定的算法来确定。

通常,把选择换出页面的算法称为页面置换算法(Page_Replacement Algorithms)。

一个好的页面置换算法,应具有较低的页面更换频率。

从理论上讲,应将那些以后不再会访问的页面换出,或将那些在较长时间内不会再访问的页面调出。

1、最佳置换算法OPT(Optimal)它是由Belady于1966年提出的一种理论上的算法。

其所选择的被淘汰页面,将是以后永不使用的或许是在最长(未来)时间内不再被访问的页面。

采用最佳置换算法,通常可保证获得最低的缺页率。

但由于人目前还无法预知一个进程在内存的若干个页面中,哪一个页面是未来最长时间内不再被访问的,因而该算法是无法实现的,便可以利用此算法来评价其它算法。

2、先进先出(FIFO)页面置换算法这是最早出现的置换算法。

该算法总是淘汰最先进入内存的页面,即选择在内存中驻留时间最久的页面予以淘汰。

该算法实现简单只需把一个进程已调入内存的页面,按先后次序链接成一个队列,并设置一个指针,称为替换指针,使它总是指向最老的页面。

请求分页存储管理模拟实验

请求分页存储管理模拟实验

操作系统模拟实验实验名称:请求分页存储管理模拟实验实验目的:通过实验了解windows系统中的线程同步如何使用,进一步了解操作系统的同步机制。

实验内容:调用Windows API,模拟解决生产者-消费者问题;思考在两个线程函数中哪些是临界资源?哪些代码是临界区?哪些代码是进入临界区?哪些代码是退出临界区?进入临界区和退出临界区的代码是否成对出现?学习Windows API中的如何创建线程,互斥,临界区等。

程序运行结果:源程序:#include "stdAfx.h"//包含头文件以支持多线程#include "windows.h"#include "stdio.h"//用于标志所有的子线程是否结束//每次子线程结束后,此值便加1。

static long ThreadCompleted = 0;//互斥量HANDLE mutex;//信号量,用于生产者通知消费者HANDLE full;//信号量,用于消费者通知生产者HANDLE empty;//信号量,当所有的子线程结束后,通知主线程,可以结束。

HANDLE evtTerminate;//生产标志#define p_item 1//消费标志#define c_item 0//哨兵#define END 10//缓冲区最大长度const int max_buf_size=11;const int cur_size=10;//缓冲区定义int BUFFER[max_buf_size];//放消息指针int in=0;//取消息指针int out=0;int front=0;int tail=0;int sleep_time=1000;bool flag=true;//线程函数的标准格式unsigned long __stdcall p_Thread(void *theBuf);unsigned long __stdcall c_Thread(void *theBuf);//打印缓冲区内容void PrintBuf(int buf[],int buf_size);int main(int argc, char* argv[]){//初始化缓冲区unsigned long TID1, TID2;for(int i=0;i<cur_size;i++)BUFFER[i]=0;//互斥量和信号量的创建,函数用法可查看MSDNmutex=CreateMutex(NULL,false,"mutex");full=CreateSemaphore(NULL,0,1,"full");empty=CreateSemaphore(NULL,max_buf_size,max_buf_size,"empty");evtTerminate = CreateEvent(NULL, FALSE, FALSE, "Terminate");//创建一个生产者线程和消费者线程。

请求调页存储管理方式的模拟

请求调页存储管理方式的模拟

实验3请求调页存储管理方式的模拟1实验目的通过对页面、页表、地址转换和页面置换过程的模拟,加深对请求调页系统的原理和实现过程的理解。

2实验内容(1)假设每个页面中可存放10条指令,分配给一作业的内存块数为4。

(2)模拟一作业的执行过程。

该作业共有320条指令,即它的地址空间为32页,目前它的所有页都还未调入内存。

在模拟过程中,如果所访问的指令已经在内存中,则显示其物理地址,并转下一条指令。

如果所访问的指令还未装入内存,则发生缺页,此时需记录缺页的次数,并将相应页调入内存。

如果4个内存块中均已装入该作业,则需进行页面置换。

最后显示其物理地址,并转下一条指令。

在所有320条指令执行完毕后,请计算并显示作业运行过程中发生的缺页率。

(3)置换算法:请分别考虑OPT、FIFO和LRU算法。

(4)作业中指令的访问次序按下述原则生成:•50%的指令是顺序执行的。

•25%的指令是均匀分布在前地址部分。

•25%的指令时均匀分布在后地址部分。

代码:package mainDart;import java.util.ArrayList;import java.util.List;import java.util.Random;public class FIFO {private static int times=0;//记录置换内存页面的次数/*** 随机产生0~319之间的数* 产生320条指令** @return 包含320条指令的数组*/public static int[] productNumber(){int order[] = new int[320];//数组存储的数字表示指令Random rand = new Random();for(int i=0;i<320;i++){if(i%4==0){order[i]=rand.nextInt(319); //0<=order<319}else if(i%4==1){order[i]=order[i-1]+1;//1<=order<320}else if(i%4==2){order[i]= rand.nextInt(order[i-1]);}else if(i%4==3){order[i]=order[i-1]+rand.nextInt(320-order[i-1]);}}return order;}/*** 打印链表* @param list*/public static void printList(List<Integer> list){for(int temt:list){System.out.print(temt+"\t");}System.out.println();}/*** 先进先出算法* 总是淘汰最先进入内存的页面* 在实现的时候,记录上一次所替换的页面在内存的下标,则本次要替换的位置就是上次下标+1的位置,并且下标是0~3循环的* @param memoryNum* @param page*/public static void FIFOChangePage(List<Integer> memoryNum,int page){int index = FIFOChangePage(memoryNum,page,++times);memoryNum.remove(index);memoryNum.add(index, page);}/*** 返回本次替换的页面在内存中的位置* @param memoryNum* @param page* @param times记录替换页面的次数,第一次替换的是内存第0个单元* @return*/public static int FIFOChangePage(List<Integer> memoryNum,int page,int times) {if(times==1){return 0;}int index = (FIFOChangePage(memoryNum,page,times-1)+1)%4;return index;}public static void main(String[] args) {int[] order = productNumber();System.out.println("320条随机指令数:");for(int i =0;i<order.length;i++){System.out.print(order[i]+"\t");if((i+1)%10==0){System.out.println();}}List<Integer> memoryNum= new ArrayList<Integer>(4);//内存块存储的页面int count=0;//记录缺页次数for(int i=0;i<320;i++){int page = order[i]/10;if(memoryNum.contains(page)) //若该指令所在页面已经在内存,输出该页面所在内存块的号{}else//没在内存,发生缺页,需要判断内存块是否存满,{if(memoryNum.size()<4) //内存没满,直接将所需页面调入内存{memoryNum.add(page);}else//内存存满,需要调用页面置换算法,进行页面置换{FIFOChangePage(memoryNum,page);//先进先出算法}count++;//记录缺页次数}printList(memoryNum);//打印内存所调入页面的情况}System.out.println("缺页率:"+(double)count/320);}}package mainDart;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Random;public class LRU {/*** 随机产生0~319之间的数* 产生320条指令** @return 包含320条指令的数组*/public static int[] productNumber(){int order[] = new int[320];//数组存储的数字表示指令Random rand = new Random();for(int i=0;i<320;i++){if(i%4==0){order[i]=rand.nextInt(319); //0<=order<319}else if(i%4==1){order[i]=order[i-1]+1;//1<=order<320}else if(i%4==2){order[i]= rand.nextInt(order[i-1]);}else if(i%4==3){order[i]=order[i-1]+rand.nextInt(320-order[i-1]);}}return order;}/*** 打印链表* @param list*/public static void printList(List<Integer> list){for(int temt:list){System.out.print(temt+"\t");}System.out.println();}/*** 最近最久未使用算法* @param order*/public static void LRUChangePage(int [] order){List<Integer> memoryNum = new ArrayList<Integer>(4);//内存块int[] timeFlag =new int[]{-1,-1,-1,-1}; //用来记录内存当中各单元未被访问的时间值int count=0;//记录缺页次数for(int i=0;i<320;i++){int page = order[i]/10;if(memoryNum.contains(page)) //若该指令所在页面已经在内存{int index = memoryNum.indexOf(page);timeFlag[index]=0;//将时间变为0 }else//没在内存,发生缺页,需要判断内存块是否存满,{if(memoryNum.size()<4) //内存没满,直接将所需页面调入内存{//将没有命中的所有页面的时间加一for(int in=0;in<4;in++){if(timeFlag[in]!=-1){timeFlag[in]+=1;}}//将page加入内存并将时间置为0memoryNum.add(page);timeFlag[memoryNum.indexOf(page)]=0;}else//内存存满,需要调用页面置换算法,进行页面置换{int maxIn=-1;//记录拥有最大时间值的标记的下标int maxT=-1;//记录最大的时间值for(int in=0;in<4;in++)//找出内存中时间值最大的进行替换{if(timeFlag[in]>maxT){maxT=timeFlag[in];maxIn=in;}}memoryNum.remove(maxIn);memoryNum.add(maxIn,page);timeFlag[maxIn]=0;//将没有命中的所有页面的时间加一for(int in=0;in<4;in++){if(in!=maxIn){timeFlag[in]+=1;}}}count++;//记录缺页次数}printList(memoryNum);//打印内存所调入页面的情况}System.out.println("缺页率:"+(double)count/320);}public static void main(String[] args) {int[] order = productNumber();System.out.println("320条随机指令数:");for(int i =0;i<order.length;i++){System.out.print(order[i]+"\t");if((i+1)%10==0){System.out.println();}}LRUChangePage(order);}}package mainDart;import java.util.ArrayList;import java.util.List;import java.util.Random;public class Optimal {/*** 随机产生0~319之间的数* 产生320条指令** @return 包含320条指令的数组*/public static int[] productNumber(){int order[] = new int[320];//数组存储的数字表示指令Random rand = new Random();for(int i=0;i<320;i++){if(i%4==0){order[i]=rand.nextInt(319); //0<=order<319}else if(i%4==1){order[i]=order[i-1]+1;//1<=order<320}else if(i%4==2){order[i]= rand.nextInt(order[i-1]);}else if(i%4==3){order[i]=order[i-1]+rand.nextInt(320-order[i-1]);}}return order;}/**** @param order320条指令数组* @return 返回一个链表,依次保存着320条指令每条指令所在的页面*/public static List<Integer> pageSeq(int[] order){List<Integer> pageSeq = new ArrayList<Integer>();for(int temp:order){pageSeq.add(temp/10);}return pageSeq;}/*** 打印链表* @param list*/public static void printList(List<Integer> list){for(int temt:list){System.out.print(temt+"\t");}System.out.println();}/*** 最佳置换算法* 根据当前已经在内存中的页面在之后被需要的先后进行置换** @param pageSeq 整个320条指令,从头到尾所需要的页面* @param memoryNum 已满的内存空间* @param page等待被调入内存的页面*/public static void OptimalChangePage(List<Integer> pageSeq,int start,List<Integer> memoryNum,int page){int maxSeq=-1,index=0;for(int pageNum:memoryNum) //遍历内存{for(int i=start;i<pageSeq.size();i++){if(pageNum==pageSeq.get(i)){if(i>maxSeq){maxSeq=i;}break;}}}if(maxSeq>-1)//maxSeq==-1说明内存当中的四个页面在将来都不会再被使用,这时默认将内存块中的第一个页面置换出{index = memoryNum.indexOf(pageSeq.get(maxSeq));//记录将要被置换的那个页面所在内存位置}memoryNum.remove(index);//将内存中将来最久被使用的页面删除memoryNum.add(index, page);//将需要调入的页面加入内存}public static void main(String[] args) {int[] order = productNumber();System.out.println("320条随机指令数:");for(int i =0;i<order.length;i++){System.out.print(order[i]+"\t");if((i+1)%10==0){System.out.println();}}List<Integer> pageSeq = pageSeq(order); //依次存放着指令所在的页面号List<Integer> memoryNum= new ArrayList<Integer>(4);//内存块存储的页面int count=0;//记录缺页次数for(int i=0;i<320;i++){int page = order[i]/10;if(memoryNum.contains(page)) //若该指令所在页面已经在内存,输出该页面所在内存块的号{}else//没在内存,发生缺页,需要判断内存块是否存满,{if(memoryNum.size()<4) //内存没满,直接将所需页面调入内存{memoryNum.add(page);}else//内存存满,需要调用页面置换算法,进行页面置换{OptimalChangePage(pageSeq,i+1,memoryNum,page);//最佳置换算法}count++;//记录缺页次数}printList(memoryNum);//打印内存所调入页面的情况}System.out.println("缺页率:"+(double)count/320);}}package mainDart;import java.util.ArrayList;import java.util.List;public class TestAPP {public static void main(String[] args) {List<Integer> list = new ArrayList<Integer>();list.add(1);list.add(3);list.add(45);System.out.println(list);list.remove(1);list.add(1, -1);System.out.println(list);}}。

请求页式管理缺页中断模拟设计-- LRU、随机淘汰算法

请求页式管理缺页中断模拟设计-- LRU、随机淘汰算法

学号:课程设计题目请求页式管理缺页中断模拟设计-- LRU、随机淘汰算法学院计算机学院专业班级姓名指导教师吴利军2013 年01 月14 日课程设计任务书学生姓名:指导教师:吴利军工作单位:计算机科学与技术学院题目:请求页式管理缺页中断模拟设计--LRU、随机淘汰算法初始条件:1.预备内容:阅读操作系统的内存管理章节内容,了解有关虚拟存储器、页式存储管理等概念,并体会和了解缺页和页面置换的具体实施方法。

2.实践准备:掌握一种计算机高级语言的使用。

要求完成的主要任务:(包括课程设计工作量及其技术要求,以及说明书撰写等具体要求)1.实现指定淘汰算法。

能够处理以下的情形:⑴能够输入给作业分配的内存块数;⑵能够输入给定的页面,并计算发生缺页的次数以及缺页率;⑶缺页时,如果发生页面置换,输出淘汰的页号。

2.设计报告内容应说明:⑴需求分析;⑵功能设计(数据结构及模块说明);⑶开发平台及源程序的主要部分;⑷测试用例,运行结果与运行情况分析;⑸自我评价与总结:i)你认为你完成的设计哪些地方做得比较好或比较出色;ii)什么地方做得不太好,以后如何改正;iii)从本设计得到的收获(在编写,调试,执行过程中的经验和教训);iv)完成本题是否有其他方法(如果有,简要说明该方法);时间安排:设计安排一周:周1、周2:完成程序分析及设计。

周2、周3:完成程序调试及测试。

周4、周5:验收、撰写课程设计报告。

(注意事项:严禁抄袭,一旦发现,一律按0分记)指导教师签名:年月日系主任(或责任教师)签名:年月日请求页式管理缺页中段模拟设计——LRU、随机淘汰算法1概述1.1原理页式存储管理把内存分割成大小相等位置固定的若干区域,叫内存页面,内存的分配以“页”为单位,一个程序可以占用不连续的页面,逻辑页面的大小和内存页面的大小相同,内外存的交换也以页为单位进行,页面交换时,先查询快表,若快表中找不到所需页面再去查询页表,若页表中仍未找到说明发生了缺页中断,需先将所需页面调入内存再进行存取。

请求调页存储器管理方式的模拟

请求调页存储器管理方式的模拟

课程设计课程设计名称:计算机操作系统课程设计专业班级:计算机科学与技术班学生姓名:学号:指导教师:课程设计时间: 2010.12.20 ~ 2010.12.24计算机科学与技术专业课程设计任务书一需求分析请求调页存储管理方式的模拟是基于LRU算法的设计而设计的,通过学习计算机操作系统中的请求调页存储管理方式的几种算法,我选择了最近最久未使用算法即LRU算法实现请求调叶存储管理,通过具体的程序来模仿LRU的工作机制。

二概要设计1.数据结构依据给定的数据信息,数组必须以结构体实现,结构类型的层次结构如下: typedef struct BLOCK//声明一种新类型——物理块类型{int pagenum;//页号int accessed;//访问字段,其值表示多久未被访问}BLOCK;2.函数原型清单:Void main();//主函数void init(int Bsize); //程序初始化函数int findExist(int curpage);//查找物理块中是否有该页面int findSpace(int Bsize);//查找是否有空闲物理块int findReplace();//查找应予置换的页面void display(int Bsize);//显示void suijishu(int r);//产生320条随机数,显示并存储到temp[320]void pagestring();//显示调用的页面队列void LRU(int Bsize);// LRU算法3.全局变量:int Bsize;int pc;//程序计数器,用来记录指令的序号int n;//缺页计数器,用来记录缺页的次数static int temp[320];//用来存储320条随机数BLOCK block[Bsize]; //定义一大小为4的物理块数组三运行环境(软硬件环境)硬件:CPU,主板,内存,显示器,硬盘,显卡,键盘等等.软件:WINDOWS XP, Visual c++应用软件.四开发工具和编程语言开发工具:Visual c++编程语言:c语言五详细设计#include <iostream.h>#include<stdlib.h>#include<conio.h>#include<stdio.h>typedef struct BLOCK//声明一种新类型——物理块类型{int pagenum;//页号int accessed;//访问字段,其值表示多久未被访问}BLOCK;int Bsize;BLOCK block[32]; //模拟内存块int pc;//程序计数器,用来记录指令的序号int n;//缺页计数器,用来记录缺页的次数static int temp[320];//用来存储320条随机数//*************************************************************void init(int Bsize); //程序初始化函数int findExist(int curpage);//查找物理块中是否有该页面int findSpace(int Bsize);//查找是否有空闲物理块int findReplace();//查找应予置换的页面void display(int Bsize);//显示void suijishu(int r);//产生320条随机数,显示并存储到temp[320] void pagestring();//显示调用的页面队列void LRU(int Bsize);// LRU算法//************************************************************* void init(int Bsize){int i;for(i=0;i<Bsize;i++){block[i].pagenum=-1;block[i].accessed=0;pc=n=0;}}//------------------------------------------------------------- int findExist(int curpage,int Bsize){int i;for(i=0;i<Bsize; i++){if(block[i].pagenum == curpage )return i;//检测到内存中有该页面,返回block中的位置}return -1;}//------------------------------------------------------------- int findSpace(int Bsize){int i;for(i=0; i<Bsize; i++){if(block[i].pagenum == -1)return i;//找到空闲的block,返回block中的位置}return -1;}//------------------------------------------------------------- int findReplace(int Bsize){int i,pos = 0;for(i=0; i<Bsize; i++){if(block[i].accessed >block[pos].accessed)pos = i;//找到应予置换页面,返回BLOCK中位置}return pos;}//------------------------------------------------------------- void display(int Bsize){if(Bsize==4){int i;for(i=0; i<Bsize; i++){if(block[i].pagenum != -1){ printf(" %02d",block[i].pagenum);}}printf("\n");}}//------------------------------------------------------------- void suijishu(int r){int i,flag=0;pc=r;printf("****按照要求产生的320个随机数:*******\n");for(i=0;i<320;i++){temp[i]=pc;if(flag%2==0) pc=++pc%320;if(flag==1) pc=rand()% (pc-1);if(flag==3) pc=pc+1+(rand()%(320-(pc+1)));flag=++flag%4;printf(" %03d",temp[i]);if((i+1)%10==0) printf("\n");}}//------------------------------------------------------------- void pagestring(){int i;for(i=0;i<320;i++){printf(" %02d",temp[i]/10);if((i+1)%10==0) printf("\n");}}//-------------------------------------------------------------void LRU(int Bsize){int exist,space,position ;int i,j,curpage;init(Bsize);for(i=0;i<320;i++){pc=temp[i];curpage=pc/10;exist = findExist(curpage,Bsize);if(exist==-1){space = findSpace(Bsize);if(space != -1){block[space].pagenum = curpage;display(Bsize);n=n+1;}else{position = findReplace(Bsize);block[position].pagenum = curpage;display(Bsize);n++;}}else block[exist].accessed = -1;//恢复存在的并刚访问过的BLOCK中页面accessed为-1for(j=0; j<4; j++){block[j].accessed++;}}if(Bsize==4)printf("(LRU)算法在不同内存容量下的命中率为\n");printf("内存容量为%d",Bsize);printf("的缺页次数:%d",n);printf("缺页率:%f",(n/320.0)*100);printf("%%");printf("命中率:%f",(1-n/320.0)*100);printf("%%");printf("\n");}//------------------------------------------------------------- //************************************************************* void main(){int t,i,j=1,select;printf("请输入第1条指令号(0~320):");scanf("%d",&i);while(i>320||i<0){printf("你输入有误,请重新输入!\n");printf("请输入第1条指令号(0~320):");scanf("%d",&i);}while(i>=0&&i<=320){suijishu(i);printf("*****对应的调用页面队列*******\n");pagestring();printf("******************************\n");init(i);printf("最近最久未使用置换算法LRU:\n");printf("******************************\n");for(t=4;t<=32;t++)LRU(t);j=j+1;printf("请输入第%d条指令号(0~320)[输入-1结束]:",j);scanf("%d",&i);}}六调试分析(1).刚开始进行编译时,出现了86个错误和警告,吓了自己一跳,于是就静下心来一点一点分析程序,发现自己竟然那么粗心,不是少了括号就是少了分号,还有一些语法错误,经过慢慢的修改,程序最终顺利运行出来.(2).在调试过程中,有时会显示没有错误和警告,但程序会无法执行,或执行到中间就无法在执行了,于是我就用F10进行调试,发现可能出错的子函数,然后进行仔细分析改正,调试成功.七测试结果图1 输入错误数字的提示信息图2随机产生的320个数图3对应的调用页面的队列图4 LRU算法(1)图5 LRU算法(2)图6 LRU算法(3)图7 LRU算法(4)图8 LRU算法(5)和计算最近最少使用(LRU)算法在不同内存容量下的命中率八参考文献[1] 谭浩强《C程序设计》第三版北京清华大学出版社[2] 丁华伟《C语言程序设计系统》第三版北京清华大学出版社[3] 严蔚敏《数据结构》C语言版北京清华大学出版社课程设计总结本程序能通过输入第一条指令号(用3位整数代表指令号),产生320个随机数,并以每行10个显示出来。

使用最近未使用页淘汰(NRU)算法模拟实现页淘汰进程

使用最近未使用页淘汰(NRU)算法模拟实现页淘汰进程

使用最近未使用页淘汰(NRU)算法模拟实现页淘汰进程摘要:最近未使用页淘汰(NRU)算法或者时钟算法是实际使用的诸多页淘汰算法中的一种。

本课程设计是使用C程序设计语言,在windows平台下对页淘汰(NRU)算法模拟,通过页淘汰(NRU)算法的模拟来进一步的加深对使用NRU算法的了解,及对C程序设计语言的使用。

关键词:页淘汰 NRU 时钟算法一.设计的背景介绍1.1 介绍相关概念,相关算法页淘汰工作通常是由一个系统进程或线程完成的,该进程称为页淘汰进程。

页淘汰的时机:当内存空闲页面数低于系统所配置的最小阈值时启动(唤醒)页淘汰的进程,页淘汰进程被启动后就开始不停地选择和淘汰释放页,直到内存的空闲页面数达到系统所配置的最大阈值为止。

此后,页淘汰进程进入睡眠(等待)状态,直到下次因内存空闲页面数少于最小阈值而被再次唤醒(启动)。

最近未使用页淘汰(NRU)算法的原理:①该算法为每个页面设置两个硬件位—访问位和修改位访问位= 0:该页尚未被访问过; 访问位= 1:该页已经被访问过修改位= 0:该页尚未被修改过; 访问位= 1:该页已经被修改过②开始时所有页的访问位,修改位都设为0, 访问/修改时再置1。

③当页淘汰进程工作时,首先淘汰那些访问位为0的页。

然后,如果还需要继续淘汰(即空闲页面尚未达到最大阈值),则淘汰那些访问位为1但修改位为0的页。

最后如果空闲页面还不够,则淘汰那些修改位为1的页。

④由于大多数页迟早要被访问,故页淘汰进程定期遍历内存页—将每页的访问位都置为0(周期性地对访问位清零)。

这种清除过程类似于时针在时钟面上的运行故NRU算法又称为时钟(clock)算法。

1.2 简要介绍设计环境、设计工具利用VC++6.0/TC3.0在Dos/Windows平台使用最近未使用页淘汰(NRU)算法模拟实现页淘汰进程二.设计思路和总体流程图2.1 基本思路以命令行方式运行程序,调用read()函数读入页面请求队列,按照页面请求队列的先后顺序逐个处理请求页面。

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

NRU
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _UNISTD_H
#define _UNISTD_H
#include <IO.H>
#include <PROCESS.H>
#endif
#define TRUE 1
#define FALSE 0
#define INVALID -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_h ead,*busypf_tail;
int diseffect, a[total_instruction];
int page[total_instruction], offset[total_instru ction];
int initialize(int);
int NUR(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);
NUR(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=INVALID; /*置页面控制结构中的页号,页面为空*/
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 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==INVALID) /*页面失效*/ {diseffect++;
if(freepf_head==NULL) /*无空闲页面*/
{ cont_flag=TRUE;
old_dp=dp;
while(cont_flag)
if(pl[dp].counter==0&&pl[dp].pfn! =INVALID)
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=INVALID;
freepf_head->next=NULL;
}
pl[page[i]].pfn=freepf_head->pfn;
freepf_head=freepf_head->next;
}
else
pl[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;
}
1.设计目的
通过对页面、页表、地址转换和页面置换过程的模拟,加深对请求调页系统的原理和实现过程的理解。

2.设计内容
1)假设每个页面中可存放10条指令,分配给作业的内存块数为4。

2)用C语言模拟一个作业的执行过程,该作业共有320条指令,即它的地址空间为32页,目前它的所有页都还未调入内存。

在模拟过程中,如果所访问的指令已在内存,则显示其物理地址,并转下一条指令。

如果所访问的指令还未装入内存,则发生缺页,此时需记录缺页的次数,并将相应页调入内存。

如果4个内存块均已装入该作业,则需进行页面置换,最后显示其物理地址,并转下一条指令。

在所有320指令执行完毕后,请计算并显示作业运行过程中发生的缺页率。

3)置换算法:最近最不经常使用(NRU)算法。

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

指令的地址按下述原则生成:
① 50%的指令是顺序执行的;
② 25%的指令是均匀分布在前地址部分;
③ 25%的指令是均匀分布在后地址部分;
具体的实施方法是:
①在[0,319]的指令地址之间随机选取一起
点m;
②顺序执行一条指令,即执行地址为m+1的指令;
③在前地址[0,m+1]中随机选取一条指令并执行,该指令的地址为m′;
④顺序执行一条指令,其地址为m′+1的指令;
⑤在后地址[m′+2,319]中随机选取一条指令并执行;
⑥重复上述步骤①~⑤,直到执行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页。

(3)计算最近最不经常使用(NRU)算法在不同内存容量下的命中率。

其中,命中率=1-页面失效次数/页地址流长度。

相关文档
最新文档