📄 mainmemory.java
字号:
/* * MainMemory.java * * Created on 2006年3月15日, 下午9:56 * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */package os.memory;/** * * @author Vernkin */import java.util.*;import javax.swing.*;import os.memory.harddisk.HardDiskDriver;import os.process.*;/** * Play as main memory * The OS simulator only hava a Object */public class MainMemory{ public final String name = "[MainMemory]"; /** * The Size of each page */ public final static int PAGE_SIZE = 16; /** * The number of page */ public final static int PAGE_NUM = 128; /** * memory is store into int array */ private int[] memory = new int[PAGE_NUM * PAGE_SIZE]; /** * boolean array indicates used pages */ private boolean[] isUsed = new boolean[PAGE_NUM]; /** * Store all the MyProcess page table */ private LinkedList alloc = new LinkedList(); /** * The Only Object of MainMemory */ private static MainMemory mm = new MainMemory(); private HardDiskDriver hd; private static JTextArea commonOut; /**Only a object**/ private MainMemory(){ hd = HardDiskDriver.getInstance(); } public static void setCommonOut(JTextArea ja){ commonOut = ja; } /** * Get the only object of MainMemory * @return the only object of MainMemory */ public static MainMemory getInstance(){ return mm; } /** * Get the page Table determined by the param p * @param p process * @return If the pageTable can,t be found in page table list,return null */ public PageTable getPageTable(os.process.MyProcess p){ for(int i=0;i<alloc.size();i++){ PageTable temp = (PageTable)alloc.get(i); if(temp.sameProcess(p)) return temp; } return null; } /** * load a serial data into the main memory * @param data in form if integer array * @param process which process wants to * @return If cannot load this MyProcess into the main memory totally, * it will return false */ public boolean LoadInMemory(int[] data,os.process.MyProcess process){ int page = getFreePage(); if(page<0) return false; /**allocate process context**/ isUsed[page] = true;; PageTable pt = new PageTable(process, page); int pageSize=0; if(data.length % PAGE_SIZE == 0) pageSize = data.length / PAGE_SIZE; else pageSize = data.length / PAGE_SIZE + 1; /**load the data**/ page = 0; int j=0; for(int i=0;i<pageSize;i++){ page = getFreePage(); if(page<0){ /**release the allready used memory**/ releaseMemory(pt.getAllUsedPage()); return false; } isUsed[page] = true; pt.add(i,page); for(int k=0;k<PAGE_SIZE && j<data.length;k++){ memory[page*PAGE_SIZE + k] = data[j++];// System.out.println("Load "+data[j-1]); } } alloc.add(pt); // System.out.println("Context page "+process+" "+pt.getContextPage()); commonOut.append(name+" Allocate Process "+process+" with size "+pt.getSize()+"\n"); process.setAllocatedMemory(pt.getSize()); return true; } public boolean allocateMemory(os.process.MyProcess p,String name){ // System.out.println("allocateMemory "+name+ " "+p); int[] data = hd.getData(name); return LoadInMemory(data, p); } /** * 确定某个进程是否有访问某一页的权限 * @param id * @param page * @return */ public boolean haveRightAccess(int id,int page){ PageTable pt; for(int i=0;i<alloc.size();i++){ pt = (PageTable)alloc.get(i); if(pt.getID() == id) return pt.isBelonged(page); } return true; } /** * get a free page * @return a free page number * if not exist , return -1 */ private int getFreePage(){ for(int i=0;i<PAGE_NUM;i++) if(!isUsed[i]) return i; return -1; } /** * Get the process context value at offset * @param p process * @param offset process context offset * @return the process context value at offset */ public int getContextValueAt(os.process.MyProcess p,int offset){ PageTable pt = this.getPageTable(p); if(pt == null){ System.out.println(p+" not exist in the main memory"); System.exit(1); } int page = pt.getContextPage(); return getValueAt(page,offset); } /** * get value at a main memory space * @param p indicates a certain page table * @param address destination in the main memory * @return value at a main memory determined by address */ public int getValueAt(os.process.MyProcess p,int address){ PageTable pt = this.getPageTable(p); if(pt == null){ System.out.println(p+" not exist in the main memory"); System.exit(1); } int page = pt.getPhysicalPage(address/PAGE_SIZE); return getValueAt(page,address % PAGE_SIZE); } /** * get value at * @param address destination * @return value whick location indicated by address */ public int getValueAt(int address){ // System.out.println("MM getValue At "+address); if(address<0 || address >= PAGE_NUM * PAGE_SIZE){ System.out.println("MainMemoey.getValueAt() "+"超过物理地址的容量"); System.exit(1); } return memory[address]; } /** * get value at * @param page page number * @param offset page offset * @return value whick location indicated by address */ public int getValueAt(int page,int offset){ return getValueAt(page * PAGE_SIZE + offset); } /** * set process p context value at offset * @param p process * @param offset process context offset * @param value new value */ public void setContextValueAt(os.process.MyProcess p,int offset,int value){ PageTable pt = this.getPageTable(p); if(pt == null){ System.out.println(p+" not exist in the main memory"); System.exit(1); } int page = pt.getContextPage(); setValueAt(page,offset,value); } /** * set value at a main memory space * @param p indicates a certain page table * @param address destination * @param value new value */ public void setValueAt(os.process.MyProcess p,int address,int value){ PageTable pt = this.getPageTable(p); if(pt == null){ System.out.println(p+" not exist in the main memory"); System.exit(1); } int page = pt.getPhysicalPage(address/PAGE_SIZE); setValueAt(page,address % PAGE_SIZE,value); } /** * set value at a main memory space * @param address destination * @param value new value */ public void setValueAt(int address,int value){ if(address<0 || address >= PAGE_NUM * PAGE_SIZE){ System.out.println("MainMemoey.getValueAt() "+"超过物理地址的容量"); System.exit(1); } memory[address] = value; } /** * set value at a main memory space * @param page address page * @param offset page offset * @param value new value */ public void setValueAt(int page,int offset,int value){ setValueAt(page * PAGE_SIZE + offset,value); } /** * Set Process Status Word * @param p process * @param data int array , stored PSW */ public void setPSW(os.process.MyProcess p,int[] data){ PageTable pt = this.getPageTable(p); if(pt == null){ System.out.println(p+" not exist in the main memory"); System.exit(1); } int page = pt.getContextPage(); for(int i=0;i<11;i++) memory[page*PAGE_SIZE + i] = data[i]; } /** * Get Process Status Word * @param p process * @return PSW,store in a array */ public int[] getPSW(os.process.MyProcess p){ PageTable pt = this.getPageTable(p); if(pt == null){ System.out.println(p+" not exist in the main memory"); System.exit(1); } int page = pt.getContextPage(); int[] data = new int[11]; for(int i=0;i<11;i++) data[i] = memory[page*PAGE_SIZE + i]; return data; } public void releaseMemory(os.process.MyProcess p){ PageTable pt = this.getPageTable(p); if(pt == null){ System.out.println(p+" not exist in the main memory"); System.exit(1); } commonOut.append(name+" release Process "+p+" with size "+pt.getSize()+"\n"); releaseMemory(pt.getAllUsedPage()); alloc.remove(pt); } public void releaseMemory(int[] page_group){ for(int i=0;i<page_group.length;i++) releaseAPage(page_group[i]); } public void releaseAPage(int page){ if(page<0 || page>= PAGE_NUM) return; for(int i=0;i<PAGE_SIZE;i++) memory[page*PAGE_SIZE + i] = 0; // System.out.println("release "+page); isUsed[page] = false; } public MyProcess[] getMainMemoryProcess(){ if(alloc.isEmpty()) return null; int size = alloc.size(); MyProcess[] ret = new MyProcess[size]; for(int i=0;i<size;i++) ret[i] = ((PageTable)alloc.get(i)).getProcess(); return ret; } public int getTotalMemory(){ return PAGE_NUM; } public int getUsedMemory(){ int count=0; for(int i=0;i<PAGE_NUM;i++) if(isUsed[i]) count++; return count; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -