代码
第一步:创建MemoryManage_Operation类
public class MemoryManage_Operation {
private String[][] dataStrings = new String[33][4];//数据传递数组
private int[] Addstream = new int[320];//地址流
private int[] Addspage = new int[320];//页面流
private int[] phyBlock = new int[32];//物理块数
private Random random = new Random();//随机数
private int blockNum;//内存块数
private int npageNum;//缺页数临时变量
private float rate;//缺页率
private int tempK, tempG, tempF;//临时变量// 产生随机地址流和页面流
public void setProduceAddstream(){
int temp;
for (int i = 0; i < 320; i+=3 )
{ temp=random.nextInt(320) % 320 + 0;
Addstream[i] = temp + 1;
temp = random.nextInt(320) % (Addstream[i] + 1);
Addstream[i+1] = temp + 1;
temp = random.nextInt(320)%(319 - Addstream[i+1]) + (Addstream[i+1] + 1);
if (i+2 >= 320){ break; } Addstream[i+2] = temp; }
for (int i = 0; i < 320; i++){ Addspage[i] = Addstream[i] / 10; } } //用户内存及相关数据初始化
private void initialization(){
for (int i = 0 ; i < 32 ;i++){ phyBlock[i] = -1; }
this.npageNum = 0;
this.rate = 0;
this.tempK = 0;
this.tempG = -1;
this.tempF = -1; }
public void FIFO() {
int[] time = new int[32]; //定义进入内存时间长度数组
int max; //max表示进入内存时间最久的,即最先进去的
initialization();
for(int i = 0; i < blockNum ; i++)
time[i]=i+1;
for (int i = 0 ; i < 320; i++) {
this.tempK = 0;
for (int j = 0; j < blockNum; j++)
if (phyBlock[j] == Addspage[i])
{ //表示内存中已有当前要调入的页面
this.tempG = j;
break; }
for (int j = 0; j < blockNum ;j++)
if (phyBlock[j] == -1) {//用户内存中存在空的物理块
this.tempF = j;
break; }
if (this.tempG != -1)
this.tempG = -1;
else { if (this.tempF == -1)
{//找到最先进入内存的页面
max = time[0];
for(int j = 0; j < blockNum ;j++)
if(time[j] > max) {
max = time[j];
this.tempK = j; }
phyBlock[tempK] = Addspage[i];
time[tempK] = 0; //该物理块中页面停留时间置零
npageNum++; //缺页数+1
}
else {
phyBlock[tempF] = Addspage[i];
time[tempF] = 0;
npageNum++;
tempF = -1; } }
for (int j =0; j < blockNum && phyBlock[j] != -1; j++)
time[j]++; //物理块中现有页面停留时间+1
}
this.rate = 1 - (float)npageNum / 320;
dataStrings[blockNum][1] = this.rate+""; }
public void LRU(){
int[] time = new int[32];
int max;
initialization();
for (int i = 0; i < blockNum ;i++)
time[i] = i + 1;
for (int i = 0; i < 320 ;i++){
tempK=0;
for (int j = 0;j < blockNum ; j++)
if (phyBlock[j] == Addspage[i]){
this.tempG = j;
break; }
for (int j = 0;j < blockNum ; j++)
if (phyBlock[j] == -1){
this.tempF = j;
break; }
if (this.tempG != -1)
{ time[tempG] = 0;
this.tempG = -1; }
else{
if (this.tempF == -1)
{ max=time[0];
for (int j = 0; j < blockNum ;j++)
if (time[j] > max)
{ this.tempK = j;
max=time[j]; }
phyBlock[tempK] = Addspage[i];
time[tempK] = 0;
npageNum++; }
else
{ phyBlock[tempF] = Addspage[i];
time[tempF] = 0;
npageNum++;
this.tempF = -1;
} }
for (int j = 0;j < blockNum && phyBlock[j] != -1 ; j++)
time[j]++;
}
this.rate = 1 - (float)npageNum / 320;
dataStrings[blockNum][2] = this.rate+"";
}
public void LFR()
{ initialization();
int[] time_lru = new int[32];
int [] time = new int[32];
int min,max_lru,t;
for (int i = 0; i < blockNum; i++)
{ time[i]=0;
time_lru[i]=i+1; }
for (int i = 0;i < 320; i++)
{ this.tempK = 0;
t=1;
for (int j = 0; j < blockNum; j++)
if (phyBlock[j] == Addspage[i]){
this.tempG = j;
break; }
for (int j = 0; j < blockNum; j++)
if (phyBlock[j] == -1)
{ this.tempF = j;
break; }
if (this.tempG != -1)
{ time_lru[tempG] = 0;
this.tempG = -1; }else
{ if (this.tempF == -1){
if (i <= 20){//将最少使用的间隔时间定位个单位
max_lru=time_lru[0]; //在未达到“一定时间”的要求时,先采用LRU进行页面置换
for (int j = 0;j < blockNum; j++)
if (time_lru[j] > max_lru) {
this.tempK = j;
max_lru = time_lru[j];
}
phyBlock[tempK] = Addspage[i];
time_lru[tempK]=0;
npageNum++;
}else
{ for (int j = 0;j < blockNum; j++) //计算一定时间间隔内物理块中的页面使用次数
for (int h = i-1;h >= i - 1; h--)
if (phyBlock[j] == Addspage[h])
time[j]++;
min=time[0];
for (int j = 0; j < blockNum; j++)
if (time[j] < min){
min=time[j];
this.tempK = j; }
for (int j = 0;j < blockNum; j++) //应对出现页面使用次数同样少的情况
if (time[j] == min)
t++;
if (t>1) {//若使用次数同样少,将次数相同的页面按照LRU进行页面置换
max_lru=time_lru[tempK];
for (int j =0; j < blockNum && time[j] == min ;j++)
if (time_lru[j]>max_lru){
this.tempK = j;
max_lru=time_lru[j];
} }
phyBlock[tempK] = Addspage[i];
time_lru[tempK] = 0;
npageNum++;
} }
else{
phyBlock[tempF] = Addspage[i];
time_lru[tempF] = 0;
npageNum++;
tempF = -1;
} }
for (int j = 0; j < blockNum && phyBlock[j] != -1; j++)
time_lru[j]++;
}
this.rate = 1 - (float)npageNum / 320;
dataStrings[blockNum][3] = this.rate+"";
}
public int getBlockNum()
{ return blockNum; }
public void setBlockNum(int blockNum)
{ this.blockNum = blockNum; }
public String[][] getDataStrings()
{ return dataStrings; }
public void setDataStrings(String[][] dataStrings)
{ this.dataStrings = dataStrings; } }
第二步:创建ManageSWT类
public class ManageSWT {
public static void main(String[] args) {
MemoryManage_Operation mOperation = new MemoryManage_Operation();
mOperation.setProduceAddstream();
String[][] tempStrings = new String[33][4];
for (int i = 2; i <= 32; i++) {
mOperation.setBlockNum(i);
tempStrings[i][0] = i + "K";
mOperation.FIFO();
mOperation.LRU();
mOperation.LFR();
tempStrings[i][1] = mOperation.getDataStrings()[i][1];
tempStrings[i][2] = mOperation.getDataStrings()[i][2];
tempStrings[i][3] = mOperation.getDataStrings()[i][3];
}
System.out.print("物理块数"+" "+"先进先出的算法"+" "+"最近最少使用算法"+" "+"最少访问页面算法");
for (int i = 2; i < 33; i++) {
System.out.println("");
for (int j = 0; j < 4; j++) {
System.out.print(tempStrings[i][j]+" ");
}
}
}
}
运行截图


博客主要介绍代码相关内容,包括第一步创建MemoryManage_Operation类,第二步创建ManageSWT类,还展示了代码运行截图。
226





