Created
May 13, 2018 14:14
-
-
Save fpGHwd/d87283b1309cda5479092e55cc7d5e46 to your computer and use it in GitHub Desktop.
LRU
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define MAXSIZE 20 | |
#include <iostream.h> | |
void main() | |
{ | |
int input=0; //用于输入作业号 | |
int worknum=0; //输入的作业个数 | |
int storesize=0; //系统分配的存储区块数 | |
int interrupt=0; //缺页中断次数 | |
int stack[MAXSIZE]; //栈,LRU算法的主要数据结构 | |
int workstep[MAXSIZE]; //记录作业走向 | |
/*初始化*/ | |
for(int i=0;i<MAXSIZE;i++) | |
{ | |
stack[i]=0; | |
workstep[i]=0; | |
} | |
cout<<"请输入存储区块数:"; | |
cin>>storesize; | |
cout<<"请输入作业的页面走向(输入0结束):\n"; | |
for(int j=0;j<MAXSIZE;j++) | |
{ | |
cout<<"页面号 "<<j+1<<” :”; | |
cin>>input; | |
workstep[j]=input; | |
if(input==0) | |
{ | |
cout<<"输入结束!\n"; | |
break; | |
} | |
worknum++; | |
} | |
if(workstep[0]==0) | |
{ | |
cout<<"未输入任何作业,系统将退出!\n"; | |
return; | |
} | |
cout<<"置换情况如下:\n"; | |
for(int k=0;k<worknum;k++) | |
{ | |
/*在栈中找相等的页号或空位置*/ | |
for(int l=0;l<storesize;l++) | |
{ | |
/*是否有相等的页号*/ | |
if(stack[l]==workstep[k]) | |
{ | |
cout<<"内存中有"<<workstep[k]<<"号页面,无须中断!\n"; | |
goto step1; | |
} | |
/*找栈中是否有空位置*/ | |
if(stack[l]==0) | |
{ | |
stack[l]=workstep[k]; | |
cout<<"发生中断,但内存中有空闲区,"<<workstep[k]<<"号页面直接调入!\n"; | |
interrupt++; | |
goto step1; | |
} | |
} | |
/*上述情况都不成立则调出栈顶,将调入页面插入栈顶*/ | |
cout<<"发生中断,将"<<stack[0]<<"号页面调出,"<<workstep[k]<<"号装入!\n"; | |
interrupt++; | |
/*新掉入的页面放栈顶*/ | |
step1: for(int m=0;m<storesize;m++) | |
{ | |
stack[m]=stack[m+1]; | |
} | |
stack[storesize-1]=workstep[k]; | |
} | |
cout<<"作业"<<worknum<<"个,"<<"中断"<<interrupt<<"次,"<<"缺页率:"<<float(interrupt)/float(worknum)*100<<"%\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LRU { | |
public static void main(String[] args){ | |
int worknum = 10; | |
int work[] = {1,2,3,4,5,7,7,7,7,7}; | |
int memory[] = new int[3]; | |
int intterp = 0; | |
boolean flag; | |
for(int i=0;i<worknum;i++){ | |
flag = true; | |
for(int j=0;j<memory.length;j++){ | |
if(memory[j]==work[i]){ | |
System.out.println("内存中有"+work[i]+"无需中断"); | |
swapmemroy(memory,work[i]); | |
flag = false; | |
} | |
else if(memory[j]==0){ | |
System.out.println("发生中断,但内存中有空闲区,"+work[i]+"直接调入"); | |
intterp++; | |
swapmemroy(memory,work[i]); | |
flag = false; | |
} | |
} | |
if(flag){ | |
System.out.println("发生中断,,"+memory[0]+"调出"+work[i]+"调入"); | |
swapmemroy(memory, work[i]); | |
intterp++; | |
} | |
} | |
System.out.println("中断次数"+intterp+"缺页率"+(double)intterp/(double)worknum*100); | |
} | |
static public void swapmemroy(int[] memory,int worknum){ | |
for(int i=0;i<memory.length-1;i++){ | |
memory[i] = memory[i+1]; | |
} | |
memory[memory.length-1] = worknum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment