页面替换算法模拟

上传人:xiao****1972 文档编号:84972694 上传时间:2019-03-06 格式:DOCX 页数:8 大小:98.30KB
返回 下载 相关 举报
页面替换算法模拟_第1页
第1页 / 共8页
页面替换算法模拟_第2页
第2页 / 共8页
页面替换算法模拟_第3页
第3页 / 共8页
页面替换算法模拟_第4页
第4页 / 共8页
页面替换算法模拟_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《页面替换算法模拟》由会员分享,可在线阅读,更多相关《页面替换算法模拟(8页珍藏版)》请在金锄头文库上搜索。

1、班级:10010908学号:2009302616姓名:任昭#include #include #include #include using namespace std;#define MAX_PRED_PATH 16#define MAX_BLOCKS 128int mem_size;int text_size;int page_size;int mem_pages;int memMAX_BLOCKS;int lastMAX_BLOCKS;int loaded;vector path;void InitAll()loaded=0;memset(mem,-1,MAX_BLOCKS*sizeof

2、(int);memset(last,0,MAX_BLOCKS*sizeof(int);void GetInput()printf(Please Input The Mem,Page And Text Size:n);scanf(%d,&mem_size);scanf(%d,&page_size);scanf(%d,&text_size);mem_pages=mem_size/page_size;printf(Mem Can Content %d Pages.nPlease Input The Path:n,mem_pages);for (;)int p=0;scanf(%d,&p);if (p

3、=0)break;path.push_back(p-1);int IsMatch(int p)for (int i=0;imem_pages;i+)if (p = memi)return i+1;return 0;int PreDict(int p)int predMAX_BLOCKS;memset(pred,p+MAX_PRED_PATH,MAX_BLOCKS*sizeof(int);int n=0;int mat=0;for(int i=p;ipath.size() & i(p+MAX_PRED_PATH);i+)if (mat = IsMatch(path.at(i)/page_size

4、)predmat-1=i;n=0;for (int i=0;ipredn)n=i;return n;void RunFIFO()int corrent=0;int matches=0;int matchen=0;printf(Start FIFO:n);for (int i=0;ipath.size();i+)printf(Visitt%dt:,path.at(i);if (matchen = IsMatch(path.at(i)/page_size)matches+;printf(Matchtpaget%dtattmemt%d,path.at(i)/page_size+1,matchen);

5、 else if(loaded mem_pages)printf(Loadtpaget%dtattmemt%d,path.at(i)/page_size+1,loaded+1);memloaded+ = path.at(i)/page_size;elseprintf(Replatpaget%dttotmemt%d,path.at(i)/page_size+1,corrent+1);memcorrent=path.at(i)/page_size;corrent=(1+corrent)%mem_pages;printf(tMem Map:);for (int j=0;jmem_pages;j+)p

6、rintf(%d,memj+1);printf(n);printf(Match:%d Rate:%.2fn,matches,(double)matches)/path.size();void RunOPT()int corrent=0;int matches=0;int matchen=0;printf(Start OPT:n);for (int i=0;ipath.size();i+)printf(Visitt%dt:,path.at(i);if (matchen = IsMatch(path.at(i)/page_size)matches+;printf(Matchtpaget%dtatt

7、memt%d,path.at(i)/page_size+1,matchen); else if(loaded mem_pages)printf(Loadtpaget%dtattmemt%d,path.at(i)/page_size+1,loaded+1);memloaded+ = path.at(i)/page_size;elsecorrent=PreDict(i);printf(Replatpaget%dttotmemt%d,path.at(i)/page_size+1,corrent+1);memcorrent=path.at(i)/page_size;printf(tMem Map:);

8、for (int j=0;jmem_pages;j+)printf(%d,memj+1);printf(n);printf(Match:%d Rate:%.2fn,matches,(double)matches)/path.size();int main()InitAll();GetInput();RunFIFO();InitAll();RunOPT();Please Input The Mem,Page And Text Size:400 200 920Mem Can Content 2 Pages.Please Input The Path:20 22 208 214 146 618 37

9、0 490 492 868 916 728 0Start FIFO:Visit 19 :Load page 1 at mem 1 Mem Map:10Visit 21 :Match page 1 at mem 1 Mem Map:10Visit 207 :Load page 2 at mem 2 Mem Map:12Visit 213 :Match page 2 at mem 2 Mem Map:12Visit 145 :Match page 1 at mem 1 Mem Map:12Visit 617 :Repla page 4 to mem 1 Mem Map:42Visit 369 :M

10、atch page 2 at mem 2 Mem Map:42Visit 489 :Repla page 3 to mem 2 Mem Map:43Visit 491 :Match page 3 at mem 2 Mem Map:43Visit 867 :Repla page 5 to mem 1 Mem Map:53Visit 915 :Match page 5 at mem 1 Mem Map:53Visit 727 :Repla page 4 to mem 2 Mem Map:54Match:6 Rate:0.50Start OPT:Visit 19 :Load page 1 at me

11、m 1 Mem Map:10Visit 21 :Match page 1 at mem 1 Mem Map:10Visit 207 :Load page 2 at mem 2 Mem Map:12Visit 213 :Match page 2 at mem 2 Mem Map:12Visit 145 :Match page 1 at mem 1 Mem Map:12Visit 617 :Repla page 4 to mem 1 Mem Map:42Visit 369 :Match page 2 at mem 2 Mem Map:42Visit 489 :Repla page 3 to mem 1 Mem Map:32Visit 491 :Match page 3 at mem 1 Mem Map:32Visit 867 :Repla page 5 to mem 1 Mem Map:52Visit 915 :Match page 5 at mem 1 Me

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号