内容发布更新时间 : 2024/11/6 8:04:25星期一 下面是文章的全部内容请认真阅读。
int curpage;
for(int i=0;i<320;i++) {
if(i0==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<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++; - 6 - } } } cout<<\缺页次数:\ cout<<\缺页率:\} //------------------------------------------------------------- void LRU( ) { int exist,space,position ; int curpage; for(int i=0;i<320;i++) { if(i0==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为-1 for(int j=0; j<4; j++) - 7 - {block[j].accessed++;} } cout<<\缺页次数:\ cout<<\缺页率:\} //------------------------------------------------------------- void FIFO( ) { int exist,space,position ; int curpage; for(int i=0;i<320;i++) { if(i0==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<<\请输入第一条指令号(0~320):\ suijishu( ); cout<<\对应的调用页面队列*******\ pagestring( ); do { cout<<\ cout<<\ 2:LRU 3:FIFO 4:退出-----\ cout<<\ cout<<\ 请选择一种页面置换算法:\ cin>>select; cout<<\ init( ); switch(select) { case 1:cout<<\最佳置换算法OPT:\ cout<<\ OPT( ); break; case 2:cout<<\最近最久未使用置换算法LRU:\ cout<<\ LRU( ); break; case 3:cout<<\先进先出置换算法FIFO:\ cout<<\ FIFO( ); break; - 9 - default: ; } }while(select!=4); } 实验结果: - 10 -