NUR面置换算法.docx
- 文档编号:4989420
- 上传时间:2022-12-12
- 格式:DOCX
- 页数:7
- 大小:15.63KB
NUR面置换算法.docx
《NUR面置换算法.docx》由会员分享,可在线阅读,更多相关《NUR面置换算法.docx(7页珍藏版)》请在冰豆网上搜索。
NUR面置换算法
NRU页面置换算法
1)假设每个页面中可存放10条指令,分配给作业的内存块数为4。
2)用C语言模拟一个作业的执行过程,该作业共有320条指令,即它的地址空间为32页,目前它的所有页都还未调入内存。
在模拟过程中,如果所访问的指令已在内存,则显示其物理地址,并转下一条指令。
如果所访问的指令还未装入内存,则发生缺页,此时需记录缺页的次数,并将相应页调入内存。
如果4个内存块均已装入该作业,则需进行页面置换,最后显示其物理地址,并转下一条指令。
在所有320指令执行完毕后,请计算并显示作业运行过程中发生的缺页率。
3)置换算法:
最近最不经常使用(NRU)算法。
#include
#include
#include
#defineN4
#definesize320
typedefstructPage{
intpage_num;
intflag_A;//访问位
intflag_M;//修改位
intread_or_write;//0表示不被修改,1表示被修改
}PAGE;
typedefstructBlock{
intblock_num;//块号
PAGEpage;//所装页的信息
structBlock*next;
}BLOCK,*BLOCKLIST;
typedefstructinstruct{//指令数据结构
intaddress;//指令地址
PAGEpage;//对应页的信息
}INSTRUCTION,*INSTRUCTIONLIST;
INSTRUCTIONinstructions[size];//定义320个指令
BLOCKLISTblock_head;//块的头指针
intdiseffect=0;//缺页次数
intblockSize=0;
voidInit_Instructions()
{
for(inti=0;i<320;i++)
instructions[i].address=rand()%320;
for(intk=0;k<320;k++){
instructions[k].page.page_num=(int)instructions[k].address/10;
instructions[k].page.flag_A=0;
instructions[k].page.read_or_write=rand()%2;
if(instructions[k].page.read_or_write==0)
instructions[k].page.flag_M=0;
elseinstructions[k].page.flag_M=1;
}
}
BLOCKLISTInit_block()
{
BLOCKLISThead=(BLOCKLIST)malloc(sizeof(BLOCK));
BLOCKLISTp;
for(inti=1;i<=N;i++){
if(i==1)p=head;
else{
p->next=(BLOCKLIST)malloc(sizeof(BLOCK));
p=p->next;
}
p->block_num=i;
p->page.page_num=-1;
p->page.flag_A=0;
p->page.flag_M=0;
}
p->next=head;
returnhead;
}
voiddisplay(INSTRUCTIONinstructions)
{
BLOCKLISTp=block_head;
printf("Thenewpage:
(page_num==%d),(flag_M==%d),(address==%d)\n",
printf("block_num,page_num,flag_A,flag_M\n");
do{
printf("%2d%10d%9d%8d\n",p->block_num,p->page.page_num,p->page.flag_A,p->page.flag_M);
p=p->next;
}while(p!
=block_head);
}
voidshow_physical_address(BLOCKLIST&p,INSTRUCTIONinstructions)
{
intaddress;
printf("physicaladdress:
");
address=p->block_num*1024+instructions.address%10;//页面大小为1k
printf("%d\n\n",address);
}
//查找四个块中是否有此页面
intFind_page_in_block(INSTRUCTIONinstructions,BLOCKLIST&p)
{
p=block_head;
do{
p->page.flag_A=1;
if(p->page.flag_M==0)
return1;
}
p=p->next;
}while(p!
=block_head);
return0;
}
//先将四个块中装满
voidloadpage(PAGEpage)
{
BLOCKLISTp;
p=block_head;
while(p->page.page_num!
=-1)
p=p->next;
p->page.page_num=page.page_num;
p->page.flag_A=1;
p->page.flag_M=page.flag_M;
blockSize++;
}
//第一次循环扫描A=0M=0
intcscanf1(BLOCKLIST&p)
{
p=block_head;
do{
if(p->page.flag_A==0&&p->page.flag_M==0)
return1;
p=p->next;
}while(p!
=block_head);
return0;
}
//第二次循环扫描A=0M=1
intcscanf2(BLOCKLIST&p)
{
p=block_head;
do{//把扫面过后的A=1,M=0或A=1,M=1中的A置0
if(p->page.flag_A==1&&(p->page.flag_M==0||p->page.flag_M==1)){
p->page.flag_A=0;
p=p->next;
continue;
}
if(p->page.flag_A==0&&p->page.flag_M==1)
return1;
p=p->next;
}while(p!
=block_head);
return0;
}
//第三次扫描将所有的A置为0
intcscanf3(BLOCKLIST&p)
{
p=block_head;
do{
p->page.flag_A=0;
p=p->next;
}while(p!
=block_head);
return1;
}
//用于换进页面
voidassignment(BLOCKLIST&p,INSTRUCTIONinstructions)
{
p->page.flag_A=1;
}
//NRU页面替换算法
voidreplace_page(INSTRUCTIONinstructions,BLOCKLIST&p)
{
if(cscanf1(p)){
assignment(p,instructions);
}
elseif(cscanf2(p)){
assignment(p,instructions);
}
elseif(cscanf3(p)){
if(cscanf1(p))
assignment(p,instructions);
else{
cscanf2(p);
assignment(p,instructions);
}
}
}
voidmain()
{
BLOCKLISTp;
Init_Instructions();
block_head=Init_block();
for(inti=0;i if(Find_page_in_block(instructions[i],p)){ display(instructions[i]); show_physical_address(p,instructions[i]); getchar(); continue; } elseif(blockSize diseffect++; loadpage(instructions[i].page); } else{ diseffect++; replace_page(instructions[i],p); } display(instructions[i]); if(diseffect>4) show_physical_address(p,instructions[i]); getchar(); } printf("NRU%f\n",(float)diseffect/size); getchar();getchar();getchar(); }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- NUR 置换 算法
![提示](https://static.bdocx.com/images/bang_tan.gif)