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

合集下载

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

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

实验五. 请求页式存储管理的模拟[实验内容]:熟悉虚拟存储管理的各种页面置换算法,并编写模拟程序实现请求页式存储管理的页面置换算法----最近最久未使用算法(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。

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

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

信息工程学院实验报告课程名称:操作系统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算法的模拟。

页面置换算法实验报告

页面置换算法实验报告

页面置换算法实验报告页面置换算法实验报告一、引言在计算机操作系统中,页面置换算法是一种重要的内存管理策略。

当物理内存不足以容纳所有需要运行的进程时,操作系统需要根据一定的算法将部分页面从内存中换出,以便为新的页面腾出空间。

本实验旨在通过实际操作,对比不同的页面置换算法在不同场景下的性能表现。

二、实验背景在计算机系统中,每个进程都有自己的虚拟内存空间,而物理内存空间是有限的。

当进程需要访问某个页面时,如果该页面不在物理内存中,就会发生缺页中断,操作系统需要根据页面置换算法选择一个页面将其换出,然后将需要访问的页面换入。

常见的页面置换算法有先进先出(FIFO)、最近最久未使用(LRU)、时钟(Clock)等。

三、实验目的本实验旨在通过模拟不同的页面置换算法,比较它们在不同情况下的缺页率和效率。

通过实验结果,评估各个算法在不同场景下的优劣,为实际系统的内存管理提供参考。

四、实验设计与方法本实验选择了三种常见的页面置换算法进行比较:FIFO、LRU和Clock。

我们使用C++编程语言模拟了一个简单的内存管理系统,并通过产生不同的访存序列来模拟不同的场景。

实验中,我们设置了不同的物理内存大小,访存序列长度和页面大小,以模拟不同的系统环境。

五、实验结果与分析在实验中,我们分别测试了FIFO、LRU和Clock算法在不同的系统环境下的表现。

通过统计不同算法的缺页率和运行时间,得出以下结论:1. FIFO算法FIFO算法是最简单的页面置换算法,它按照页面进入内存的顺序进行置换。

实验结果表明,FIFO算法在缺页率方面表现一般,特别是在访存序列具有局部性的情况下,其性能明显下降。

这是因为FIFO算法无法区分不同页面的重要性,可能会将经常使用的页面换出,导致缺页率升高。

2. LRU算法LRU算法是一种基于页面访问时间的置换算法,它认为最近被访问的页面很可能在未来会被再次访问。

实验结果表明,LRU算法在缺页率方面表现较好,特别是在访存序列具有较强的局部性时,其性能明显优于FIFO算法。

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

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

实验 5 页面置换算法模拟实验一.实验目地1.进一步掌握虚拟存储器地实现方法.2.掌握各种页面置换算法.3.比较各种页面置换算法地优缺点. 二.实验内容模拟实现页面置换算法, 步骤为:①使用产生随机数函数得到一个随机地数列, 作为将要载入地页面序列.②使用先进先出vFIFO)算法、最近最久未使用<LRU置换算法和最佳vOPT置换算法, 列出所需淘汰地页面号序列. b5E2RGbCAP③列出缺页中断次数. 三.参考源程序如下:#include vstdio.h> #include vstdlib.h> #include vtime.h> #define N 10 #define B 4/* ------------------------------------------------------ p1EanqFDPw函数名:IslnBuf(>,返回某个数X在不在缓冲Buf[],如在,返回位置,否则返回-1--- */ DXDiTa9E3dint IsInBuf(int buf[],int x>{int i,j=-1 。

