baar - 2007-6-26 0:13:00
学校发了个大作业,要求用JAVA实现虚拟存储技术中的页面置换算法,当中已经给出了FIFO的算法,还有要实现的是LRU 跟LFU算法,请教下应该怎样写~
http://forum.ikaka.com/topic.asp?board=55&artid=8327226在这个帖子里面已经有了LRU 和LFU的算法代码,不过偶水平不够,不知道怎样套在作业里面用~请高手指教下~
谢谢~
baar - 2007-6-26 0:13:00
import java.util.Random;
/**
* 要求这个类中的Durchgang1显示CACHE命中率只有1% 而Durchgang 2的命中率有99,9%.
*
*
*
* 这个类不能被改变.
*/
public class Worker {
public static void main(String[] args) {
int storagesize = 1000;
int cachesize = 10;
Storage storage = new Storage(storagesize);
System.out.println("Durchgang 1:");
testStrategy1(storage, cachesize, new StrategyLIFO(cachesize,
storagesize));
testStrategy1(storage, cachesize, new StrategyLFU(cachesize,
storagesize));
testStrategy1(storage, cachesize,
new StrategyFIFO(cachesize, storagesize));
testStrategy1(storage, cachesize, new StrategyLRU(cachesize,
storagesize));
System.out.println();
System.out.println("Durchgang 2:");
testStrategy2(storage, cachesize, new StrategyLIFO(cachesize,
storagesize));
testStrategy2(storage, cachesize, new StrategyLFU(cachesize,
storagesize));
testStrategy2(storage, cachesize,
new StrategyFIFO(cachesize, storagesize));
testStrategy2(storage, cachesize, new StrategyLRU(cachesize,
storagesize));
WorkerLFU.main(args);
}
private static void testStrategy1(Storage storage, int cachesize,
CacheStrategy cs) //这个方法是在STORAGE中随机抽取一个数值作为被请求的页面(数据)
{
Cache cache = new Cache(storage, cachesize, cs);
Random rng = new Random();
int max = storage.getSize();
for (int i = 0; i < storage.getSize() * 1000; i++) {
int index = rng.nextInt(max);
int[] data = cache.getElement(index);
if (data[0] != index) {
System.err.println("Falsche Daten erhalten.");
}
}
cache.printStats();
}
private static void testStrategy2(Storage storage, int cachesize, //这个方法是在STORAGE中按顺序抽取一个数值作为被请求的页面(数据)而且每个数值抽取1000次,也就是被访问1000次
CacheStrategy cs)
{
Cache cache = new Cache(storage, cachesize, cs);
for (int i = 0; i < storage.getSize() * 1000; i++) {
int index = i / 1000;
int[] data = cache.getElement(index);
if (data[0] != index) {
System.err.println("Falsche Daten erhalten.");
}
}
cache.printStats();
}
}
baar - 2007-6-26 0:14:00
import java.text.*;
/**
*
*
* 这个类不能修改
*/
public class Cache {
private final Storage storage;
private final CacheStrategy strategy;
private int[][] data = null;
private int[] indizes = null;
// 统计部分
/** 页面被请求的次数*/
private int[] requests = null;
/** 被请求的总量 */
private int requestcount = 0;
/** 在CACH 里命中的次数*/
private int hitcount = 0;
/**
* Construktor
* @param storage Speicher hinter dem Cache
* @param size Groesse des Caches
* @param cs Strategie des Caches
*/
public Cache(Storage storage, int size, CacheStrategy cs) {
this.storage = storage;
this.strategy = cs;
data = new int[size][0];
indizes = new int[size];
requests = new int[storage.getSize()];
for (int x = 0; x < indizes.length; x++) {
indizes[x] = -1;
requests[x] = 0;
}
}
/**
* 从Storage中输出一个index 为i的元素。如果CACHE里面有这个元素,则,不用从Storage中抽 * 取,如果没有,则用CacheStrategy里面的CacheElement方法来选取并替换页面*
*
*
* @param i Index der angeforderten Daten
* @return angeforderte Daten
*/
int[] getElement(int i) {
requests++;
requestcount++;
strategy.notifyRequest(i, indizes, requests);
for (int x = 0; x < indizes.length; x++) {
if (indizes[x] == i) {
hitcount++;
return data[x];
}
} //循环判断内存中是否有所需要的数据
int index = strategy.replaceIndex(i, indizes, requests);
indizes[index] = i;
data[index] = storage.getData(i);
return data[index];
}
/**
* Ausgabe der Statistiken.
*/
public void printStats() {
double ratio = ((double) hitcount) / ((double) requestcount) * 100.0;
System.out.println("Cache: " + NumberFormat.getInstance().format(ratio)
+ "%");
// System.out.println(" Requests: " + requestcount);
// System.out.println(" Hits: " + hitcount);
}
} //打印出命中率。如果CPU每次调用到的数据块都在内存中,则命中率是100%
baar - 2007-6-26 0:14:00
/**
*
*
*
* 这个类不能被修改.
*/
public abstract class CacheStrategy {
protected final int cachesize;
protected final int storagesize;
/**
* Construktor
* @param cachesize Groesse des Caches
* @param storagesize Groesse des Speichers
*/
public CacheStrategy(int cachesize, int storagesize) {
this.cachesize = cachesize;
this.storagesize = storagesize;
}
/**
* 某个页面被请求
* @param index
* @param indizes
* @param requests
*/
public abstract void notifyRequest(int index, int[] indizes, int[] requests);
/**
* 确定在Cache被将被替换的Index
*
*
* @param frame index der angeforderten Seite
* @param indizes indizes der Seiten im Cache
* @param requests Anzahl der requests jeder Seite
* @return Index der zu ersetzenden Stelle im Cache
*/
public abstract int replaceIndex(int frame, int[] indizes, int[] requests);
}
baar - 2007-6-26 0:15:00
/**
* 这个类不能被修改
*/
final class StrategyFIFO extends CacheStrategy {
private int index = 0;
StrategyFIFO(int cachesize, int storagesize) {
super(cachesize, storagesize);
}
public void notifyRequest(int i, int[] indizes, int[] requests) {
// Nothing to do
}
public int replaceIndex(int frame, int[] indizes, int[] requests) {
//只要CACHE里面有空闲的位置,则调用下面的循环
for (int x = 0; x < cachesize; x++) {
if (indizes[x] < 0) {
return x;
}
}
// 记录被替换的INDEX
int next = index;
// 计算下次被体会的INDEX
index = (index + 1) % cachesize; //利用取模的方式来标志谁先进来
return next;
}
}
baar - 2007-6-26 0:15:00
/**
*
*
* 这个类不能被修改
*/
public class Storage {
private int[][] data = null;
/** Construktor
* @param size Groesse des Speichers
*/
public Storage(int size) {
data = new int[size][1];
for (int x = 0; x < size; x++) {
data[x][0] = x;
}
}
/** 读取Speichers中的某个数据
*
* @param i Position der Daten
* @return geforderte Daten
*/
public int[] getData(int i) {
return data;
}
/**
*返回Speichers的大小
* @return Speichergroesse
*/
public int getSize() {
return data.length;
}
}
baar - 2007-6-26 0:17:00
这是要填写的方法~
public class StrategyLFU extends CacheStrategy {
StrategyLFU(int cachesize, int storagesize) {
super(cachesize, storagesize);
}
public void notifyRequest(int index, int[] indizes, int[] requests) {
}
public int replaceIndex(int frame, int[] indizes, int[] requests) {
return 0;
}
}
public class StrategyLRU extends CacheStrategy {
StrategyLRU(int cachesize, int storagesize) {
super(cachesize, storagesize);
}
public void notifyRequest(int index, int[] indizes, int[] requests) {
}
public int replaceIndex(int frame, int[] indizes, int[] requests) {
return 0;
}
}
baar - 2007-6-26 0:18:00
这个是例子~
/**
* 这个类不能被修改
*/
final class StrategyFIFO extends CacheStrategy {
private int index = 0;
StrategyFIFO(int cachesize, int storagesize) {
super(cachesize, storagesize);
}
public void notifyRequest(int i, int[] indizes, int[] requests) {
// Nothing to do
}
public int replaceIndex(int frame, int[] indizes, int[] requests) {
//只要CACHE里面有空闲的位置,则调用下面的循环
for (int x = 0; x < cachesize; x++) {
if (indizes[x] < 0) {
return x;
}
}
// 记录被替换的INDEX
int next = index;
// 计算下次被体会的INDEX
index = (index + 1) % cachesize; //利用取模的方式来标志谁先进来
return next;
}
}
自由自在的虾仁 - 2007-6-29 13:24:00
路过不懂^^^^
很爱老婆 - 2007-10-19 17:21:00
不是很明白
很爱老婆 - 2007-10-19 17:22:00
不是很明白~
laura28 - 2007-10-19 17:38:00
晕死了,不懂啊
© 2000 - 2026 Rising Corp. Ltd.