存储管理算法java代码
生活随笔
收集整理的這篇文章主要介紹了
存储管理算法java代码
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
代碼
第一步:創(chuàng)建MemoryManage_Operation類
public class MemoryManage_Operation {private String[][] dataStrings = new String[33][4];//數(shù)據(jù)傳遞數(shù)組private int[] Addstream = new int[320];//地址流private int[] Addspage = new int[320];//頁面流private int[] phyBlock = new int[32];//物理塊數(shù)private Random random = new Random();//隨機數(shù)private int blockNum;//內(nèi)存塊數(shù)private int npageNum;//缺頁數(shù)臨時變量private float rate;//缺頁率private int tempK, tempG, tempF;//臨時變量// 產(chǎn)生隨機地址流和頁面流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; } } //用戶內(nèi)存及相關(guān)數(shù)據(jù)初始化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]; //定義進入內(nèi)存時間長度數(shù)組int max; //max表示進入內(nèi)存時間最久的,即最先進去的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]){ //表示內(nèi)存中已有當前要調(diào)入的頁面this.tempG = j;break; }for (int j = 0; j < blockNum ;j++)if (phyBlock[j] == -1) {//用戶內(nèi)存中存在空的物理塊this.tempF = j;break; }if (this.tempG != -1)this.tempG = -1;else { if (this.tempF == -1){//找到最先進入內(nèi)存的頁面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++; //缺頁數(shù)+1}else {phyBlock[tempF] = Addspage[i];time[tempF] = 0;npageNum++;tempF = -1; } }for (int j =0; j < blockNum && phyBlock[j] != -1; j++)time[j]++; //物理塊中現(xiàn)有頁面停留時間+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++) //計算一定時間間隔內(nèi)物理塊中的頁面使用次數(shù)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++) //應對出現(xiàn)頁面使用次數(shù)同樣少的情況if (time[j] == min)t++;if (t>1) {//若使用次數(shù)同樣少,將次數(shù)相同的頁面按照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; } }第二步:創(chuàng)建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("物理塊數(shù)"+" "+"先進先出的算法"+" "+"最近最少使用算法"+" "+"最少訪問頁面算法");for (int i = 2; i < 33; i++) {System.out.println("");for (int j = 0; j < 4; j++) {System.out.print(tempStrings[i][j]+" ");}}} }運行截圖
總結(jié)
以上是生活随笔為你收集整理的存储管理算法java代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络编程基础函数
- 下一篇: 银行家算法java代码