设计用于模拟快表、页表、地址变换所用的寄存器的数据结构;
编制页表的初始信息文件,举例说明文件中具有的信息:共有5块,每块的状态、在内存和外存的起始地址等。
编程实现虚拟存储器地址变换算法程序,动态输入所要访问的逻辑地址,变换过程文字描述以及变换后的物理地址;
测试:输入有效、无效地址,测试程序的正确性和错误处理能力。
实验结果:
虚拟地址变换程序提供逻辑地址输入界面,形象地表示出变换成物理地址的过程与最后变换成的物理地址。
快表的地址变换原理
需要输入一个数 地址变换算法计算出所在的页号 先访问快表 找不到再访问页表 根据表里的块号 计算出物理地址 块号*块大小+偏移量
页表结构
PageTableManager
package AddressTransformation;
import java.util.HashMap;
import java.util.Map;
public class PageTableManager {
private Map<Integer, PageTableEntry> pageTable = new HashMap<>();
public static class PageTableEntry {
private int frameNumber;
private boolean present;
private int pageNumber;
public PageTableEntry(int pageNumber, int frameNumber, boolean present) {
this.pageNumber = pageNumber;
this.frameNumber = frameNumber;
this.present = present;
}
public int getFrameNumber() {
return frameNumber;
}
public boolean isPresent() {
return present;
}
public int getPageNumber() {
return pageNumber;
}
}
public void setEntry(int pageNumber, int frameNumber, boolean present) {
pageTable.put(pageNumber, new PageTableEntry(pageNumber, frameNumber, present));
}
public PageTableEntry getEntry(int pageNumber) {
return pageTable.get(pageNumber);
}
}
快表结构
TLBManager
package AddressTransformation;
import java.util.HashMap;
import java.util.Map;
public class TLBManager {
private Map<Integer, TLBEntry> tlb = new HashMap<>();
public static class TLBEntry {
private int virtualPageNumber;
private int physicalFrameNumber;
public TLBEntry(int virtualPageNumber, int physicalFrameNumber) {
this.virtualPageNumber = virtualPageNumber;
this.physicalFrameNumber = physicalFrameNumber;
}
public int getVirtualPageNumber() {
return virtualPageNumber;
}
public int getPhysicalFrameNumber() {
return physicalFrameNumber;
}
}
public TLBEntry lookup(int virtualPageNumber) {
return tlb.get(virtualPageNumber);
}
public void update(int virtualPageNumber, int physicalFrameNumber) {
tlb.put(virtualPageNumber, new TLBEntry(virtualPageNumber, physicalFrameNumber));
}
}
地址变换算法
AddressTransformation.java
package AddressTransformation;
public class AddressTransformation {
private PageTableManager pageTableManager;
private TLBManager tlbManager;
private int pageSize;
public AddressTransformation(int pageSize) {
this.pageSize = pageSize;
this.pageTableManager = new PageTableManager();
this.tlbManager = new TLBManager();
}
public String addressTranslation(int virtualAddress) {
int pageNumber = virtualAddress / pageSize;
int offset = virtualAddress % pageSize;
TLBManager.TLBEntry tlbEntry = tlbManager.lookup(pageNumber);
if (tlbEntry != null) {
int frameNumber = tlbEntry.getPhysicalFrameNumber();
return "TLB 命中。物理地址:" + (frameNumber * pageSize + offset) + "(帧号:" + frameNumber + ",偏移量:" + offset + ")";
} else {
PageTableManager.PageTableEntry ptEntry = pageTableManager.getEntry(pageNumber);
if (ptEntry != null && ptEntry.isPresent()) {
int frameNumber = ptEntry.getFrameNumber();
tlbManager.update(pageNumber, frameNumber);
return "TLB 未命中。物理地址:" + (frameNumber * pageSize + offset) + "(帧号:" + frameNumber + ",偏移量:" + offset + ")";
} else {
return "页面错误。页号 " + pageNumber + " 不在内存中。";
}
}
}
public void setPageTableEntry(int pageNumber, int frameNumber, boolean present) {
pageTableManager.setEntry(pageNumber, frameNumber, present);
}
public void setTLBEntry(int virtualPageNumber, int physicalFrameNumber) {
tlbManager.update(virtualPageNumber, physicalFrameNumber);
}
}
测试类
Test.java
package AddressTransformation;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
AddressTransformation simulator = new AddressTransformation(1024); // 假设页面大小为1024
// 初始化页表和TLB
simulator.setPageTableEntry(1, 0, true);
simulator.setPageTableEntry(2, 1, true);
simulator.setPageTableEntry(3, 2, true);
simulator.setPageTableEntry(4, 3, true);
simulator.setPageTableEntry(5, 4, true);
simulator.setTLBEntry(1, 0);
simulator.setTLBEntry(2, 1);
Scanner scanner = new Scanner(System.in);
System.out.println("输入虚拟地址(或输入-1退出):");
while (true) {
int virtualAddress = scanner.nextInt();
if (virtualAddress == -1) {
break;
}
String result = simulator.addressTranslation(virtualAddress);
System.out.println(result);
System.out.println("输入虚拟地址(或输入-1退出):");
}
scanner.close();
}
}
测试结果