1.利用随机数产生一个指令序列,共320条指令。其地址按下述原则生成:
①50%的指令是顺序执行的;
②25%的指令是均匀分布在前地址部分;
③25%的指令是均匀分布在后地址部分;
Instruction.java源代码:
package instructions; import java.util.ArrayList; /** * The instruction class. * @author DigitalSonic */ public class Instruction { private String name = null; private boolean inSequence = false; private ArrayList<Instruction> list = null; /** * Construct the instruction with a specified name. * @param Name The name of the instruction. */ public Instruction(String Name) { this.name = Name; } /** * Add the instruction to the sequence. * @param List the instruction sequence */ public void addToList(ArrayList List) { this.list = List; this.inSequence = true; List.add(this); } /** * Remove the instruction from the sequence. */ public void removeFromList() { this.list.remove(this); this.list = null; this.inSequence = false; } /** * Getter for property name. * @return Value of property name. */ public String getName() { return name; } /** * Getter for property inSequence. * @return Value of property inSequence. */ public boolean isInSequence() { return this.inSequence; } }
Sequence.java源代码:
package instructions; import java.util.ArrayList; /** * The instructions sequence class. * @author DigitalSonic */ public class Sequence { private Instruction[] instructions; private ArrayList<Instruction> list = new ArrayList<Instruction>(Page.PAGE_SIZE * VirtualMemory.TOTAL_PAGE_COUNT); /** * Construct the sequence with the given instructions * @param Instructions The instructions used to construct the sequence. */ public Sequence(Instruction[] Instructions) { this.instructions = Instructions; generateSequence(); } /** * Print the sequence. */ public void printSequence() { for (int i = 0; i < Page.PAGE_SIZE * VirtualMemory.TOTAL_PAGE_COUNT; i++) { System.out.print(list.get(i).getName() + " "); } } /** * Getter for property instructions. * @return Value of property instructions. */ public Instruction[] getInstructions() { return instructions; } /** * Setter for property instructions. * @param instructions New value of property instructions. */ public void setInstructions(Instruction[] instructions) { this.instructions = instructions; } /** * Getter for property list. * @return Value of property list. */ public java.util.ArrayList<Instruction> getList() { return list; } /** * Setter for property list. * @param list New value of property list. */ public void setList(java.util.ArrayList list) { this.list = list; } private void generateSequence() { list.clear(); try { instructions[getRandom(0, Page.PAGE_SIZE * VirtualMemory.TOTAL_PAGE_COUNT)].addToList(list); for (int i = 1; i< Page.PAGE_SIZE * VirtualMemory.TOTAL_PAGE_COUNT; i++) getNextInstruction(list.get(i - 1)).addToList(list); } catch (IndexOutOfBoundsException ex) { generateSequence(); } } private Instruction getNextInstruction(Instruction Current) throws IndexOutOfBoundsException { Instruction Next = null; if (Math.random() < 0.5) { Next = instructions[Integer.parseInt(Current.getName()) + 1]; } else { int Min, Max; if (Math.random() < 0.5) { Min = 0; Max = (int)(Page.PAGE_SIZE * VirtualMemory.TOTAL_PAGE_COUNT * 0.5); } else { Min = (int)(Page.PAGE_SIZE * VirtualMemory.TOTAL_PAGE_COUNT * 0.5); Max = Page.PAGE_SIZE * VirtualMemory.TOTAL_PAGE_COUNT; } while (Next == null) if (!instructions[getRandom(Min,Max)].isInSequence()) Next = instructions[getRandom(Min,Max)]; } return Next; } private int getRandom(int Min, int Max) { return (int)((Max - Min) * Math.random() + Min); } }
2. 指令序列变换成页地址流
设:页面大小为1K; 用户虚存容量为32K。
在用户虚存中,按每1K存放10条指令排列虚存地址,即320条指令在虚存中的存放方式为:
第0条—第9条指令为第0页(对应虚存地址为[0,9]);
第10条—第19条指令为第1页(对应虚存地址为[10,19]);
310条—第319条指令为第31页(对应虚存地址为[310,319]);
按以上方式,用户指令可组成32页。
Page.java源代码:
package instructions; /** * The page that stores instructions. * @author DigitalSonic */ public class Page { /** * The total count of the instructions stored in this page. */ public static final int PAGE_SIZE = 10; private Instruction[] instructions = null; private int visitTime; /** * Creates a new instance of Page. * @param Instructions The instructions stored in this page. */ public Page(Instruction[] Instructions) { this.instructions = Instructions; } /** * Getter for property instructions. * @return Value of property instructions. */ public Instruction[] getInstructions() { return this.instructions; } /** * Getter for property visitTime. * @return Value of property visitTime. */ public int getVisitTime() { return this.visitTime; } /** * Setter for property visitTime. * @param visitTime New value of property visitTime. */ public void setVisitTime(int visitTime) { this.visitTime = visitTime; } }
VirtualMemory.java源代码:
package instructions; /** * The virtual page memory. * @author DigitalSonic */ public class VirtualMemory { /** * The total count of the pages in this virtual memory. */ public static final int TOTAL_PAGE_COUNT = 32; /** * The count of the pages that user has. */ public static int UserPagesCount = 0; private Instruction[] instructions = null; private Page[] pages = null; private Page[] userPages = null; private int currentEmptyIndex; /** * Creates a new instance of VirtualMemory. */ public VirtualMemory() { int m, n; Instruction[] Tmp; Page[] Pages = new Page[TOTAL_PAGE_COUNT]; //generate instructions instructions = new Instruction[Page.PAGE_SIZE * VirtualMemory.TOTAL_PAGE_COUNT]; for (int i = 0; i < Page.PAGE_SIZE * VirtualMemory.TOTAL_PAGE_COUNT; i++) instructions[i] = new Instruction(Integer.toString(i)); //generate pages for (m = 0; m < TOTAL_PAGE_COUNT; m++) { Tmp = new Instruction[Page.PAGE_SIZE]; for (n = 0; n < Page.PAGE_SIZE; n++) Tmp[n] = instructions[m * Page.PAGE_SIZE + n]; Pages[m] = new Page(Tmp); } this.pages = Pages; } /** * Creates a new instance of VirtualMemory. * @param Pages The memory pages stored in this virtual memory. */ public VirtualMemory(Page[] Pages) { this.pages = Pages; } /** * Getter for property pages. * @return Value of property pages. */ public Page[] getPages() { return this.pages; } /** * Getter for property instructions. * @return Value of property instructions. */ public Instruction[] getInstructions() { return this.instructions; } /** * Setter for property instructions. * @param instructions New value of property instructions. */ public void setInstructions(Instruction[] instructions) { this.instructions = instructions; } /** * Getter for property userPages. * @return Value of property userPages. */ public Page[] getUserPages() { return this.userPages; } /** * Setter for property userPages. * @param userPages New value of property userPages. */ public void setUserPages(Page[] userPages) { this.userPages = userPages; UserPagesCount = userPages.length; } /** * Initialize the user pages. */ public void initUserPages() { initUserPages(this.UserPagesCount); } /** * Initialize the user pages. * @param Count The count of the user pages. */ public void initUserPages(int Count) { this.UserPagesCount = Count; this.userPages = new Page[Count]; this.currentEmptyIndex = 0; } /** * Getter for property currentEmptyIndex. * @return Value of property currentEmptyIndex. */ public int getCurrentEmptyIndex() { return this.currentEmptyIndex; } /** * Setter for property currentEmptyIndex. * @param currentEmptyIndex New value of property currentEmptyIndex. */ public void setCurrentEmptyIndex(int currentEmptyIndex) { this.currentEmptyIndex = currentEmptyIndex; } }
3. 计算并输出下述各种算法在不同内存容量(用户内存容量为4页到32页)下的缺页率。
A. FIFO先进先出的算法
B. LRU最近最少使用算法
C. OPT最佳淘汰算法
Algorithm.java源代码:
package instructions.algorithm; import instructions.*; /** * The abstract class represents algorithm. * @author DigitalSonic */ public abstract class Algorithm { private int lackCount = 0; protected String name; protected int userPagesCount = 0; protected VirtualMemory VM = null; protected Sequence seq = null; /** * Creates a new instance of Algorithm */ public Algorithm(int UserPagesCount, Sequence Seq, VirtualMemory VM) { this.userPagesCount = UserPagesCount; this.seq = Seq; this.VM = VM; } /** * To check whether the page is in the user pages. * @param page The wanted page. * @return True if the page is in the user pages, else it returns false. */ public boolean isPageInUserPages(Page page) { Page[] Pages = VM.getUserPages(); boolean Flag = false; for (int i = 0; i < Pages.length; i++) { if (Pages[i] == page) { Flag = true; break; } } if (!Flag) lackCount++; return Flag; } /** * The execution part of the algorithm. */ public void execute() { Page TargetPage = null; VM.initUserPages(userPagesCount); for (int i = 0; i < seq.getList().size(); i++) { TargetPage = VM.getPages()[Integer.parseInt(seq.getList().get(i).getName()) / Page.PAGE_SIZE]; TargetPage.setVisitTime(i); if (!isPageInUserPages(TargetPage)) replacePage(TargetPage); } System.out.println("Lack page count:" + (getLackCount() - userPagesCount) + " Rate:" + 100 * ((double)(getLackCount() - userPagesCount) / (double)seq.getList().size()) + "%"); } /** * Replace a page with the new one. * @param NewPage The page ready to be inserted. */ public abstract void replacePage(Page NewPage); /** * Getter for property name. * @return Value of property name. */ public String getName() { return this.name; } /** * Getter for property userPagesCount. * @return Value of property userPagessCount. */ public int getUserPagesCount() { return this.userPagesCount; } /** * Getter for property VM. * @return Value of property VM. */ public VirtualMemory getVM() { return this.VM; } /** * Getter for property seq. * @return Value of property seq. */ public Sequence getSeq() { return this.seq; } /** * Getter for property lackCount. * @return Value of property lackCount. */ public int getLackCount() { return this.lackCount; } }
FIFO.java源代码:
package instructions.algorithm; import instructions.*; /** * The implementation of "First In First Out" algorithm. * @author DigitalSonic */ public class FIFO extends Algorithm { /** * Creates a new instance of FIFO */ public FIFO(int UserPagesCount, Sequence Seq, VirtualMemory VM) { super(UserPagesCount, Seq, VM); this.name = "FIFO"; } /** * Replace a page with the new one. * @param NewPage The page ready to be inserted. */ public void replacePage(Page NewPage) { if (VM.getCurrentEmptyIndex() != VM.UserPagesCount) { VM.getUserPages()[VM.getCurrentEmptyIndex()] = NewPage; VM.setCurrentEmptyIndex(VM.getCurrentEmptyIndex() + 1); } else { VM.getUserPages()[findFIPage(VM.getUserPages())] = NewPage; } } private int findFIPage(Page[] Pages) { int Index = Page.PAGE_SIZE * VirtualMemory.TOTAL_PAGE_COUNT; for (int i = 0; i < Pages.length; i++) { if (Pages[i].getVisitTime() < Index) Index = i; } return Index; } }
LRU.java源代码:
package instructions.algorithm; import instructions.*; /** * The implementation of "Least Recently Used" algorithm. * @author DigitalSonic */ public class LRU extends Algorithm { /** * Creates a new instance of LRU */ public LRU(int UserPagesCount, Sequence Seq, VirtualMemory VM) { super(UserPagesCount, Seq, VM); this.name = "LRU"; } /** * Replace a page with the new one. * @param NewPage The page ready to be inserted. */ public void replacePage(Page NewPage) { if (VM.getCurrentEmptyIndex() != VM.UserPagesCount) { VM.getUserPages()[VM.getCurrentEmptyIndex()] = NewPage; VM.setCurrentEmptyIndex(VM.getCurrentEmptyIndex() + 1); } else { VM.getUserPages()[findLRUPage(VM.getUserPages(), NewPage.getVisitTime())] = NewPage; } } private int findLRUPage(Page[] Pages, int CurrentInstruction) { int[] Count = new int[Pages.length]; Page page; for (int m = 0; m < Pages.length; m++) { Count[m] = 0; page = Pages[m]; for (int n = 0; n < CurrentInstruction && n < seq.getList().size(); n++) { if (page == VM.getPages()[Integer.parseInt(seq.getList().get(n).getName()) / Page.PAGE_SIZE]) Count[m]++; } } return findMin(Count); } private int findMin(int[] Count) { int Min = Page.PAGE_SIZE * VirtualMemory.TOTAL_PAGE_COUNT; int Index = 0; for (int i = 0; i < Count.length; i++) { if (Count[i] < Min) { Min = Count[i]; Index = i; } } return Index; } }
OPT.java源代码:
package instructions.algorithm; import instructions.*; /** * The implementation of "Optimal" algorithm. * @author DigitalSonic */ public class OPT extends Algorithm { /** * Creates a new instance of OPT */ public OPT(int UserPagesCount, Sequence Seq, VirtualMemory VM) { super(UserPagesCount, Seq, VM); this.name = "OPT"; } /** * Replace a page with the new one. * @param NewPage The page ready to be inserted. */ public void replacePage(Page NewPage) { if (VM.getCurrentEmptyIndex() != VM.UserPagesCount) { VM.getUserPages()[VM.getCurrentEmptyIndex()] = NewPage; VM.setCurrentEmptyIndex(VM.getCurrentEmptyIndex() + 1); } else { VM.getUserPages()[findUnusedPage(VM.getUserPages(), NewPage.getVisitTime())] = NewPage; } } private int findUnusedPage(Page[] Pages, int CurrentInstruction) { int[] Pos = new int[Pages.length]; Page page; for (int m = 0; m < Pages.length; m++) { Pos[m] = 0; page = Pages[m]; for (int n = CurrentInstruction; n < seq.getList().size(); n++) { if (page == VM.getPages()[Integer.parseInt(seq.getList().get(n).getName()) / Page.PAGE_SIZE]) { Pos[m] = n; break; } } } return findMax(Pos); } private int findMax(int[] Count) { int Max = 0; int Index = 0; for (int i = 0; i < Count.length; i++) { if (Count[i] > Max) { Max = Count[i]; Index = i; } } return Index; } }
最后当然是一个执行上述代码的TestCase
TestCase.java源代码:
package instructions; import instructions.algorithm.*; /** * Run 3 algorithm and print the result. * @author DigitalSonic */ public class TestCase { public static void main(String[] args) { int i; VirtualMemory VM = new VirtualMemory(); Sequence Seq = new Sequence(VM.getInstructions()); System.out.println("Instructions sequence:"); Seq.printSequence(); System.out.println(); System.out.println("OPT"); for (i = 4; i <= 32; i++) { System.out.print("User pages count:" + i + " "); new OPT(i, Seq, VM).execute(); } System.out.println(); System.out.println("FIFO"); for (i = 4; i <= 32; i++) { System.out.print("User pages count:" + i + " "); new FIFO(i, Seq, VM).execute(); } System.out.println(); System.out.println("LRU"); for (i = 4; i <= 32; i++) { System.out.print("User pages count:" + i + " "); new LRU(i, Seq, VM).execute(); } } }
运行结果:
Instructions sequence: 224 225 226 227 166 167 168 169 120 121 271 272 175 281 222 9 12 191 25 26 172 173 174 275 251 136 137 138 139 256 146 147 148 264 265 14 103 32 271 272 273 274 23 84 143 144 145 146 147 212 213 267 255 44 45 129 288 261 262 263 264 265 266 267 143 144 248 209 210 175 176 177 178 179 180 181 182 183 278 279 260 244 245 246 179 180 205 206 207 166 167 168 60 61 62 3 210 81 78 100 89 305 146 154 46 47 231 9 10 179 180 24 25 107 108 109 110 77 78 79 80 34 35 296 297 279 280 154 155 156 33 34 46 47 48 49 58 110 127 306 218 219 284 285 93 94 95 12 13 294 282 144 155 52 53 101 102 103 104 105 106 107 143 22 23 24 250 251 252 253 254 225 226 227 120 211 74 75 185 186 187 188 119 120 121 122 81 82 83 84 85 86 60 302 235 192 143 258 259 260 261 262 196 130 131 84 49 50 51 143 144 214 28 29 248 249 41 42 250 191 192 193 219 220 221 222 223 224 55 104 259 260 176 198 199 200 101 265 266 267 68 69 70 162 163 164 165 113 87 115 128 179 22 23 65 185 212 213 227 228 227 228 191 192 75 183 199 225 12 313 314 315 213 137 133 134 135 136 199 200 201 202 203 204 274 15 16 17 20 21 81 82 284 285 286 287 288 284 95 128 233 234 101 220 26 162 252 253 254 255 256 295 222 223 216 217 218 10 11 35 OPT User pages count:4 Lack page count:142 Rate:44.375% User pages count:5 Lack page count:133 Rate:41.5625% User pages count:6 Lack page count:125 Rate:39.0625% User pages count:7 Lack page count:119 Rate:37.1875% User pages count:8 Lack page count:112 Rate:35.0% User pages count:9 Lack page count:103 Rate:32.1875% User pages count:10 Lack page count:95 Rate:29.6875% User pages count:11 Lack page count:88 Rate:27.500000000000004% User pages count:12 Lack page count:81 Rate:25.3125% User pages count:13 Lack page count:75 Rate:23.4375% User pages count:14 Lack page count:69 Rate:21.5625% User pages count:15 Lack page count:63 Rate:19.6875% User pages count:16 Lack page count:57 Rate:17.8125% User pages count:17 Lack page count:52 Rate:16.25% User pages count:18 Lack page count:48 Rate:15.0% User pages count:19 Lack page count:44 Rate:13.750000000000002% User pages count:20 Lack page count:40 Rate:12.5% User pages count:21 Lack page count:36 Rate:11.25% User pages count:22 Lack page count:32 Rate:10.0% User pages count:23 Lack page count:29 Rate:9.0625% User pages count:24 Lack page count:26 Rate:8.125% User pages count:25 Lack page count:22 Rate:6.875000000000001% User pages count:26 Lack page count:18 Rate:5.625% User pages count:27 Lack page count:15 Rate:4.6875% User pages count:28 Lack page count:12 Rate:3.75% User pages count:29 Lack page count:8 Rate:2.5% User pages count:30 Lack page count:5 Rate:1.5625% User pages count:31 Lack page count:2 Rate:0.625% User pages count:32 Lack page count:0 Rate:0.0% FIFO User pages count:4 Lack page count:152 Rate:47.5% User pages count:5 Lack page count:145 Rate:45.3125% User pages count:6 Lack page count:138 Rate:43.125% User pages count:7 Lack page count:135 Rate:42.1875% User pages count:8 Lack page count:128 Rate:40.0% User pages count:9 Lack page count:120 Rate:37.5% User pages count:10 Lack page count:112 Rate:35.0% User pages count:11 Lack page count:104 Rate:32.5% User pages count:12 Lack page count:101 Rate:31.5625% User pages count:13 Lack page count:93 Rate:29.062500000000004% User pages count:14 Lack page count:86 Rate:26.875% User pages count:15 Lack page count:79 Rate:24.6875% User pages count:16 Lack page count:75 Rate:23.4375% User pages count:17 Lack page count:66 Rate:20.625% User pages count:18 Lack page count:56 Rate:17.5% User pages count:19 Lack page count:51 Rate:15.937499999999998% User pages count:20 Lack page count:47 Rate:14.6875% User pages count:21 Lack page count:43 Rate:13.4375% User pages count:22 Lack page count:38 Rate:11.875% User pages count:23 Lack page count:34 Rate:10.625% User pages count:24 Lack page count:28 Rate:8.75% User pages count:25 Lack page count:25 Rate:7.8125% User pages count:26 Lack page count:22 Rate:6.875000000000001% User pages count:27 Lack page count:19 Rate:5.9375% User pages count:28 Lack page count:15 Rate:4.6875% User pages count:29 Lack page count:11 Rate:3.4375000000000004% User pages count:30 Lack page count:5 Rate:1.5625% User pages count:31 Lack page count:2 Rate:0.625% User pages count:32 Lack page count:0 Rate:0.0% LRU User pages count:4 Lack page count:149 Rate:46.5625% User pages count:5 Lack page count:143 Rate:44.6875% User pages count:6 Lack page count:138 Rate:43.125% User pages count:7 Lack page count:132 Rate:41.25% User pages count:8 Lack page count:124 Rate:38.75% User pages count:9 Lack page count:119 Rate:37.1875% User pages count:10 Lack page count:113 Rate:35.3125% User pages count:11 Lack page count:109 Rate:34.0625% User pages count:12 Lack page count:98 Rate:30.625000000000004% User pages count:13 Lack page count:93 Rate:29.062500000000004% User pages count:14 Lack page count:87 Rate:27.187499999999996% User pages count:15 Lack page count:82 Rate:25.624999999999996% User pages count:16 Lack page count:77 Rate:24.0625% User pages count:17 Lack page count:71 Rate:22.1875% User pages count:18 Lack page count:69 Rate:21.5625% User pages count:19 Lack page count:61 Rate:19.0625% User pages count:20 Lack page count:54 Rate:16.875% User pages count:21 Lack page count:49 Rate:15.312500000000002% User pages count:22 Lack page count:43 Rate:13.4375% User pages count:23 Lack page count:38 Rate:11.875% User pages count:24 Lack page count:29 Rate:9.0625% User pages count:25 Lack page count:24 Rate:7.5% User pages count:26 Lack page count:21 Rate:6.5625% User pages count:27 Lack page count:17 Rate:5.3125% User pages count:28 Lack page count:11 Rate:3.4375000000000004% User pages count:29 Lack page count:8 Rate:2.5% User pages count:30 Lack page count:4 Rate:1.25% User pages count:31 Lack page count:2 Rate:0.625% User pages count:32 Lack page count:0 Rate:0.0%