《实验操作系统存储管理实验报告.pdf》由会员分享,可在线阅读,更多相关《实验操作系统存储管理实验报告.pdf(16页珍藏版)》请在三一文库上搜索。
1、个人资料整理仅限学习使用 实验四操作系统存储管理实验报告 一、实验目的 存储管理的主要功能之一是合理地分配空间。请求页式管理是一种常用的虚拟存储管 理技术。 本实验的目的是通过请求页式管理中页面置换算法模拟设计,了解虚拟存储技术的特 点,掌握请求页式存储管理的页面置换算法。 二、实验内容 (1)通过计算不同算法的命中率比较算法的优劣。同时也考虑了用户内存容量 对命中率的影响。 页面失效次数为每次访问相应指令时,该指令所对应的页不在内存中的次数 。 在本实验中,假定页面大小为1k,用户虚存容量为32k,用户内存容量为4页 到32页。 (2)produce_addstream通过随机数产生一个指令
2、序列,共320条指令。 A、指令的地址按下述原则生成: 1) 50%的指令是顺序执行的 2)25%的指令是均匀分布在前地址部分 3) 25%的指令是均匀分布在后地址部分 B、具体的实施方法是: 1)在0,319的指令地址之间随机选取一起点m; 2)顺序执行一条指令,即执行地址为m+1的指令; 3)在前地址 0,m+1中随机选取一条指令并执行,该指令的地址为m ; 4)顺序执行一条指令,地址为m +1的指令 5)在后地址 m +2,319中随机选取一条指令并执行; 6)重复上述步骤 1)5),直到执行320次指令 C、将指令序列变换称为页地址流 个人资料整理仅限学习使用 在用户虚存中,按每k存放
3、 10条指令排列虚存地址,即320条指令在虚存中 的存放方式为: 第0条第9条指令为第 0页 FIFO(LRU(LFU( Msize加1 S=? 是否用其他算 法继续 结 束 N Y 1 2 3 4 Y N 提示出错 ,重新输 个人资料整理仅限学习使用 选择 FIFO调度算法,并且内存从3也开始逐渐增加到32页,打印出缺页次数缺页率,命中 率 个人资料整理仅限学习使用 选择 LRU 调度算法,并且内存从3也开始逐渐增加到32页,打印出缺页次数缺页率,命中率 个人资料整理仅限学习使用 选择 OPT调度算法,并且内存从3也开始逐渐增加到32页,打印出缺页次数缺页率,命中率 个人资料整理仅限学习使用
4、 六 实验程序 产生指令流文件produce_addstream.h #ifndef PRODUCE_ADDSTREAM_H #define PRODUCE_ADDSTREAM_H #include #include #include #include 个人资料整理仅限学习使用 #include using namespace std。 #define random(x (rand(%x #define MAX_LENGTH 320 struct produce int num。 /指令序号 int zhiling 。 /指令地址 int virtualpage 。 /指令虚页号 produc
5、e *next 。 。 struct produce*creatlist( 。 void insert(struct produce *first,struct produce *s。 /插入一个节点。 /打印函数 int max(vector ,int 。 struct produce*creatlist( srand(inttime(0 。 struct produce*first=new produce 。 first-next=NULL。 int m=0,m1=0 。 /* int yanzheng20=7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1。
6、for (int i=0 。i。i+ struct produce *s0 。 s0=new produce。 s0-num=i*4+0 。 s0-zhiling=yanzhengi*4+0。 s0-virtualpage=s0-zhiling 。 insert(first,s0 。 struct produce *s1 。 s1=new produce。 s1-num=i*4+1 。 s1-zhiling=yanzhengi*4+1。 s1-virtualpage=s1-zhiling 。 insert(first,s1 。 struct produce *s2 。 s2=new produ
7、ce。 s2-num=i*4+2 。 s2-zhiling=yanzhengi*4+2。 s2-virtualpage=s2-zhiling 。 insert(first,s2 。 struct produce *s3 。 个人资料整理仅限学习使用 s3=new produce。 s3-num=i*4+3 。 s3-zhiling=yanzhengi*4+3。 s3-virtualpage=s3-zhiling 。 insert(first,s3 。 /*/ /* for (int i=0 。i。i+ struct produce *s0 。 s0=new produce。 m=random(
8、MAX_LENGTH。 s0-num=i*4+0 。 s0-zhiling=m+1 。 s0-virtualpage=s0-zhiling/10 。 insert(first,s0 。 m1=random(m+1 。 struct produce *s1 。 s1=new produce。 s1-num=i*4+1 。 s1-zhiling=m1 。 s1-virtualpage=s1-zhiling/10 。 insert(first,s1 。 struct produce *s2 。 s2=new produce。 s2-num=i*4+2 。 s2-zhiling=m1+1 。 s2-v
9、irtualpage=s2-zhiling/10 。 insert(first,s2 。 struct produce *s3 。 s3=new produce。 s3-num=i*4+3 。 s3-zhiling=random(MAX_LENGTH-m1-2+m1+2。 s3-virtualpage=s3-zhiling/10 。 insert(first,s3 。 /*/ return first 。 void insert(struct produce *first,struct produce *s struct produce *r=first 。 struct produce *p
10、 。 while(rp=r 。r=r-next 。 个人资料整理仅限学习使用 p-next=s。p=s。 p-next=NULL 。 void print(struct produce *first /打印函数 struct produce *p 。 p =first-next 。 cout coutnumzhilingvirtualpagenext 。 int max(vector page,int Maxpage int a=0,position=0 。 for (int i=0 。i if (pagei1a a=pagei1 。 position=i 。 return position
11、。 #endif 先来先出调度算法: fifo.h #ifndef FIFO_H #define FIFO_H void fifo(struct produce *first,int Maxpage vector page(Maxpage 。/ for (int i=0 。ipagei=-1。 int rear=0。/定义一个变量,指向要被替换的位置 int pages 。/定义变量保存当前指令的所在的地址 int count1=0。/ int count2=0。/缺页次数 int find=1。 struct produce *p=first-next。 while (p 个人资料整理仅限学
12、习使用 pages=p-virtualpage 。 for(int i=0 。i if (pagei=-1|count1 pagei=pages 。 count1 +。 count2 +。 find =1。 break。 else if (pagei=pages find =1。 break。 find=0。 if (find=0 pagerear=pages 。 rear +。 rear=rear%Maxpage 。 count2 +。 p=p-next。 cout/MAX_LENGTH/MAX_LENGTH using namespace std 。 /int max(vector ,in
13、t 。 void lru(struct produce*first,int Maxpage struct produce*p=first-next。 vector page2(Maxpage, vector(2。 int count1=0。 /定义内存已经被占用的页数 个人资料整理仅限学习使用 int count2=0。 /定义记录缺页次数 int equal=0。 /定义判断如果当前页数与比较的页数,如果相等则为1,否则为 0 int place=0。 /定义要替换的位置 for (int i=0 。i page2i0=-1。page2i1=0。 while (p if (count1 fo
14、r (int i=0 。i page2i1=page2i1+1 。 if (page2i0=-1 page2i0=p-virtualpage。 count2+。 break。 else if (page2i0=p-virtualpage page2i1 =1。 count1+。 else for (int i=0 。i page2i1 +=1。 if (page2i0=p-virtualpage equal=1。place=i。break。 if (equal=1 page2place1 =1。 equal=0。 else place = max(page2,Maxpage 。 个人资料整理仅
15、限学习使用 page2place1=1。 page2place0=p-virtualpage。 count2+。 p=p-next。 cout/MAX_LENGTH/MAX_LENGTH using namespace std 。 int search(struct produce*place,int position 。 void opt(struct produce*first,int Maxpage struct produce*p =first-next。 vector page3(Maxpage, vector(2。 int count1=0。 /定义内存已被使用的页数 int co
16、unt2=0。 /定义缺页次数 int current=0。 /定义当前工作位置 int equal=0。 /定义判断如果当前页数与比较的页数,如果相等则为1,否则为 0 int place=0。 /定义要替换的位置 for (int i=0 。i page3i0=-1。page3i1=0。 while (p /cout for (int i=0 。i if (page3i0=-1 page3i0=p-virtualpage。 page3i1=search(p,current。 个人资料整理仅限学习使用 count2+。 break。 else if (page3i0=p-virtualpag
17、e page3i1=search(p,current。 count1+。 else for (int i=0 。i if (page3i0=p-virtualpage equal=1。place=i。break。 if (equal=1 page3place1 =search(p,current 。 equal=0。 else place = max(page3,Maxpage 。 page3place1=search(p,current 。 page3place0=p-virtualpage。 count2 +=1。 p=p-next。 current +=1。 cout/MAX_LENGT
18、H/MAX_LENGTH struct produce*p=place-next 。 int current=place-virtualpage。 int position1=position+1。 while(p 个人资料整理仅限学习使用 if (current=p-virtualpage return position1。 position1+。 p=p-next。 return position1。 #endif 主函数 控制台ccglmain.cpp #include #include “produce_addstream.h“ #include “fifo.h“ #include “
19、lru.h“ #include “opt.h“ void main( int S。 / 定义变量记录用户选择 char again 。 /定义变量用户选择继续还是退出 cout。/产生随机指令 cout 。/打印产生的指令信息 while (1 int Maxpage=3。/定义内存最大页面数 cout调度算法 n“n“n“S。 while(S4|S coutS。 if (S!=4 while(Maxpage switch(S 个人资料整理仅限学习使用 case 1:fifo(first,Maxpage。break。 case 2:lru(first,Maxpage。break。 case 3:opt(first,Maxpage 。break。 default:break。 Maxpage+。 coutagain。 if(again=y|again=Y continue。 else break 。 else system(“cls“。
链接地址:https://www.31doc.com/p-4646627.html