for(i=0 o i<B。

i++>{ if(buf[i]==x>{ j=i 。

break 。

}else if(buf[i]==-1>{ buf[i]=x 。

j=i 。

break 。

} } return j 。

}/* ------------------------------------------------------ RTCrpUDGiT函数名:oldest(>, 返回最近最久未使用地页面位置--- */ 5PCzVD7HxAint oldest(int f[]>{int i,j=0,max=-1 。

for(i=0 。

ivB 。

i++>{ if(f[i]>max>{ max=f[i] 。

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

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

实验编号4名称页面置换算法模拟实验目的通过请求页式存储管理中页面置换算法模拟设计,以便:1、了解虚拟存储技术的特点2、掌握请求页式存储管理中页面置换算法实验内容与步骤设计一个虚拟存储区和内存工作区,并使用FIFO和LRU算法计算访问命中率。

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

<程序1>#include <windows.h> //Windows版,随机函数需要,GetCurrentProcessId()需要//#include <stdlib.h>//Linux版,随机函数srand和rand需要#include <stdio.h> //printf()需要#define TRUE 1#define FALSE 0#define INVALID -1#define NULL 0#define total_instruction 320 //共320条指令#define total_vp 32 //虚存页共32页#define clear_period 50 //访问次数清零周期typedef struct{//定义页表结构类型(页面映射表PMT)int pn, pfn, counter, time;//页号、页框号(块号)、一个周期内访问该页面的次数、访问时间}PMT;PMT pmt[32];typedef struct pfc_struct{//页面控制结构int pn, pfn;struct pfc_struct *next;}pfc_type;pfc_type pfc[32];pfc_type *freepf_head,*busypf_head,*busypf_tail;//空闲页头指针,忙页头指针,忙页尾指针int NoPageCount; //缺页次数int a[total_instruction];//指令流数组int page[total_instruction], offset[total_instruction];//每条指令的页和页内偏移void initialize( int );void FIFO( int );//先进先出void LRU( int );//最近最久未使用void NRU( int );//最近最不经常使用/****************************************************************************main()*****************************************************************************/void main(){int i,s;//srand(10*getpid());//用进程号作为初始化随机数队列的种子//Linux版srand(10*GetCurrentProcessId());//用进程号作为初始化随机数的种子//Windows 版s=rand()%320;//在[0,319]的指令地址之间随机选取一起点mfor(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。

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

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

实验目地通过模拟实现请求页式存储管理地几种基本页面置换算法,了解虚拟存储技术地特点,掌握虚拟存储请求页式存储管理中几种基本页面置换算法地基本思想和实现过程,并比较它们地效率.文档收集自网络,仅用于个人学习实验内容设计一个虚拟存储区和内存工作区,并使用下述算法计算访问命中率.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?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 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);10· ····20·· ·30··· ·40/* 指令流长 */ /* 虚页长 */ /*清 0周期*/ /* 页面结构 *//* 页面结构数组 */ /* 页面控制结构 */文档收集自网络,仅用于个人学习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;人学习/* 初始化相关数据结构*/ 文档收集自网络,仅用于个人学习/* 用户进程地内存页面数*/ 文档收集自网络,仅用于个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){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]; return 0;}int FIFO(total_pf) int total_pf;{int i,j; pfc_type *p; initialize(total_pf);/* 空页面队列地头指针为 pfc[0]*//* 先进先出算法 *//* 用户进程地内存页面数 *//* 初始化相关页面控制用数据结构 */busypf_head=busypf_tail=NULL; /* 忙页面队列头,队列尾链接 */ /* 页面失效 *//* 失效次数 */ /* 无空闲页面*/busypf_head=busypf_tail=freepf_head; else {busypf_tail->next=freepf_head; busypf_tail=freepf_head;}freepf_head=p;}elsepl[page[i]].time=present_time;收集自网络,仅用于个人学习}}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++) if(min>pl[j].time&&pl[j].pfn!=INV {/* 找出 time 地最小值 */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 页面/*free 页面减少一个 *///命中则增加该单元地访问次数文档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 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;}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;/* 最近未使用算法 *//* 页面失效 */ /* 无空闲页面 */ALID)}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//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) /* 最不经常使用置换法*/1,i=%d\n",i);文档收集自网络,仅用于个人文档收集自网络,仅用于个人学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 页面}else pl[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 50typedef 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);/* 指令流长 */ /* 虚页长 */ /*清0周期*/ /* 页面结构 *//* 页面结构数组 */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]; return 0;}int FIFO(total_pf)int total_pf;{int i,j;pfc_type *p;initialize(total_pf); /* 空页面队列地头指针为pfc[0]*//* 先进先出算法*//* 用户进程地内存页面数*//* 初始化相关页面控制用数据结构*/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++;min=32767;/* 页面失效*/if(freepf_head==NULL){/* 无空闲页面*/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; }else //有空闲页面,改为有效//减少一个free 页面pl[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 cont_flag=FALSE;else{dp++;if(dp==total_vp)dp=0; /* 最近未使用算法*//* 页面失效*//* 无空闲页面*/ ALID)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; /* 最大"距离" */ 文档收集自网络,仅用于个人学习elsedist[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;}else pl[page[i]].counter++;} printf("LFU:%6.4f\n",1-(float)diseffect/320);return 0;}五、分析1、从几种算法地命中率看, OPT 最高,其次为 NUR 相对较高,而 FIFO 与LRU 相差无几, 最低地是 LFU. 但每个页面执行结果会有所不同 .文档收集自网络,仅用于个人学习//有空闲页面 , 改为有效// 减少一个 free 页面2、OPT 算法在执行过程中可能会发生错误五、思考1、为什么OPT 在执行时会有错误产生?本文来自CSDN 博客,转载请标明出处:/followingturing/archive/2010/12/22/6091196.aspx 文档收集自网络,仅用于个人学习版权申明本文部分内容,包括文字、图片、以及设计等在网上搜集整理。

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

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

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

操作系统实验报告材料6-页面置换算法模拟

操作系统实验报告材料6-页面置换算法模拟

实验报告( 2013 / 2014学年第1学期)课程名称操作系统原理实验名称实验6:页面置换算法模拟实验时间2013 年12 月 10 日指导单位软件工程系指导教师杨健学生姓名班级学号学院(系) 软件工程系专业计算机软件与服务外包//物?理え?块é定¨义?typedef struct BlockNode{int page_index;//page数簓组哩?的?下?标括?struct BlockNode * next;}BlockNode;struct{int length;//当獭?前°物?理え?块é长¤度èint miss_flag;//缺ā?页?标括?志?,?若?为a1,?则ò缺ā?页?int miss_count;//缺ā?页?次?数簓BlockNode*front;BlockNode*rear;}Block;//本?程ì序ò中D全?局?变?量?名?均ù由?两?个?单蹋?词洙?组哩?成é,?且ò开a头?字?母?大洙?写′int BlockSize = 5;//物?理え?块é大洙?小?int PageCount = 200;//页?面?总哩?数簓int PageSize = 1024;//页?面?大洙?小?int AddrRange = 8*1024;//访?问ê地?址·范?围§int get_num(int down,int up)//得?到?一?个?down~up之?间?的?整?数簓{int num;char str[111];while(1){fgets(str,111*sizeof(int),stdin);num=atoi(str);//把?字?符?串?中D的?数簓字?转羇换?为a整?数簓if(num>=down&& num<=up)break;printf("输?入?范?围§有瓺误ó,请?重?新?输?入?:");}//whilereturn num;}void init_block()//构1造ì一?个?空?的?物?理え?块é队ó列{Block.rear=Block.front=(BlockNode*)malloc(sizeof(BlockNode)); if(!Block.front){printf("内ú存?分?配?失骸?败悒?\n");exit(0);}Block.length=0;Block.miss_count=0;Block.rear->next=NULL;}void enqueue(int page_index)//入?队óvoid clear_block()//清?空?物?理え?块é{while(Block.rear=Block.front->next){ Block.front->next=Block.rear->next;free(Block.rear);Block.length--;}Block.rear=Block.front;Block.length=0;Block.miss_count=0;}void destroy_block()//销ú毁ù物?理え?块é{while(Block.rear=Block.front){Block.front=Block.front->next;free(Block.rear);}free(page);}void init_page()//初?始?化ˉ页?面?系μ列int i,j;srand(time(NULL));//用?当獭?前°系μ统?时骸?间?来ぁ?初?始?化ˉ随?机ú种?子哩? page=(struct node_page*) malloc(PageCount*sizeof(struct node_page));for(i=0;i<PageCount;i++){page[i].address=rand()%AddrRange;page[i].page_num=page[i].address/PageSize;}for(i=0;i<PageCount;i++){for(j=i+1;j<PageCount;j++){if(page[i].page_num== page[j].page_num){page[i].next_order=j;break;}//if}//forif(j== PageCount)//说μ明÷page[i]以?后ó都?不?会á再ù访?问êpage[i].next_order= PageCount;}//for}void print_page()//打洙?印?页?面?系μ列{int i;printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); printf("页?面?系μ列为a:阰\n");for(i=0;i<PageCount;i++){printf("[%-2d,%-4d]",page[i].page_num,page[i].address%PageSize); if((i+1)%5== 0){printf("\n");}//if}printf("\n");}void FIFO_Replace(int page_index)//FIFO置?换?{BlockNode*node;if(!Block.length){enqueue(page_index);Block.miss_flag=0;return;}node=Block.front;while(node=node->next){if(page[node->page_index].page_num==page[page_index].page_num){destroy_block(); return 0;}截图。

内存页面置换算法实验报告

内存页面置换算法实验报告

竭诚为您提供优质文档/双击可除内存页面置换算法实验报告篇一:内存页面置换算法的设计实验报告内存页面置换算法的设计一、实验题目:实现最近最久未使用(LRu)置换算法二、实验目的:LInux中,为了提高内存利用率,提供了内外存进程对换机制,内存空间的分配和回收均以页为单位进行,一个进程只需将其一部分调入内存便可运行,还支持请求调页的存储管理方式。

本实习要求通过请求页式存储管理中页面置换算法模拟设计,了解虚拟存储技术的特点,掌握请求页式存储管理的页面置换算法。

三、实验原理:最近最久未使用(LRu)置换算法原理就是:当需要淘汰某页面时,选择当前一段时间内最久未使用过的页先淘汰,即淘汰距当前最远的上次使用的页。

例如:分配给该进程的页块数为3,一个20位长的页面访问序列为:12560,36536,56042,70435,则缺页次数和缺页率按下图给出:2.假定分配给该进程的页块数为3,页面访问序列长度为20。

本实验可以采用数组结构实现,首先随机产生页面序列,当发生请求调页时,若内存已满,则需要利用LRu算法,将当前一段时间内最久未使用过的页替换出去。

模拟程序的算法如下图:四、数据结构:数组五、程序代码:package页面置换;publicclassLRu{staticintblocknum=3;//页面数staticintlistnum=20;//序列页数staticintpageorder[]={7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};的访问页面序列staticinttime[]=newint[blocknum];用的//随机生成//记录最近使staticintpageFrame[][]=newint[listnum][blocknum];//模拟页面置换staticintcount=0;//记录缺页数booleanfound;publicstaticvoidshow(){for(inti=0;isystem.out.print(pageorder[i]+"");system.out.println("");for(inti=0;i system.out.print("--");system.out.println("");for(intj=0;j for(inti=0;iif(pageFrame[i][j]==-1)system.out.print(+"");elsesystem.out.print(pageFrame[i][j]+"");}system.out.println("");}system.out.println("缺页数:"+count);}publicstaticvoidinit(){for(inti=0;i for(intj=0;jpageFrame[i][j]=-1;}}count=1;}publicstaticvoidLeastused(){init();pageFrame[0][0]=pageorder[0];inttemp,flag=0;time[0]=0;inti,j,k;for(i=1;i for(j=0;jif(pageorder[i]==pageFrame[flag][j]){//(与前一行的每一块比较)time[j]=i;break;}}if(j!=blocknum)//若该页面已经在内存中就跳出此次for循环进入下一个页面continue;for(k=0;k if(pageFrame[flag][k]==-1)break;elsepageFrame[i][k]=pageFrame[flag][k];}for(j=0;j if(pageFrame[i][j]==-1){//是否还有可用的块空间来分配给页面pageFrame[i][j]=pageorder[i];time[j]=i;count++;flag=i;break;}}if(j!=blocknum)//说明已经有空间放置该页面则已经分配好,跳出此次for循环continue;temp=0;for(j=0;j //很久不被访问的页面,即time值最小的那一个if(time[temp]>time[j])temp=j;}pageFrame[i][temp]=pageorder[i];time[temp]=i;count++;flag=i;}}}publicstaticvoidmain(string[]args){Leastused ();show();}六、运行结果:七、实验心得:这次的实验是内存页面的置换,相较上次的内存分配管理在算法实现上简单一些,但是代码不足在于使用到了较多的static全局变量使得整个代码质量不是很好,而且也只是简单的根据算法设计来模拟实现整个过程。

页面置换算法模拟

页面置换算法模拟

实验四页面置换算法模拟(1)一、实验目的1、理解虚拟存储器概念。

2、掌握分页式存储管理地址转换和缺页中断。

二、实验内容与基本要求1、模拟分页式存储管理中硬件的地址转换和产生缺页中断。

2、用先进先出页面调度算法处理缺页中断。

三、实验报告内容1、分页式存储管理和先进先出页面调度算法原理。

a.分页式存储管理原理在存储器管理中,连续分配方式会形成许多“碎片”,虽然可通过“紧凑”方法将许多碎片拼接成可用的大块空间,但须为之付出很大开销。

如果允许将一个进程直接分散地装入到许多不相邻的分区中,则无须再进行“紧凑”。

基于这一思想而产生了离散分配方式。

如果离散分配的基本单位是页,则称为分页存储管理方式。

在分页存储管理方式中,如果不具备页面对换功能,则称为基本分页存储管理方式,或称为纯分页存储管理方式,它不具有支持实现虚拟存储器的功能,它要求把每个作业全部装入内存后方能运行。

请求式分页系统是建立在基本分页基础上的,为了能支持虚拟存储器功能,而增加了请求调页功能和页面置换功能。

b.先进先出页面调度算法原理优先淘汰最早进入内存的页面,亦即在内存中驻留时间最久的页面。

该算法实现简单,只需把调入内存的页面根据先后次序链接成队列,设置一个指针总指向最早的页面。

但该算法与进程实际运行时的规律不适应,因为在进程中,有的页面经常被访问。

2、程序流程图。

3、程序及注释。

(完善程序:数据能动态的,少了越界错误的判断(页号小于页长,页内地址小于页的大小))#include<cstdio>#include<cstring>#define SizeOfPage 128#define SizeOfBlock 128#define M 4struct info//页表信息结构体{bool flag; //页标志,1表示该页已在主存,0表示该页不在主存long block;//块号long disk;//在磁盘上的位置bool dirty;//更新标志}pagelist[SizeOfPage];long po;//队列标记long P[M];//假设内存中最多允许M=4个页面void init_ex1() //内存空间初始化。

操作系统实验报告_页面置换算法模拟

操作系统实验报告_页面置换算法模拟

操作系统实验报告_页面置换算法模拟学生实验报告姓名: 年级专业班级学号成绩验证设计实验3 请求分页系统的页面实验类型课程名称实验名称操作系统综合创新置换算法【实验目的、要求】1.通过编程实现请求分页存储管理系统的Optimal、FIFO、LRU调度算法,使学生掌握计算机虚拟存储管理中有关缺页处理方法等内容,巩固有关虚拟存储管理的知识。

2.了解Windows2000/XP中内存管理机制,掌握页式虚拟存储技术。

3.理解内存分配原理,特别是以页面为单位的虚拟内存分配方法。

【实验内容】在Windows XP或Windows 2000等操作系统环境下,使用VC、VB、Delphi、java或C等编程语言,实现请求分页存储管理系统的Optimal、FIFO、LRU调度算法。

【实验环境】(含主要设计设备、器材、软件等)计算机 C语言编程软件【实验步骤、过程】(含原理图、流程图、关键代码,或实验过程中的记录、数据等)1.启动计算机,运行C语言编程软件。

2.分析理解页面的几种基本算法的特点和原理,在纸上画出原理图。

3.编辑源程序,关键代码如下。

(1)先进先出页面置换算法。

#include<stdio.h>void main(){int i,n,t,k=3,a[100];scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&a[i]);for(i=3;i<n;i++)if(a[i]!=a[0]&&a[i]!=a[1]&&a[i]!=a[2]) //该页面在内存中,不需要置换。

{t=a[i];a[i]=a[k%3]; //通过对k值对3取余的值来确定需要置换的当前页面。

a[k%3]=t;k++; //仅当发生了页面置换时,k的值才发生改变。

printf("%d %d %d\n",a[0],a[1],a[2]);}else{printf("%d %d %d\n",a[0],a[1],a[2]);}}(2)最佳置换算法#include<stdio.h>void main(){int i,j,,n,a[100];int c1,c2,c3; // 标志该页面再次被访问时在引用串中的位置int p,k,r;printf("请输入页面数:\n");scanf("%d",&n);printf("请输入页面号引用串:\n");for(i=0;i<n;i++)scanf("%d",&a[i]);for(j=3;j<n;j++){if((a[j]!=a[0])&&(a[j]!=a[1])&&(a[j]!=a[2])) //页面在内存不发生置换~{for(p=j;p<n;p++)if(a[0]==a[p]){ c1=p;break; //跳出循环,直接置c1=n!} else c1=n; //标志该页面再次被访问时在引用串中的位置~若该页面不会再次被访问,则将c1置为最大n!for(k=j;k<n;k++)if(a[1]==a[k]){ c2=k;break; }elsec2=n;for(r=j;r<n;r++)if(a[2]==a[r]){ c3=r;break;}else c3=n; //通过比较c1,c2,c3的大小确定最长时间内不再访问的页面~if((c1>c2)&&(c1>c3)||(c1==c3)||(c1==c2)) //当前a[0]页面未来最长时间不再访问!{t=a[j];a[j]=a[0];a[0]=t; //把当前访问页面和最佳页面交换~printf("%d %d %d\n",a[0],a[1],a[2]);}if((c2>c1)&&(c2>c3)||(c2==c3)) //当前a[1]页面未来最长时间不再访问!{t=a[j];a[j]=a[1];a[1]=t;printf("%d %d %d\n",a[0],a[1],a[2]);}if((c3>c1)&&(c3>c2)) //当前a[2]页面未来最长时间不再访问!{t=a[j];a[j]=a[2];a[2]=t;printf("%d %d %d\n",a[0],a[1],a[2]); //输出置换后页框中的物理块组成~}}elseprintf("%d %d %d\n",a[0],a[1],a[2]);}}(3)LRU算法。

计算机操作系统实验三页面置换算法模拟实验

计算机操作系统实验三页面置换算法模拟实验

计算机操作系统实验三页面置换算法模拟实验计算机工程学院实验报告书课程名:《操作系统原理A》题目:虚拟存储器管理页面置换算法模拟实验班级:学号:姓名:评语:成绩:指导教师:批阅时间:年月日一、实验目的与要求1.目的:请求页式虚存管理是常用的虚拟存储管理方案之一。

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

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

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

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

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

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

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

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

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

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

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

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

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

相关主题
  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。

相关文档
最新文档