南京邮电大学实验报告模版 2.docx
- 文档编号:12896093
- 上传时间:2023-06-09
- 格式:DOCX
- 页数:19
- 大小:402.34KB
南京邮电大学实验报告模版 2.docx
《南京邮电大学实验报告模版 2.docx》由会员分享,可在线阅读,更多相关《南京邮电大学实验报告模版 2.docx(19页珍藏版)》请在冰点文库上搜索。
南京邮电大学实验报告模版2
实验报告
〔2014/2015学年第一学期〕
课程名称
电脑操作系统
实验名称
虚拟存储中页面置换算法的模拟实现
实验时间
2014
年
12
月
19
日
指导单位
南京邮电大学
指导教师
崔衍
学生
班级学号
学院(系)
物联网院
专业
网络工程
实验报告
实验名称
虚拟存储中页面置换算法的模拟实现
指导教师
崔衍
实验类型
上机实验
实验学时
4
实验时间
2014.12.19
一、实验目的和要求
目的:
通过请求页式存储管理中页面置换算法模拟设计,
了解虚拟存储技术的技术特点,
掌握请求页式存储管理的页面置换算法
要求:
(1)设计实现下面算法,并输出下述各种算法的命中率。
①先进先出的算法〔FIFO〕;
②最近最少使用算法〔LRR);
③最正确淘汰算法〔OPT〕先淘汰最不常用的页地址。
(2)多次反复运行改良后的程序,观察并记录运行结果,并分析原因。
二、实验环境(实验设备)
Vc++6.0
二、实验原理及内容
实验三
#include
#include
#include
#include
#defineBsize4
typedefstructBLOCK//声明一种新类型——物理块类型
{
intpagenum;//页号
intaccessed;//访问字段,其值表示多久未被访问
}BLOCK;
intpc;//程序计数器,用来记录指令的序号
intn;//缺页计数器,用来记录缺页的次数
staticinttemp[320];//用来存储320条随机数
BLOCKblock[Bsize];//定义一大小为4的物理块数组
//*************************************************************
voidinit();//程序初始化函数
intfindExist(intcurpage);//查找物理块中是否有该页面
intfindSpace();//查找是否有空闲物理块
intfindReplace();//查找应予置换的页面
voiddisplay();//显示
voidsuijishu();//产生320条随机数,显示并存储到temp[320]
voidpagestring();//显示调用的页面队列
voidOPT();//OPT算法
voidLRU();//LRU算法
voidFIFO();//FIFO算法
//*************************************************************
voidinit()
{
for(inti=0;i { block[i].pagenum=-1; block[i].accessed=0; pc=n=0; } } //------------------------------------------------------------- intfindExist(intcurpage) { for(inti=0;i { if(block[i].pagenum==curpage) returni;//检测到内存中有该页面,返回block中的位置 } return-1; } //------------------------------------------------------------- intfindSpace() { for(inti=0;i { if(block[i].pagenum==-1) returni;//找到空闲的block,返回block中的位置 } return-1; } //------------------------------------------------------------- intfindReplace() { intpos=0; for(inti=0;i { if(block[i].accessed>block[pos].accessed) pos=i;//找到应予置换页面,返回BLOCK中位置 } returnpos; } //------------------------------------------------------------- voiddisplay() { for(inti=0;i { if(block[i].pagenum! =-1) {printf("%02d",block[i].pagenum);} } cout< } //------------------------------------------------------------- voidsuijishu() {intflag=0; cin>>pc; cout<<"******按照要求产生的320个随机数: *******"< for(inti=0;i<320;i++) { temp[i]=pc; if(flag%2==0)pc=++pc%320; if(flag==1)pc=rand()%(pc-1); if(flag==3)pc=pc+1+(rand()%(320-(pc+1))); flag=++flag%4; printf("%03d",temp[i]); if((i+1)%10==0)cout< } } //------------------------------------------------------------- voidpagestring() { for(inti=0;i<320;i++) { printf("%02d",temp[i]/10); if((i+1)%10==0)cout< } } //------------------------------------------------------------- voidOPT() { intexist,space,position; intcurpage; for(inti=0;i<320;i++) { if(i%100==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(intk=0;k { for(intj=i;j<320;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++; } } } cout<<"缺页次数: "< cout<<"缺页率: "<<(n/320.0)*100<<"%"< } //------------------------------------------------------------- voidLRU() { intexist,space,position; intcurpage; for(inti=0;i<320;i++) { if(i%100==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++; } } elseblock[exist].accessed=-1;//恢复存在的并刚访问过的BLOCK中页面accessed为-1 for(intj=0;j<4;j++) {block[j].accessed++;} } cout<<"缺页次数: "< cout<<"缺页率: "<<(n/320.0)*100<<"%"< } //------------------------------------------------------------- voidFIFO() { intexist,space,position; intcurpage; for(inti=0;i<320;i++) { if(i%100==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(intj=0;j block[j].accessed++; } cout<<"缺页次数: "< cout<<"缺页率: "<<(n/320.0)*100<<"%"< } //************************************************************* voidmain() { intselect; cout<<"请输入第一条指令号(0~320): "; suijishu(); cout<<"*****对应的调用页面队列*******"< pagestring(); do { cout<<"****************************************"< cout<<"------1: OPT2: LRU3: FIFO4: 退出-----"< cout<<"****************************************"< cout<<"请选择一种页面置换算法: "; cin>>select; cout<<"****************************************"< init(); switch(select) { case1: cout<<"最正确置换算法OPT: "< cout<<"*****************"< OPT(); break; case2: cout<<"最近最久未使用置换算法LRU: "< cout<<"**************************"< LRU(); break; case3: cout<<"先进先出置换算法FIFO: "< cout<<"*********************"< FIFO(); break; default: ; } }while(select! =4); } 实验结果: 实验四 Login用户登录 boolchklogin(char*users,char*pwd) { inti; for(i=0;i<8;i++) { if((strcmp(users,usrarray[i].name)==0)&&(strcmp(pwd,usrarray[i].pwd)==0)) returntrue; } returnfalse; } Create创建文件 intcreate() { temp=initfile("",0); cin>>temp->filename; cin>>temp->content; if(recent->child==NULL) { temp->parent=recent; temp->child=NULL; recent->child=temp; temp->prev=temp->next=NULL; cout<<"文件建立成功! "< } else { ttemp=recent->child; while(ttemp->next) { ttemp=ttemp->next; if(strcmp(ttemp->filename,temp->filename)==0&&ttemp->isdir==0) { printf("对不起,文件已存在! "); return1; } } ttemp->next=temp; temp->parent=NULL; temp->child=NULL; temp->prev=ttemp; temp->next=NULL; cout<<"文件建立成功! "< } return1; } intdir() { inti=0,j=0; temp=newfnode; temp=recent; if(temp! =root) {cout<<"\ if(temp->child==NULL) { cout<<"Total: "<<"directors"< return1; } temp=temp->child; while(temp) { if(temp->isdir) {cout<<" else {cout<<" temp=temp->next; } cout<<"Total: "<<"directors"< } Read读取文件 intread() { charfilename[FILENAME_LENGTH]; cin>>filename; if(recent->child==NULL) { cout<<"文件不存在! "< return1; } if(strcmp(recent->child->filename,filename)==0) { cout< return1; } else { temp=recent->child; while(temp->next) { if(strcmp(temp->next->filename,filename)==0) {cout< return1;} } cout<<"文件不存在! "< } } Write写入文件 intwrite() { charfilename[FILENAME_LENGTH]; cin>>filename; if(recent->child==NULL) { cout<<"文件不存在! "< return1; } if(strcmp(recent->child->filename,filename)==0) { recent->child->isopen=1;//设置文件标记为打开 cin>>recent->child->content; recent->child->isopen=0;//设置文件标记为关闭 cout<<"文件写入成功! "< return1; } else { temp=recent->child; while(temp->next) { if(strcmp(temp->next->filename,filename)==0) { recent->child->isopen=1;//设置文件标记为打开 cin>>temp->next->content; recent->child->isopen=0;//设置文件标记为关闭 cout<<"文件写入成功! "< return1;} } cout<<"文件不存在! "< } } Del删除 intdel() { charfilename[FILENAME_LENGTH]; cin>>filename; temp=newfnode; 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) { cout<<"不存在该文件! "< return0; } } else { cout<<"不存在该文件! "< return0; } if(temp->parent==NULL) { temp->prev->next=temp->next; if(temp->next) temp->next->prev=temp->prev; temp->prev=temp->next=NULL; } else { if(temp->next) temp->next->parent=temp->parent; temp->parent->child=temp->next; } deletetemp; cout<<"文件已删除! "< } 建立目录 intmkdir() { temp=initfile("",1); cin>>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->filename)==0&&ttemp->isdir==1) { printf("对不起,目录已存在! "); return1; } } ttemp->next=temp; temp->parent=NULL; temp->child=NULL; temp->prev=ttemp; temp->next=NULL; } return1; } 切换目录 intdir() { inti=0,j=0; temp=newfnode; temp=recent; if(temp! =root) {cout<<"\ if(temp->child==NULL) { cout<<"Total: "<<"directors"< return1; } temp=temp->child; while(temp) { if(temp->isdir) {cout<<" else {cout<<" temp=temp->next; } cout<<"Total: "<<"directors"< } 四、实验小结〔包括问题和解决方法、心得体会、意见与建议等〕 通过课程设计,加深了对操作系统的认识,了解了操作系统中各种资源分配算法的实现,特别是对虚拟存储,页面置换有了深入的了解,并能够用高级语言进行模拟演示。 在这短短的两周时间里,通过浏览、阅读有关的资料,学到了很多东西,同时也发现仅仅书本的知识是远远不够的,需要把知识运用到实践中去,能力才能得到提高。 两种页面置换算法FIFO和LRU理解起来相当容易,但在实际编程实现的时候需要注意各种细节,需要耐心细致,实际编程中遇到一些细节上的小问题确实需要仔细考虑才行。 思考题: 1.如果增加分配给作业的内存块数,则将会对作业运行过程中的缺页率产生什么影响? 答: LUR算法将降低缺页率;OPT算法将降低缺页率;FIFO算法将提高缺页率2.为什么一般情况下,LRU具有比FIFO更好的性能? 答: FIFO置换算法设
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 南京邮电大学实验报告模版 南京 邮电大学 实验 报告 模版