当前位置:文档之家› 南京邮电大学实验报告模版

南京邮电大学实验报告模版

南京邮电大学实验报告模版实验报告课程名称实验名称计算机操作系统虚拟存储中页面置换算法的模拟实现2014 实验时间指导单位指导教师年12 月19 日南京邮电大学崔衍学生姓名学院(系) 物联网院班级学号专业网络工程实验报告实验名称虚拟存储中页面置换算法的模拟实现实验类型上机实验实验学时一、实验目的和要求目的:通过请求页式存储管理中页面置换算法模拟设计,了解虚拟存储技术的技术特点,掌握请求页式存储管理的页面置换算法要求:(1)设计实现下面算法,并输出下述各种算法的命中率。

①先进先出的算法;②最近最少使用算法先淘汰最不常用的页地址。

(2)多次反复运行改进后的程序,观察并记录运行结果,并分析原因。

指导教师崔衍 4 实验时间二、实验环境(实验设备) Vc++ - 2 - 二、实验原理及内容实验三#include #include #include #include #define Bsize 4 typedef struct BLOCK//声明一种新类型——物理块类型{ int pagenum;//页号int accessed;//访问字段,其值表示多久未被访问}BLOCK; int pc;//程序计数器,用来记录指令的序号int n;//缺页计数器,用来记录缺页的次数static int temp[320];//用来存储320条随机数BLOCK block[Bsize]; //定义一大小为4的物理块数组//********************************* **************************** void init( );//程序初始化函数int findExist(int curpage);//查找物理块中是否有该页面int findSpace( );//查找是否有空闲物理块int findReplace( );//查找应予置换的页面void display ( );//显示void suijishu( );//产生320条随机数,显示并存储到temp[320] void pagestring( );//显示调用的页面队列void OPT( );//OPT 算法void LRU( );// LRU算法void FIFO( );//FIFO算法//********************************* **************************** void init( ) { for(int i=0;i block[i].pagenum=-1;block[i].accessed=0; - 3 - pc=n=0;} } //------------------------------------------------------------- int findExist(int curpage) { for(int i=0; i if(block[i].pagenum == curpage ) return i;//检测到内存中有该页面,返回block中的位置} return -1; } //------------------------------------------------------------- int findSpace( ) {for(int i=0; i if(block[i].pagenum == -1) return i;//找到空闲的block,返回block中的位置}return -1; }//------------------------------------------------------------- int findReplace( ) { int pos = 0; for(int i=0; i if(block[i].accessed >block[pos].accessed) pos = i;//找到应予置换页面,返回BLOCK中位置} return pos; } //------------------------------------------------------------- void display( ) - 4 - { for(int i=0; i if(block[i].pagenum != -1) { printf(\ } cout //------------------------------------------------------------- void suijishu( ) { int flag=0; cin>>pc; cout if(flag%2==0) pc=++pc20;if(flag==1) pc=rand( )% (pc-1); if(flag==3) pc=pc+1+(rand( )%(320-(pc+1))); flag=++flag%4; printf(\ if((i+1) ==0) cout //------------------------------------------------------------- void pagestring( ) {for(int i=0;i printf(\if((i+1) ==0) cout //------------------------------------------------------------- void OPT( ) { int exist,space,position ; - 5 -int curpage; for(int i=0;i if(i 0==0) getch( ); pc=temp[i];curpage=pc/10; exist = findExist(curpage); if(exist==-1) { space = findSpace ( );if(space != -1) { block[space].pagenum = curpage; display( );n=n+1;} else { for(int k=0;k for(int j=i;j if(block[k].pagenum!= temp[j]/10) { block[k].accessed = 1000;}//将来不会用,设置为一个很大数else{ block[k].accessed = j; break; }}} position = findReplace( ); block[position].pagenum = curpage; display( );n++; - 6- } } } cout cout //------------------------------------------------------------- void LRU( ) { int exist,space,position ; int curpage; for(int i=0;i if(i 0==0) getch( ); pc=temp[i];curpage=pc/10; exist = findExist(curpage); if(exist==-1) { space = findSpace( );if(space != -1) { block[space].pagenum = curpage; display( );n=n+1;} else { position = findReplace( ); block[position].pagenum = curpage; display( );n++;} } else block[exist].accessed = -1;//恢复存在的并刚访问过的BLOCK中页面accessed 为-1for(int j=0; j- 7 - {block[j].accessed++;} } cout cout//------------------------------------------------------------- void FIFO( ) { int exist,space,position ; int curpage;for(int i=0;i if(i 0==0) getch( ); pc=temp[i];curpage=pc/10; exist = findExist(curpage); if(exist==-1){ space = findSpace( );if(space != -1) { block[space].pagenum = curpage; display( );n=n+1;} else { position = findReplace( ); block[position].pagenum = curpage; display( );n++; block[position].accessed--;} } for(int j=0; j- 8 - block[j].accessed++; } cout cout//********************************* **************************** void main( ) { int select; cout cout cout>select; cout switch(select){ case 1:cout case 2:cout case 3:cout- 9 - default: ;}}while(select!=4); } 实验结果:- 10 -实验四Login用户登录bool chklogin(char *users, char *pwd) { int i; for(i=0; i>temp->filename; cin>>temp->content;if(recent->child==NULL){ temp->parent=recent; temp->child=NULL; recent->child=temp; temp->prev=temp->next=NULL; coutchild; while(ttemp->next) { - 11 - ttemp=ttemp->next;if(strcmp(ttemp->filename,temp->filenam e)==0&&ttemp->isdir==0) { printf(\对不起,文件已存在!\return 1; } } ttemp->next=temp; temp->parent=NULL; temp->child=NULL; temp->prev=ttemp; temp->next=NULL; coutchild==NULL) { coutchild; while(temp) { if(temp->isdir) {coutnext; } cout cin>>filename; if(recent->child==NULL){ coutchild->filename,filename)==0) { coutchild->contentchild;while(temp->next){ if(strcmp(temp->next->filename,filenam e)==0) {coutnext->content>filename; if(recent->child==NULL){ coutchild->filename,filename)==0) { recent->child->isopen=1;//设置文件标记为打开cin>>recent->child->content; recent->child->isopen=0;//设置文件标记为关闭cout { temp=recent->child; while(temp->next) { if(strcmp(temp->next->filename,filenam e)==0) { recent->child->isopen=1;//设置文件标记为打开cin>>temp->next->content;recent->child->isopen=0;//设置文件标记为关闭cout>filename; temp=new fnode; if(recent->child) { temp=recent->child; while(temp->next && (strcmp(temp->filename,filename)!=0 || temp->isdir!=0)) temp=temp->next; if(strcmp(temp->filename,filename)!=0) { coutparent==NULL){ temp->prev->next=temp->next; if(temp->next)temp->next->prev=temp->prev;temp->prev=temp->next=NULL; - 14 - } else { if(temp->next) temp->next->parent=temp->parent;temp->parent->child=temp->next; } delete temp; cout>temp->filename; if(recent->child==NULL){ temp->parent=recent; temp->child=NULL; recent->child=temp; temp->prev=temp->next=NULL; } else { ttemp=recent->child; while(ttemp->next) { ttemp=ttemp->next; if(strcmp(ttemp->filename,temp->filenam e)==0&&ttemp->isdir==1) { printf(\对不起,目录已存在!\return 1; } } ttemp->next=temp; temp->parent=NULL; temp->child=NULL; temp->prev=ttemp; temp->next=NULL; } return 1; - 15 -} 切换目录int dir(){ int i=0,j=0; temp=new fnode; temp=recent; if(temp!=root) {coutchild==NULL) { coutchild; while(temp) { if(temp->isdir) {coutnext; } cout- 17 - 四、实验小结通过课程设计,加深了对操作系统的认识,了解了操作系统中各种资源分配算法的实现,特别是对虚拟存储,页面置换有了深入的了解,并能够用高级语言进行模拟演示。

相关主题