实验七请求页式存储管理中常用页面置换算法模拟
- 格式:doc
- 大小:21.50 KB
- 文档页数:8
实验编号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 INV ALID -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、设计两个程序模拟实现一个作业在内存中执行的页面置换,并计算缺页中断次数。
3、编制两种页面置换算法:1)FIFO页面置换算法;2)LRU页面置换算法四、实验原理:1、FIFO页面置换算法:总是选择在内存中驻留时间最长的一页将其淘汰。
2、LRU页面置换算法:选择最近一段时间内最长时间没有被访问过的页面予以淘汰。
五、参考程序:(提供代码,未改动)1、FIFO页面置换算法:#define M 4 /*m为系统分配给作业的主存中的页面数*/#define N 15#include <stdio.h>void main(){int a[M];/*定义内存页面数*/int b[N];/*定义总页面数*/int c[N];/*定义被淘汰的页面号*/int i,k,flag,count,m=0;printf("请输入作业序号:\n");for(i=0;i<N;i++) /*输入作业依次要访问的页号*/scanf("%d",&b[i]);printf("发生缺页的面号分别为:");for(i=0;i<M;i++)/*输出发生缺页中断的初始页号共m块*/ { a[i]=b[i];printf("%3d,",a[i]);}count=M;for(i=M;i<N;i++){ flag=0;for(k=0;k<M;k++)if(a[k]==b[i])flag=1;if(flag==0){ c[m]=a[0];m++;for(k=0;k<M-1;k++)a[k]=a[k+1];a[M-1]=b[i];count++;printf("%3d,",b[i]);}}printf("\n发生缺页的次数=%d\n",count);printf("\n缺页中断率=%.2f%%%\n",(float)count/N*100); printf("\n驻留内存的页号分别为:");for(i=0;i<M;i++)printf("%3d,",a[i]);printf("\n被淘汰的页号分别为:");for(i=0;i<m;i++)printf("%3d,",c[i]);}2、LRU页面置换算法:#define M 4 /*m为在主存中的页面数*/#define N 15#include <stdio.h>void main(){int a[M];/*定义内存页面数*/int b[N];int c[N];/*定义被淘汰的页面号*/int i,j,k,count,flag,m=0;printf("请输入作业序号:\n");for(i=0;i<N;i++)/*输入作业依次访问的页号*/scanf("%d",&b[i]);printf("发生缺页的面号分别为:\n");for(i=0;i<M;i++){ a[i]=b[i];printf("%3d",a[i]);}count=M;在此处将程序补充完整printf("\n发生缺页的次数=%d\n",count);printf("\n缺页中断率=%.2f%%%\n",(float)count/N*100);printf("\n驻留内存的页号分别为:");for(i=0;i<M;i++)printf("%d,",a[i]);printf("\n被淘汰的页号分别为:");for(i=0;i<m;i++)printf("%3d,",c[i]);}六、源程序:1、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是页地址流长度。
计算机操作系统实验报告济南大学信息科学与技术学院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。
《计算机操作系统》课外实践报告题目:页面置换算法模拟班级: 13级物联网工程一班姓名:王铎学号: 130911044指导老师: 王蕾设计时间: 2014.6一、实验目标:通过设计请求页面存储管理中页面置换算法模拟设计,了解虚拟存储技术的特点,掌握请求页面存储管理的页面置换算法。
二、实验要求1通过随机数产生一个指令序列 共320条指令。
指令的地址按下述原则生成50%的指令是顺序执行的25%的指令是均匀分布在前地址部分25%的指令是均匀分布在地址部分。
2将指令序列换成为页地址流。
3计算并输出下述各种算法在不同内存容量下的命中率。
(1)先进先出的算法 FIFO(2)最近最少使用算法 LRU(3)最近最不经常使用算法 NUR三.实践内容简要描述1、实践环境windows XP/7,visual C++ 6.02、算法思路与算法原理2.1、先进先出算法(FIFO):该算法总是淘汰最先进入内存的页面,即选择在内存中驻留时间最久的页面予以淘汰。
该算法实现简单,只需要把一个进程已调入内存的页面,按先后次序连接成一个队列,并设置一个指针,成为替换指针,使它总是指向最老的页面。
2.2、最近最久未使用页面置换算法(LRU):算法的基本思想:当需要淘汰某一页时,选择离当前时间最近一段时间内最久没有使用过的页先淘汰。
该算法的出发点是,如果某页被访问了,则它可能马上还被访问。
或者反过来说,如果某页长时间未被访问,则它在最近一段时间不会被访问。
3概要设计3.1 总体设计框图4系统流程图图4.1详细设计框图图4.2为置换方法的流程图5代码分析结果5.1 数据结构int m, int need[],int n, result[10][20],int state[],int count1[];5.2 FIFO具体函数及设计实现FIFO流程图FIFO函数实现void FIFO(int m, int need[],int n) //m分配物理页面大小,n需要页面数组的最大值{int p=0; //当前请求的页面int del=0; //步数int count1[20];double count=0; //缺页数double que=0; //缺页率int result[10][20]; //结果表for(int i =0;i<=m;i++)for(int j=0;j<=n;j++){result[i][j]=-1;count1[j]=-1;}while(n>=p){int k=need[p];if(p>0){for(int i=0;i<=m;i++){result[i][p]=result[i][p-1];}}int f1=0;//首先看是否有空位置或者已经存在请求的页面for(int i =0;i<=m;i++){if(result[i][p]==-1){result[i][p]=k;f1=1;i=m+1;count1[p]=k;count=count+1;p=p+1;}else if(result[i][p]==k){f1=1;p=p+1;i=m+1;}}if(f1==1) continue;//这里发生替换result[del][p]=k;count1[p]=k;count=count+1;p=p+1;del=(del+1)%(m+1);}cout<<"*******************FIFO过程如下表************************"<<endl; for(int t3=0;t3<=n;t3++)//输出原来的页面cout<<need[t3]<<" ";cout<<endl;for(int t0 =0;t0<=m;t0++)//判断页表是否填满{for(int t1=0;t1<=n;t1++){if(result[t0][t1]!=-1)cout<<result[t0][t1]<<" ";else cout<<" "<<" ";}cout<<endl;}for(int j1=0;j1<=n;j1++)//对于缺页的打×,否则打√{if(count1[j1]!=-1)cout<<"×";else cout<<"√";}cout<<endl;que=count/(n+1);//统计缺页次数和缺页率cout<<"缺页次数为:"<<count<<endl;cout<<"缺页率"<<count<<"/"<<n+1<<"="<<que<<endl;cout<<"**************************************************"<<endl;}5.3LRU具体函数及设计实现LRU流程图LRU函数实现void LRU(int m, int need[],int n){m++;n++;int i, j, min, num = 1, k, flag;int state[10], count[20], hsive[10];int result[10][20];memset(state, 0, sizeof(state));//初始化内存空间,给三个数组分配内存memset(count, -1, sizeof(count));memset(hsive, -1, sizeof(hsive));memset(result, -1, sizeof(result));for(i = 0; i < n; i++)//将need数组值赋给resultresult[0][i] = need[i];cout<<"*****************LRU过程如下表*********************"<<endl;for(i = 0; i < n; i++){flag = 0;//标志位,如果页面和页表内的熄灯则赋值for(j = 1; j < num; j++){if(result[0][i] == hsive[j]){flag = 1;state[j] = -1;break;}}if(flag == 0)//替换{if(num <= m)hsive[num] = result[0][i];else{min = -1;for(j = 1; j <= m; j++){if(state[j] > min){k = j;min = state[j];}}hsive[k] = result[0][i];state[k] = -1;}count[i] = 1;num++;}for(j = 1; j <= m; j++){result[j][i] = hsive[j];state[j]++;}}for(j = 0; j <= m; j++)//输入个页面替换情况{for(i = 0; i < n; i++){if(result[j][i] == -1)cout << " ";elsecout << result[j][i] << " ";}cout << endl;}for(i = 0; i < n; i++){if(count[i] != -1)cout<<"×";elsecout<<"√";}cout << endl;//统计各页面缺页次数和缺页率cout << "缺页次数为:" << num - 1 << endl;cout << "缺页率" << num - 1 << "/"<<n << "=" << double(num - 1) / n <<endl;cout<<"**************************************************"<<endl; }主方法int main(){cout<<" *********************************************"<<endl;cout<<" * 页式存储管理*"<<endl;cout<<" ********************************************* "<<endl;int m;int n=0;int choose=2;int need[20];char flag;while(1){cout<<"指定内存分配页面数:";while (flag<'0'||flag>'9'){cin>>flag;}m=flag-'0'-1;flag=' ';cout<<"请选择页面序列产生方式:"<<endl;cout<<" (0)手动输入"<<endl;cout<<" (1)随机产生"<<endl;while (flag<'0'||flag>'1'){cin>>flag;}choose =flag-'0';flag=' ';if(choose==0){cout<<"输入页面走向!以s结尾"<<endl;while(1){while ((flag<'0'||flag>'9')&&flag!='s'){cin>>flag;}if(flag=='s') break;need[n]=flag-'0';flag=' ';n=n+1;}flag=' ';n=n-1;}else {cout<<"随机产生的页面个数:";cin>>n;n=n-1;for(int i=0;i<=n;i++){need[i]=rand()%10;}}system("cls");cout<<"选择页面置换算法:"<<endl;cout<<"0-FIFO 1-LRU"<<endl;while (flag<'0'||flag>'1'){cin>>flag;}choose =flag-'0';flag=' ';if(choose==0){FIFO(m, need,n);}else {LRU(m, need,n);}cout<<"输入Y/y可以看另外一种置换算法的执行过程"<<endl;cin>>flag;if(flag=='Y'||flag=='y'){system("cls");if(choose==0) LRU(m, need,n);else FIFO(m, need,n);}else flag=' ';cout<<"输入N/n退出否则输入任意键继续"<<endl;cin>>flag;if(flag=='N'||flag=='n') break;else{system("cls");flag=' ';}}return 0;}5.4调用关系图6测试6.1进入界面及产生页面走向运行结果如下图6.1 进入管理界面图6.2产生页面走向6.2FIFO算法及查看结果图6.3选择FIFO算法结果6.3LRU算法及查看结果图6.4选择LRU结果6.4继续进入主界面及产生页面走向图6.5按y继续回到主界面设置页面为46.5调度算法及结果图6.6选择LRU算法结果图6.7选择FIFO算法结果7 总结与体会通过这次实验,我基本了解了页面置换算法的基本设置与要求,懂得了先进先出算法(FIFO)、最近最久未使用页面置换算法(LRU)的基本编程及算法原理。
请求页式管理中的置换算法页式管理是计算机内存管理的一种方式,它利用虚拟内存和分页技术使得操作系统可以在有限的物理内存下运行更多的进程。
在页式存储管理中,操作系统将物理内存分成若干个固定的大小的块,称为物理块,同时将进程分成若干个大小相等的块,称为页面。
当一个进程需要访问某个页面时,操作系统就将页面调入物理块中,若所有物理块都被占满,则需要选择一个物理块将其替换成即将调入的页面,这就需要使用到置换算法。
常见的置换算法有如下几种:1. 先进先出算法(FIFO)该算法是最简单的、最易实现的一种页面置换算法。
其原理是将先进入内存的页面优先替换出去。
使用一个先进先出的队列来维护物理块的使用情况,每次选择队列头的页面进行置换。
因此该算法的缺点就是无法考虑页面替换的实际使用情况。
2. 最近最少使用算法(LRU)该算法会将最近未被使用的页面优先置换出去,即在内存中驻留时间最长的页面最不容易被替换出去。
其难点在于如何确定何时页面被使用过。
可以使用栈或者链表来实现。
3. 最不常用算法(LFU)该算法会将内存中使用次数最少的页面优先置换出去,即在内存中使用次数最少的页面最容易被替换出去。
为实现该算法,需要维护每个页面的使用次数。
4. 时钟算法(Clock)该算法是一种Clock页面置换算法,在物理块使用情况时采用了一个环形链表的结构。
每个页面都有一个标志位,若该位为1,则说明该页面在当前的一轮中被使用过,同时将该位变为0;若该位为0,则说明该页面未被使用过,这时候会考虑是否将该页面替换出去。
5. 二次机会算法(Second chance)该算法对时钟算法进行了改进,增加了一个修改位,表明是否被修改过。
每次选择的是位0(未被使用/未被修改)或者1(未被使用/被修改)的页面,如果选择到的是1则把修改位清零,并转化为0重新加入内存块队列。
以上是常见的五种页面置换算法,不同的算法有各自的优缺点,需要根据内存管理的需要、机器性能和系统配置来选择合适的算法来实现。
实验目地通过模拟实现请求页式存储管理地几种基本页面置换算法,了解虚拟存储技术地特点,掌握虚拟存储请求页式存储管理中几种基本页面置换算法地基本思想和实现过程,并比较它们地效率.文档收集自网络,仅用于个人学习实验内容设计一个虚拟存储区和内存工作区,并使用下述算法计算访问命中率.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 文档收集自网络,仅用于个人学习版权申明本文部分内容,包括文字、图片、以及设计等在网上搜集整理。
淮海工学院计算机科学系实验报告书课程名:《操作系统原理》题目:虚拟存储器管理页面置换算法模拟实验班级:学号:姓名:一、实验目的与要求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%。
湖南科技学院计算机与信息科学系实验报告实验名称请求页式存储管理中常用页面置换算法模拟课程名称计算机操作系统所属系部班级计科0902时间2011年12地E305月8日第3,4节点姓名姜泽政学号200908001217成绩本组成员(一人一组)一、实验要求1、熟练掌握页面请求算法的原理;2、熟练掌握页面置换算法的思路;3、上机前编写程序源码;4、上机后,写好实验报告和实验结果,实验报告交任课教师。
二、实验目的页式虚拟存储器实现的一个难点是设计页面调度(置换)算法,即将新页面调入内存时,如果内存中所有的物理页都已经分配出去,就要按某种策略来废弃某个页面,将其所占据的物理页释放出来,供新页面使用。
本实验的目的是通过编程实现几种常见的页面调度(置换)算法,加深读者对页面思想的理解三、实验环境每人一台电脑,在下实现。
四、实验内容及过程(1)设计程序实现以上三种页面调度算法,要求:①.可以选择页面调度算法类型;②.可以为进程设置分到物理页的数目,设置进程的页面引用情况,可以从键盘输入页面序列,也可从文件中读取;③.随时计算当前的页面调度次数的缺页中断率;④.使用敲键盘或响应WM-TIMER的形式模仿时间的流逝;⑤.以直观的的形式将程序的执行情况显示在计算机屏幕上;⑥.存盘及读盘功能,可以随时将数据存入磁盘文件,供以后重复实验时使用。
(2)假定进程分配到3个物理块,对于下面的页面引用序列:7-0-1-2-0-3-0-4-2-3-0-3-2-1-2-0-1-7-0-1请分别用先进和先出调度算法,最近最少用调度算法,最近最不常用调度算法计算缺页中断次数,缺页中断率和缺页调度次数、缺页置换率。
再假定进程分配到4、5个物理块,重复本实验。
(3)假定进程分配到3个物理块,对于下面的页面引用序列:4-3-2-1-4-3-5-4-3-2-1-5-0-7-3-8-9-0-2-1-4-7-3-9请分别用先进先出调度算法、最近最少用调度算法,最近最不常用调度算法计算缺页中断次数,缺页中断率和缺页调度次数、缺页置换率。
实验七请求页式存储管理中常用页面置换算法模拟实验学时:4实验类型:设计实验要求:必修一、实验目的(1)了解内存分页管理策略(2)掌握调页策略(3)掌握一般常用的调度算法(4)学会各种存储分配算法的实现方法。
(5)了解页面大小和内存实际容量对命中率的影响。
二、实验内容(1)采用页式分配存储方案,通过分别计算不同算法的命中率来比较算法的优劣,同时也考虑页面大小及内存实际容量对命中率的影响;(2)实现OPT 算法(最优置换算法) 、LRU 算法(Least Recently) 、FIFO 算法(First IN First Out)的模拟;(3)会使用某种编程语言。
三、实验原理分页存储管理将一个进程的逻辑地址空间分成若干大小相等的片,称为页面或页。
在进程运行过程中,若其所要访问的页面不在内存而需把它们调入内存,但内存已无空闲空间时,为了保证该进程能正常运行,系统必须从内存中调出一页程序或数据,送磁盘的对换区中。
但应将哪个页面调出,须根据一定的算法来确定。
通常,把选择换出页面的算法称为页面置换算法(Page_Replacement Algorithms)。
一个好的页面置换算法,应具有较低的页面更换频率。
从理论上讲,应将那些以后不再会访问的页面换出,或将那些在较长时间内不会再访问的页面调出。
1、最佳置换算法OPT(Optimal)它是由Belady于1966年提出的一种理论上的算法。
其所选择的被淘汰页面,将是以后永不使用的或许是在最长(未来)时间内不再被访问的页面。
采用最佳置换算法,通常可保证获得最低的缺页率。
但由于人目前还无法预知一个进程在内存的若干个页面中,哪一个页面是未来最长时间内不再被访问的,因而该算法是无法实现的,便可以利用此算法来评价其它算法。
2、先进先出(FIFO)页面置换算法这是最早出现的置换算法。
该算法总是淘汰最先进入内存的页面,即选择在内存中驻留时间最久的页面予以淘汰。
该算法实现简单只需把一个进程已调入内存的页面,按先后次序链接成一个队列,并设置一个指针,称为替换指针,使它总是指向最老的页面。
实验四请求分页存储管理模拟实验一:实验目的通过对页面、页表、地址转换和页面置换过程的模拟,加深对请求分页存储管理系统的原理和实现技术的理解;二:实验内容假设每个页面可以存放10条指令,分配给进程的存储块数为4;用C语言或Pascal语言模拟一进程的执行过程;设该进程共有320条指令,地址空间为32个页面,运行前所有页面均没有调入内存;模拟运行时,如果所访问的指令已经在内存,则显示其物理地址,并转下一条指令;如果所访问的指令还未装入内存,则发生缺页,此时需要记录缺页产生次数,并将相应页面调入内存,如果4个内存块已满,则需要进行页面置换;最后显示其物理地址,并转下一条指令;在所有指令执行完毕后,显示进程运行过程中的缺页次数和缺页率;页面置换算法:分别采用OPT、FIFO、LRU三种算法;进程中的指令访问次序按如下原则生成:50%的指令是顺序执行的;25%的指令是均匀分布在低地址部分;25%的指令是均匀分布在高地址部分;三:实验类别分页存储管理四:实验类型模拟实验五:主要仪器计算机六:结果OPT:LRU: FIFO:七:程序include<>include<>include<>define blocknum 4agenum=-1;blocki.accessed=0;m=0;}}int pageExistint curpageagenum == curpagereturn i; agenum==-1return i; ccessed > blockpos.accessedpos = i; agenum = -1{printf" %02d ",blocki.pagenum;printf"%p |",&blocki.pagenum;}}printf"\n";}void randamagenum = curpage; agenum= numj/10{blockk.accessed = 1000;} ccessed = j;break;}}}position = findReplace;agenum = curpage;agenum = curpage; agenum = curpage;display;n++; ccessed = -1;ccessed++;}}printf"缺页次数:%d\n",n;printf"缺页率:%f%%\n",n/100;}void FIFO{int n=0;agenum=curpage; agenum = curpage; //将此页面调入内存n++;display;}}}printf"缺页次数:%d\n",n;printf"缺页率:%f%%\n",n/100;}void main{int choice;printf"请求分页存储管理模拟系统\n";randam;printf"此进程的页面调用序列如下\n";pagestring;whilechoice = 4{printf"1:OPT 2:LRU 3:FIFO 4:退出\n";printf"请选择一种页面置换算法:";scanf"%d",&choice;init;switchchoice{case 1:printf"最佳置换算法OPT:\n";printf"页面号物理地址页面号物理地址页面号物理地址页面号物理地址\n";OPT;break;case 2:printf"最近最久未使用置换算法LRU:\n";printf"页面号物理地址页面号物理地址页面号物理地址页面号物理地址\n";LRU;break;case 3:printf"先进先出置换算法FIFO:\n";printf"页面号物理地址页面号物理地址页面号物理地址页面号物理地址\n";FIFO;break;}}}。
操作系统实验报告_页面置换算法模拟学生实验报告姓名: 年级专业班级学号成绩验证设计实验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算法。
实验七请求页式存储管理中常用页面置换算法模拟实验七请求页式存储管理中常用页面置换算法模拟实验学时:4实验类型:设计实验要求:必修一、实验目的(1)了解内存分页管理策略(2)掌握调页策略(3)掌握一般常用的调度算法(4)学会各种存储分配算法的实现方法。
(5)了解页面大小和内存实际容量对命中率的影响。
二、实验内容(1)采用页式分配存储方案,通过分别计算不同算法的命中率来比较算法的优劣,同时也考虑页面大小及内存实际容量对命中率的影响;(2)实现OPT 算法 (最优置换算法) 、LRU 算法 (Least Recently) 、 FIFO 算法 (First IN First Out)的模拟;(3)会使用某种编程语言。
三、实验原理分页存储管理将一个进程的逻辑地址空间分成若干大小相等的片,称为页面或页。
在进程运行过程中,若其所要访问的页面不在内存而需把它们调入内存,但内存已无空闲空间时,为了保证该进程能正常运行,系统必须从内存中调出一页程序或数据,送磁盘的对换区中。
但应将哪个页面调出,须根据一定的算法来确定。
通常,把选择换出页面的算法称为页面置换算法(Page_Replacement Algorithms)。
一个好的页面置换算法,应具有较低的页面更换频率。
从理论上讲,应将那些以后不再会访问的页面换出,或将那些在较长时间内不会再访问的页面调出。
1、最佳置换算法OPT(Optimal)它是由Belady于1966年提出的一种理论上的算法。
其所选择的被淘汰页面,将是以后永不使用的或许是在最长(未来)时间内不再被访问的页面。
采用最佳置换算法,通常可保证获得最低的缺页率。
但由于人目前还无法预知一个进程在内存的若干个页面中,哪一个页面是未来最长时间内不再被访问的,因而该算法是无法实现的,便可以利用此算法来评价其它算法。
2、先进先出(FIFO)页面置换算法这是最早出现的置换算法。
该算法总是淘汰最先进入内存的页面,即选择在内存中驻留时间最久的页面予以淘汰。
该算法实现简单只需把一个进程已调入内存的页面,按先后次序链接成一个队列,并设置一个指针,称为替换指针,使它总是指向最老的页面。
3、最近最久未使用置换算法(1)LRU(Least Recently Used)置换算法的描述FIFO置换算法性能之所以较差,是因为它所依据的条件是各个页面调入内存的时间,而页面调入的先后并不能反映页面的使用情况。
最近最久未使用(LRU)置换算法,是根据页面调入内存后的使用情况进行决策的。
由于无法预测各页面将来的使用情况,只能利用“最近的过去”作为“最近的将来”的近似,因此,LRU置换算法是选择最近最久未使用的页面予以淘汰。
该算法赋予每个页面一个访问字段,用来记录一个页面自上次被访问以来所经历的时间t,,当须淘汰一个页面时,选择现有页面中其t值最大的,即最近最久未使用的页面予以淘汰。
(2)LRU置换算法的硬件支持LRU置换算法虽然是一种比较好的算法,但要求系统有较多的支持硬件。
为了了解一个进程在内存中的各个页面各有多少时间未被进程访问,以及如何快速地知道哪一页是最近最久未使用的页面,须有以下两类硬件之一的支持:a)寄存器为了记录某个进程在内存中各页的使用情况,须为每个在内存中的页面配置一个移位寄存器,可表示为R=RRR……RRR当进程访问某物理块时,要将相应寄存器的Rn-1位置成1。
n-1n-2n-3210此时,定时信号将每隔一定时间(例如100ms)将寄存器右移一位。
如果我们把n位寄存器的数看作是一个整数,那么具有最小数值的寄存器所对应的页面,就是最近最久未使用的页面。
如图1示出了某进程在内存中具有8个页面,为每个内存页面配置一个8位寄存器时的LRU访问情况。
这里,把8个内存页面的序号分别定为1――,8。
由图可以看出,第7个内存页面的R值最小,当发生缺页时首先将它置换出去。
R7 R6 R5 R4 R3 R2 R1 R01 0 1 0 1 0 0 1 02 1 0 1 0 1 1 0 03 0 0 0 0 0 1 0 04 0 1 1 0 1 0 1 15 1 1 0 1 0 1 1 06 0 0 1 0 1 0 1 17 0 0 0 0 0 1 1 18 0 1 1 0 1 1 0 1b)栈可利用一个特殊的栈来保存当前使用的各个页面的页面号。
每当进程访问某页面时,便将页面的页面号从栈中移出,将它压入栈顶。
因此,栈顶始终是最新被访问页面的编号民,而栈底则是最近最久未使用的页面的页面号。
四、程序清单参考实验步骤如下:// 现定义数据结构和全局变量。
#include<stdio.h>#include<conio.h>#define M 4#define N 17#define Myprintf printf("|---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---|\n") /*表格控制*/typedef struct page{ int num; /*记录页面号*/int time; /*记录调入内存时间*/}Page; /* 页面逻辑结构,结构为方便算法实现设计*/Page b[M]; /*内存单元数*/int c[M][N]; /*暂保存内存当前的状态:缓冲区*/ int queue[100]; /*记录调入队列*/int K; /*调入队列计数变量*///初始化内存单元、缓冲区void Init(Page *b,int c[M][N]){ int i,j;for(i=0;i<N;i++){ b[i].num=-1;b[i].time=N-i-1; }for(i=0;i<M;i++)for(j=0;j<N;j++)c[i][j]=-1;}//取得在内存中停留最久的页面,默认状态下为最早调入的页面 int GetMax(Page *b) { int i;int max=-1;int tag=0;for(i=0;i<M;i++){ if(b[i].time>max){ max=b[i].time;tag=i; } }return tag;}//判断页面是否已在内存中int Equation(int fold,Page *b){ int i;for(i=0;i<M;i++)if (fold==b[i].num) return i;return -1;}//LRU算法void Lru(int fold,Page *b) { int i;int val;val=Equation(fold,b);if (val>=0){ b[val].time=0;for(i=0;i<M;i++)if (i!=val) b[i].time++;}else{ queue[++K]=fold;/*记录调入页面*/val=GetMax(b);b[val].num=fold;b[val].time=0;for(i=0;i<M;i++)if (i!=val) b[i].time++;}}//FIFO与OPT的算法描述省略。
//主程序void main(){int a[N]={1,0,1,0,2,4,1,0,0,8,7,5,4,3,2,3,4}; int i,j;start:K=-1;Init(b, c);for(i=0;i<N;i++){ Lru(a[i],b);c[0][i]=a[i];/*记录当前的内存单元中的页面*/for(j=0;j<M;j++)c[j][i]=b[j].num;}/*结果输出*/printf("内存状态为:\n");Myprintf;for(j=0;j<N;j++)printf("|%2d ",a[j]);printf("|\n");Myprintf;for(i=0;i<M;i++){ for(j=0;j<N;j++)if(c[i][j]==-1) printf("|%2c ",32); elseprintf("|%2d ",c[i][j]);printf("|\n");}Myprintf;printf("\n调入队列为:");for(i=0;i<K+1;i++)printf("%3d",queue[i]);printf("\n缺页次数为:%6d\n缺页率:%16.6f",K+1,(float)(K+1)/N); printf("\nAre you continuing!\ty?");if(getche()=='y') goto start; }六、思考题(1)补充完成FIFO与OPT的算法。
(2)为什么在实际的系统中不用LRU置换算法,而用它的近似算法,(3)OPT算法为什么难以实现,。