设主存由8个存储体按低位交叉编址方式组成,主存容量1M字,Cache容量为
4K字,要求一个主存周期从主存取得一个块。采用全相联地址映像,用相联目录表实现地
址变换。求相联目录表的行数、比较位数、宽度和总位数。
这里是是计算相联目录表的行数、比较位数、宽度和总位数的代码:
java
import java.util.Scanner;
public class LinkedDirectoryTable {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("输入相联目录表中项目个数:");
int n = scanner.nextInt();
// 行数 = n + 1
int rowNum = n + 1;
System.out.println("行数为:" + rowNum);
// 比较位数 = log2n
int compareDigit = (int) Math.ceil(Math.log(n) / Math.log(2));
System.out.println("比较位数为:" + compareDigit);
// 宽度 = n * 比较位数
int width = n * compareDigit;
System.out.println("宽度为:" + width);
// 总位数 = n * (log2n + 1)
int totalDigit = n * (compareDigit + 1);
System.out.println("总位数为:" + totalDigit);
}
}
该程序实现了:
存储体、主存和缓存的容量有以下关系:
存储体(硬盘)
|
|
▼
主存(内存)
|
|
▼
缓存(CPU Cache)
从上至下,存储速度越来越快,容量越来越小。CPU首先从缓存中查找数据,如果没有找到才去主存中查找,最后才访问存储体。