目录
- 题目要求
- 具体代码
题目要求
采用多道程序思想设计一个程序,模拟页存储管理地址变换的过程,可采用FIFO、LRU、LFU、OPT四页面置换算法。基本要求如下:
- 需要建立访问页表线程、访问快表线程、缺页中断处理线程、访问内存线程等,协同这些线程模拟完成地址变换的过程。
- 输入一个逻辑页面访问序列和随机产生逻辑页面访问序列,分别由四个算法完成页面置换;
- 能够设定驻留内存页面的个数、内存的存取时间、缺页中断的时间、快表的时间,并提供合理省缺值,可以暂停和继续系统的执行;
- 能够设定页号序列中逻辑页面个数和范围;
- 能够设定有快表和没有快表的运行模式;
- 提供良好图形界面,同时能够展示四个算法运行的结果;
- 给出每种页面置换算法每个页面的存取时间;
- 能够将每次的实验输入和实验结果存储起来,随时可查询;
- 完成多次不同设置的实验,总结实验数据,看看能得出什么结论。
运行界面
执行界面
保存界面
历史界面
具体代码
四种页面置换算法具体实现
config
package com.lr.algurithm; | |
public class config { | |
public static int page_low =; | |
public static int page_high =; | |
public static int page_list_low =; | |
public static int page_list_high =; | |
public static int run_time =; | |
public static int quick_table_num =;//快表大小 | |
public static int memory_num =;//内存块数 | |
public static double memory_access =;//内存存取时间 | |
public static double quick_table_access =;//快表存取时间 | |
public static double miss_page_interrupt =;//缺页中断处理时间 | |
public static int taskSize =;//页面序列数量 | |
public static int[] task = new int[taskSize];//页面序列存放 | |
public static boolean is_check=false;//快表启用判断 | |
} |
FIFO算法
FIFO
package com.lr.algurithm; | |
import com.lr.frame.Page; | |
import sun.awt.Mutex; | |
import java.math.BigDecimal; | |
import java.math.RoundingMode; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class FIFO extends Thread{ | |
private int fifo_i =; | |
public int fifo_lackpage=;//缺页数 | |
public double fifo_ave_time=;//平均时间 | |
public double fifo_total_time=;//总时间 | |
public List<Integer> fifo_memory_page = new ArrayList<>();//内存现有页数存放 | |
public List<Integer> fifo_quick_table_page = new ArrayList<>();//快表现有页数存放 | |
public Mutex pause = new Mutex(); | |
public String snumber = new String(); | |
public String interruption = new String(); | |
public String totalTime = new String(); | |
public String avrTime = new String(); | |
public String missPage = new String(); | |
public double start(int a, double per_time) { | |
if (!config.is_check) { //无快表模式 | |
if (!fifo_memory_page.contains(a)) { //不在内存,产生缺页中断 | |
fifo_lackpage ++ ; | |
if (fifo_memory_page.size() < config.memory_num) | |
fifo_memory_page.add(a); | |
else { | |
fifo_memory_page.remove(); | |
fifo_memory_page.add(a); | |
} | |
fifo_total_time += * config.memory_access + config.miss_page_interrupt; | |
per_time += * config.memory_access + config.miss_page_interrupt; | |
} else { | |
fifo_total_time += * config.memory_access; | |
per_time += * config.memory_access; | |
} | |
} | |
else { //快表模式 | |
if (!fifo_quick_table_page.contains(a)) { //不在快表 | |
if (!fifo_memory_page.contains(a)) { //不在内存,产生缺页中断 | |
fifo_lackpage ++ ; | |
if (fifo_memory_page.size() < config.memory_num) //内存块未满,直接调入 | |
fifo_memory_page.add(a); | |
else { | |
fifo_memory_page.remove(); | |
fifo_memory_page.add(a); | |
} | |
//快表置换用FIFO | |
if (fifo_quick_table_page.size() < config.quick_table_num) | |
fifo_quick_table_page.add(a); | |
else { | |
fifo_quick_table_page.remove(); | |
fifo_quick_table_page.add(a); | |
} | |
fifo_total_time += * config.memory_access + config.miss_page_interrupt; | |
per_time += * config.memory_access + config.miss_page_interrupt; | |
} else { //在内存 | |
fifo_total_time += * config.memory_access; | |
per_time += * config.memory_access; | |
} | |
} else { //在快表 | |
fifo_total_time += config.quick_table_access + config.memory_access; | |
per_time += config.quick_table_access + config.memory_access;; | |
} | |
} | |
return per_time; | |
} | |
public void run() { | |
super.run(); | |
for(fifo_i =; fifo_i < config.taskSize; fifo_i ++ ) { | |
pause.lock(); | |
double per_time =; | |
per_time = start(config.task[fifo_i], per_time); | |
String str="查找"; | |
String s = String.valueOf(config.task[fifo_i]); | |
snumber += str; | |
snumber += s; | |
snumber += ":"; | |
for (int j =; j < config.memory_num; j ++ ) { | |
if (j < fifo_memory_page.size()) { | |
String s = String.valueOf(fifo_memory_page.get(j)); | |
snumber += s; | |
snumber += " "; | |
} | |
else { | |
snumber += " "; | |
} | |
} | |
pause.unlock(); | |
BigDecimal bd = new BigDecimal(per_time); | |
bd = bd.setScale(, RoundingMode.HALF_UP); | |
snumber += "耗费时间:" + bd + "\n"; | |
Page.FIFOtest.append(snumber); | |
snumber = ""; | |
try { | |
sleep(config.run_time);//睡眠ms | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
String s = String.valueOf(config.taskSize); | |
s = String.valueOf(fifo_lackpage); | |
interruption = s + "次"; | |
// emit fifo_updata(s+"次","interrupt_label"); | |
double d = (double)fifo_lackpage / config.taskSize *; | |
BigDecimal bd = new BigDecimal(d); | |
bd = bd.setScale(, RoundingMode.HALF_UP); | |
totalTime = String.valueOf(fifo_total_time); | |
fifo_ave_time = fifo_total_time / config.taskSize; | |
BigDecimal bda = new BigDecimal(fifo_ave_time); | |
bda = bda.setScale(, RoundingMode.HALF_UP); | |
avrTime = String.valueOf(bda); | |
missPage = bd + "%"; | |
Page.fifo_interrupt_label.setText(interruption); | |
Page.fifo_total_time_label.setText(totalTime); | |
Page.fifo_ave_time_label.setText(avrTime); | |
Page.fifo_miss_page_label.setText(missPage); | |
// System.out.println("fifo:"+s); | |
// System.out.println("fifo:"+fifo_total_time); | |
// System.out.println("fifo:"+fifo_ave_time); | |
// System.out.println("fifo:"+d + "%"); | |
} | |
} |
LRU算法
LRU
package com.lr.algurithm; | |
import com.lr.frame.Page; | |
import sun.awt.Mutex; | |
import java.math.BigDecimal; | |
import java.math.RoundingMode; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class LRU extends Thread { | |
private int lru_i =; | |
public int lru_lackpage=;//缺页数 | |
public double lru_ave_time=;//平均时间 | |
public double lru_total_time=;//总时间 | |
public List<Integer> lru_memory_page = new ArrayList<>();//内存现有页数存放 | |
public List<Integer> lru_quick_table_page = new ArrayList<>();//快表现有页数存放 | |
public Mutex pause = new Mutex(); | |
public String snumber = new String(); | |
public String interruption = new String(); | |
public String totalTime = new String(); | |
public String avrTime = new String(); | |
public String missPage = new String(); | |
public double start(int a, double per_time) { | |
if(!config.is_check) { //无快表模式 | |
if(!lru_memory_page.contains(a)) { //不在内存,产生缺页中断 | |
lru_lackpage ++ ; | |
if(lru_memory_page.size() < config.memory_num) { | |
lru_memory_page.add(a); | |
} else { | |
lru_memory_page.remove(); | |
lru_memory_page.add(a); | |
} | |
lru_total_time += * config.memory_access + config.miss_page_interrupt; | |
per_time += * config.memory_access + config.miss_page_interrupt; | |
} | |
else { //在内存中则找出这个元素并删除,在末尾添加这个元素,保证最近访问的置于末尾 | |
// if (a < lru_memory_page.size()) | |
int temp =; | |
for (int i =; i < lru_memory_page.size(); ++ i) { | |
if (a == lru_memory_page.get(i)) | |
temp = i; | |
} | |
lru_memory_page.remove(temp); | |
// System.out.println(a); | |
lru_memory_page.add(a); | |
lru_total_time += * config.memory_access; | |
per_time += * config.memory_access; | |
} | |
} | |
else { //快表模式 | |
if(!lru_quick_table_page.contains(a)) { //不在快表 | |
if(!lru_memory_page.contains(a)) { //不在内存,产生缺页中断 | |
lru_lackpage ++ ; | |
if(lru_memory_page.size() < config.memory_num) { //内存块未满,直接调入 | |
lru_memory_page.add(a); | |
} else { | |
lru_memory_page.remove(); | |
lru_memory_page.add(a); | |
} | |
//快表置换用FIFO | |
if(lru_quick_table_page.size() < config.quick_table_num) { | |
lru_quick_table_page.add(a); | |
} else { | |
lru_quick_table_page.remove(); | |
lru_quick_table_page.add(a); | |
} | |
lru_total_time += * config.memory_access + config.miss_page_interrupt; | |
per_time += * config.memory_access + config.miss_page_interrupt; | |
} | |
else { //在内存 | |
int temp =; | |
for (int i =; i < lru_memory_page.size(); ++ i) { | |
if (a == lru_memory_page.get(i)) | |
temp = i; | |
} | |
lru_memory_page.remove(temp); | |
lru_memory_page.add(a); | |
lru_total_time += * config.memory_access; | |
per_time += * config.memory_access; | |
} | |
} else { //在快表 | |
int temp =; | |
for (int i =; i < lru_memory_page.size(); ++ i) { | |
if (a == lru_memory_page.get(i)) | |
temp = i; | |
} | |
lru_memory_page.remove(temp); | |
lru_memory_page.add(a); | |
lru_total_time += config.quick_table_access + config.memory_access; | |
per_time += config.quick_table_access + config.memory_access; | |
} | |
} | |
return per_time; | |
} | |
public void run() { | |
for (lru_i =; lru_i < config.taskSize; lru_i ++ ) { | |
pause.lock(); | |
double per_time =; | |
per_time = start(config.task[lru_i],per_time); | |
String str="查找"; | |
String s = String.valueOf(config.task[lru_i]); | |
snumber += str; | |
snumber += s; | |
snumber += ":"; | |
for (int j =; j < config.memory_num; j ++ ) { | |
if (j < lru_memory_page.size()) { | |
String s = String.valueOf(lru_memory_page.get(j)); | |
snumber+=s; | |
snumber+=" "; | |
} else { | |
snumber+=" "; | |
} | |
} | |
pause.unlock(); | |
BigDecimal bd = new BigDecimal(per_time); | |
bd = bd.setScale(, RoundingMode.HALF_UP); | |
snumber += "耗费时间:" + bd.toString() + "\n"; | |
Page.LRUtest.append(snumber); | |
snumber = ""; | |
try { | |
sleep(config.run_time); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
String s = String.valueOf(config.taskSize); | |
s = String.valueOf(lru_lackpage); | |
interruption = s + "次"; | |
double d = (double)lru_lackpage / config.taskSize *; | |
BigDecimal bd = new BigDecimal(d); | |
bd = bd.setScale(, RoundingMode.HALF_UP); | |
lru_ave_time = (double)lru_total_time / config.taskSize; | |
totalTime = String.valueOf(lru_total_time); | |
BigDecimal bda = new BigDecimal(lru_ave_time); | |
bda = bda.setScale(, RoundingMode.HALF_UP); | |
avrTime = String.valueOf(bda); | |
missPage = bd + "%"; | |
Page.lru_interrupt_label.setText(interruption); | |
Page.lru_total_time_label.setText(totalTime); | |
Page.lru_ave_time_label.setText(avrTime); | |
Page.lru_miss_page_label.setText(missPage); | |
// System.out.println("lru"+s); | |
// System.out.println("lru"+lru_total_time); | |
// System.out.println("lru"+lru_ave_time); | |
// System.out.println("lru"+d + "%"); | |
} | |
} |
LFU算法
LFU
package com.lr.algurithm; | |
import com.lr.frame.Page; | |
import com.lr.tool.Node; | |
import sun.awt.Mutex; | |
import java.math.BigDecimal; | |
import java.math.RoundingMode; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class LFU extends Thread { | |
private int lfu_i =; | |
public int lfu_lackpage=;//缺页数 | |
public double lfu_ave_time=;//平均时间 | |
public double lfu_total_time=;//总时间 | |
public List<Node> lfu_memory_page = new ArrayList<>();//内存现有页数存放 | |
public List<Integer> lfu_quick_table_page = new ArrayList<>();//快表现有页数存放 | |
public Mutex pause = new Mutex(); | |
public String snumber = new String(); | |
public String interruption = new String(); | |
public String totalTime = new String(); | |
public String avrTime = new String(); | |
public String missPage = new String(); | |
public double start(int a, double per_time) { | |
if(!config.is_check) { //无快表模式 | |
boolean flag = true; | |
int index =; | |
for (Node map:lfu_memory_page) { | |
if(map.key == a) { | |
flag = false; | |
break; | |
} | |
index ++ ; | |
} | |
if (flag) { //不在内存 | |
lfu_lackpage ++ ; | |
//内存满,删去访问频率最小的那个,相等的频率按fifo删除 | |
if(lfu_memory_page.size() >= config.memory_num) { | |
Node min_obj; | |
min_obj = lfu_memory_page.get(); | |
for (Node pair : lfu_memory_page) { | |
if (pair.value < min_obj.value) { | |
min_obj = pair; | |
} | |
} | |
lfu_memory_page.remove(min_obj); | |
} | |
lfu_memory_page.add(new Node(a)); | |
lfu_total_time += * config.memory_access + config.miss_page_interrupt; | |
per_time += * config.memory_access + config.miss_page_interrupt; | |
} else { | |
lfu_memory_page.get(index).value++; | |
lfu_total_time += * config.memory_access; | |
per_time += * config.memory_access; | |
} | |
} else { //快表模式 | |
if(!lfu_quick_table_page.contains(a))//不在快表 | |
{ | |
boolean flag = true; | |
int index =; | |
for (Node map : lfu_memory_page) { | |
if (map.key == a) { | |
flag = false; | |
break; | |
} | |
index ++ ; | |
} | |
if (flag) { //不在内存 | |
lfu_lackpage ++ ; | |
//内存满,删去访问频率最小的那个,相等的频率按lfu删除 | |
if (lfu_memory_page.size() >= config.memory_num) { | |
Node min_obj; | |
min_obj = lfu_memory_page.get(); | |
for (Node map : lfu_memory_page) { | |
if(map.value < min_obj.value) { | |
min_obj = map; | |
} | |
} | |
lfu_memory_page.remove(min_obj); | |
} | |
lfu_memory_page.add(new Node(a)); | |
//快表置换用FIFO | |
if (lfu_quick_table_page.size() < config.quick_table_num) { | |
lfu_quick_table_page.add(a); | |
} else { | |
lfu_quick_table_page.remove(); | |
lfu_quick_table_page.add(a); | |
} | |
lfu_total_time += * config.memory_access + config.miss_page_interrupt; | |
per_time += * config.memory_access + config.miss_page_interrupt; | |
} else { //在内存 | |
lfu_memory_page.get(index).value++; | |
lfu_total_time += * config.memory_access; | |
per_time += * config.memory_access; | |
} | |
} else { //在快表 | |
int index =; | |
for (Node map : lfu_memory_page) { | |
if(map.key == a) { | |
lfu_memory_page.get(index).value++; | |
break; | |
} | |
index++; | |
} | |
lfu_total_time += config.quick_table_access + config.memory_access; | |
per_time += config.quick_table_access + config.memory_access; | |
} | |
} | |
return per_time; | |
} | |
public void run() { | |
for(lfu_i =; lfu_i < config.taskSize; lfu_i ++ ) { | |
pause.lock(); | |
double per_time =; | |
per_time = start(config.task[lfu_i], per_time); | |
String str = "查找"; | |
String s = String.valueOf(config.task[lfu_i]); | |
snumber += str; | |
snumber += s; | |
snumber += ":"; | |
for (int j =; j < config.memory_num; j ++) { | |
if (j < lfu_memory_page.size()) { | |
String s = String.valueOf(lfu_memory_page.get(j).key); | |
snumber+=s; | |
snumber+=" "; | |
} else { | |
snumber+=" "; | |
} | |
} | |
pause.unlock(); | |
BigDecimal bd = new BigDecimal(per_time); | |
bd = bd.setScale(, RoundingMode.HALF_UP); | |
snumber += "耗费时间:" + bd + "\n"; | |
Page.LFUtest.append(snumber); | |
snumber = ""; | |
try { | |
sleep(config.run_time); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
String s = String.valueOf(config.taskSize); | |
s = String.valueOf(lfu_lackpage); | |
interruption = s + "次"; | |
double d = (double)lfu_lackpage / config.taskSize *; | |
BigDecimal bd = new BigDecimal(d); | |
bd = bd.setScale(, RoundingMode.HALF_UP); | |
lfu_ave_time = (double)lfu_total_time / config.taskSize; | |
totalTime = String.valueOf(lfu_total_time); | |
BigDecimal bda = new BigDecimal(lfu_ave_time); | |
bda = bda.setScale(, RoundingMode.HALF_UP); | |
avrTime = String.valueOf(bda); | |
missPage = bd + "%"; | |
Page.lfu_interrupt_label.setText(interruption); | |
Page.lfu_total_time_label.setText(totalTime); | |
Page.lfu_ave_time_label.setText(avrTime); | |
Page.lfu_miss_page_label.setText(missPage); | |
// System.out.println("lfu"+s); | |
// System.out.println("lfu"+lfu_total_time); | |
// System.out.println("lfu"+lfu_ave_time); | |
// System.out.println("lfu"+d + "%"); | |
} | |
} |
Node
package com.lr.tool; | |
public class Node { | |
public int key; | |
public int value; | |
public Node() { | |
key =; | |
value =; | |
} | |
public Node(int Key) { | |
key = Key; | |
value =; | |
} | |
public Node(int a, int i) { | |
} | |
} |
OPT算法
OPT
package com.lr.algurithm; | |
import com.lr.frame.Page; | |
import sun.awt.Mutex; | |
import java.math.BigDecimal; | |
import java.math.RoundingMode; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class OPT extends Thread { | |
private int opt_i =; | |
public int opt_lackpage=;//缺页数 | |
public double opt_ave_time=;//平均时间 | |
public double opt_total_time=;//总时间 | |
public List<Integer> opt_memory_page = new ArrayList<>();//内存现有页数存放 | |
// public int[] opt_memory_page = new int[config.taskSize]; | |
public List<Integer> opt_quick_table_page = new ArrayList<>();//快表现有页数存放 | |
public Mutex pause = new Mutex(); | |
public String snumber = new String(); | |
public String interruption = new String(); | |
public String totalTime = new String(); | |
public String avrTime = new String(); | |
public String missPage = new String(); | |
public double start(int a, double per_time) { | |
if (!config.is_check) { //无快表模式 | |
if (!opt_memory_page.contains(a)) { //不在内存,产生缺页中断 | |
opt_lackpage ++ ; | |
if (opt_memory_page.size() < config.memory_num) { | |
opt_memory_page.add(a); | |
} else { //遍历之后所有页号,与内存内页号相同则移到尾部,最后删除头部 | |
Map<Integer, Boolean> has_move = new HashMap<>();//移动过的不能再移动 | |
int cnt =;//移动次数要小于内存块数 | |
for (int j =; j < config.taskSize; j ++ ) { | |
has_move.put(config.task[j], false); | |
} | |
for (int j = opt_i +; j < config.taskSize; j ++ ) { //从需要进入内存的页号的后一位开始 | |
if(opt_memory_page.contains(config.task[j]) | |
&& has_move.get(config.task[j]) != true | |
&& cnt < config.memory_num -) | |
{ | |
int temp =; | |
for (int i =; i < opt_memory_page.size(); ++ i) { | |
if (config.task[j] == opt_memory_page.get(i)) | |
temp = i; | |
} | |
opt_memory_page.remove(temp); | |
opt_memory_page.add(config.task[j]); | |
has_move.put(config.task[j], true); | |
cnt ++ ; | |
} | |
} | |
opt_memory_page.remove(); | |
opt_memory_page.add(a); | |
} | |
opt_total_time += * config.memory_access + config.miss_page_interrupt; | |
per_time += * config.memory_access + config.miss_page_interrupt; | |
} else { | |
opt_total_time += * config.memory_access; | |
per_time += * config.memory_access; | |
} | |
} else { //快表模式 | |
if(!opt_quick_table_page.contains(a)) { //不在快表 | |
if(!opt_memory_page.contains(a)) { //不在内存,产生缺页中断 | |
opt_lackpage ++ ; | |
if(opt_memory_page.size() < config.memory_num) { //内存块未满,直接调入 | |
opt_memory_page.add(a); | |
} else { | |
Map<Integer, Boolean> has_move = new HashMap<>();//移动过的不能再移动 | |
int cnt =;//移动次数要小于内存块数 | |
for (int j =; j < config.taskSize; j ++ ) { | |
has_move.put(config.task[j], false); | |
} | |
for(int j = opt_i +; j < config.taskSize; j ++ ) { //从需要进入内存的页号的后一位开始 | |
if(opt_memory_page.contains(config.task[j]) | |
&& has_move.get(config.task[j]) != true | |
&& cnt < config.memory_num -) { | |
int temp =; | |
for (int i =; i < opt_memory_page.size(); ++ i) { | |
if (config.task[j] == opt_memory_page.get(i)) | |
temp = i; | |
} | |
opt_memory_page.remove(temp); | |
opt_memory_page.add(config.task[j]); | |
has_move.put(config.task[j], true); | |
cnt++; | |
} | |
} | |
opt_memory_page.remove(); | |
opt_memory_page.add(a); | |
} | |
//快表置换用FIFO | |
if(opt_quick_table_page.size() < config.quick_table_num) { | |
opt_quick_table_page.add(a); | |
} else { | |
opt_quick_table_page.remove(); | |
opt_quick_table_page.add(a); | |
} | |
opt_total_time += * config.memory_access + config.miss_page_interrupt; | |
per_time += * config.memory_access + config.miss_page_interrupt; | |
} else { //在内存 | |
opt_total_time += * config.memory_access; | |
per_time += * config.memory_access; | |
} | |
} else { //在快表 | |
opt_total_time += config.quick_table_access + config.memory_access; | |
per_time += config.quick_table_access + config.memory_access; | |
} | |
} | |
return per_time; | |
} | |
public void run() { | |
for (opt_i =; opt_i < config.taskSize; opt_i ++ ) { | |
pause.lock(); | |
double per_time =; | |
per_time = start(config.task[opt_i], per_time); | |
String str="查找"; | |
String s = String.valueOf(config.task[opt_i]); | |
snumber += str; | |
snumber += s; | |
snumber += ":"; | |
for (int j =; j < config.memory_num; j ++ ) { | |
if (j < opt_memory_page.size()) { | |
String s = String.valueOf(opt_memory_page.get(j)); | |
snumber += s; | |
snumber += " "; | |
} else { | |
snumber+=" "; | |
} | |
} | |
pause.unlock(); | |
BigDecimal bd = new BigDecimal(per_time); | |
bd = bd.setScale(, RoundingMode.HALF_UP); | |
snumber += "耗费时间:" + bd + "\n"; | |
Page.OPTtest.append(snumber); | |
snumber = ""; | |
try { | |
sleep(config.run_time); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
String s = String.valueOf(config.taskSize); | |
s = String.valueOf(opt_lackpage); | |
interruption = s + "次"; | |
double d = (double)opt_lackpage / config.taskSize *; | |
BigDecimal bd = new BigDecimal(d); | |
bd = bd.setScale(, RoundingMode.HALF_UP); | |
opt_ave_time = (double)opt_total_time / config.taskSize; | |
totalTime = String.valueOf(opt_total_time); | |
BigDecimal bda = new BigDecimal(opt_ave_time); | |
bda = bda.setScale(, RoundingMode.HALF_UP); | |
avrTime = String.valueOf(bda); | |
missPage = bd + "%"; | |
Page.opt_interrupt_label.setText(interruption); | |
Page.opt_total_time_label.setText(totalTime); | |
Page.opt_ave_time_label.setText(avrTime); | |
Page.opt_miss_page_label.setText(missPage); | |
// System.out.println("opt"+s); | |
// System.out.println("opt"+opt_total_time); | |
// System.out.println("opt"+opt_ave_time); | |
// System.out.println("opt"+d + "%"); | |
} | |
} |
连接数据库
保存记录
Save
package com.lr.db; | |
import java.sql.*; | |
import com.lr.frame.Page; | |
import com.lr.algurithm.config; | |
import javax.swing.*; | |
public class Save { | |
private static String url = null; | |
private static String username = null; | |
private static String password = null; | |
private static Connection conn = null; | |
private static Statement st = null; | |
public Save() { | |
try { | |
saveRecord(); | |
} catch (SQLException throwables) { | |
throwables.printStackTrace(); | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} finally { | |
if (st != null) { | |
try { | |
st.close(); | |
} catch (SQLException throwables) { | |
throwables.printStackTrace(); | |
} | |
} | |
if (conn != null) { | |
try { | |
conn.close(); | |
} catch (SQLException throwables) { | |
throwables.printStackTrace(); | |
} | |
} | |
} | |
} | |
public void saveRecord() throws SQLException, ClassNotFoundException { | |
//.加载驱动 | |
//DriverManager.registerDriver(new com.mysql.jdbc.Driver()); | |
Class.forName("com.mysql.jdbc.Driver");//固定写法 | |
//.用户信息和 | |
url = "jdbc:mysql://.0.0.1:3306/os?useUnicode=true&characterEncoding=utf8&useSSL=true"; | |
username = "root"; | |
password = ""; | |
//.连接成功,数据库对象 Connection 代表数据库 | |
conn = DriverManager.getConnection(url, username, password); | |
//.获得SQL的执行对象 | |
st = conn.createStatement(); | |
String sql = "INSERT INTO test(memory_n,page_n,page_list,memory_t,quick_table_t," + | |
"interrupt_t,is_check,fifo_total_t,fifo_ave_t,fifo_miss_page,fifo_interrupt_n," + | |
"fifo_text,lru_total_t,lru_ave_t,lru_miss_page,lru_interrupt_n,lru_text," + | |
"lfu_total_t,lfu_ave_t,lfu_miss_page,lfu_interrupt_n,lfu_text,opt_total_t," + | |
"opt_ave_t,opt_miss_page,opt_interrupt_n,opt_text)VALUES('" + config.memory_num + "','" + | |
config.taskSize + "','" + Page.task_edit.getText() + "','" + config.memory_access + "','" + | |
config.quick_table_access + "','" + config.miss_page_interrupt + "','" + | |
config.is_check + "','" + Page.fifo_total_time_label.getText() + "','" + | |
Page.fifo_ave_time_label.getText() + "','" + Page.fifo_miss_page_label.getText() + "','" + | |
Page.fifo_interrupt_label.getText() + "','" + Page.FIFOtest.getText() + "','" + | |
Page.lru_total_time_label.getText() + "','" + Page.lru_ave_time_label.getText() + "','" + | |
Page.lru_miss_page_label.getText() + "','" + Page.lru_interrupt_label.getText() + "','" + | |
Page.LRUtest.getText() + "','" + Page.lfu_total_time_label.getText() + "','" + | |
Page.lfu_ave_time_label.getText() + "','" + Page.lfu_miss_page_label.getText() + "','" + | |
Page.lfu_interrupt_label.getText() + "','" + Page.LFUtest.getText() + "','" + | |
Page.opt_total_time_label.getText() + "','" + Page.opt_ave_time_label.getText() + "','" + | |
Page.opt_miss_page_label.getText() + "','" + Page.opt_interrupt_label.getText() + "','" + | |
Page.OPTtest.getText() + "')"; | |
int i = st.executeUpdate(sql); | |
if (i >) { | |
JOptionPane.showMessageDialog(null, "保存成功", "提交信息",); | |
} else { | |
JOptionPane.showMessageDialog(null, "保存失败", "warning", JOptionPane.ERROR_MESSAGE); | |
} | |
} | |
public static void main(String[] args) { | |
new Save(); | |
} | |
} |
历史记录
History
package com.lr.db; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.sql.*; | |
public class History { | |
private static String url=null; | |
private static String username=null; | |
private static String password=null; | |
private JFrame frame; | |
public History() { | |
frame = new JFrame("历史记录"); | |
// frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 退出应用程序默认窗口关闭 | |
// //获取与系统相关的默认工具类对象 | |
Toolkit toolkit = Toolkit.getDefaultToolkit(); | |
//获取屏幕分辨率 | |
Dimension d = toolkit.getScreenSize(); | |
frame.setBounds((int)(d.getWidth() -) / 2,(int)(d.getHeight() - 850) / 2, | |
, 850); | |
frame.setVisible(true); | |
try { | |
historyRecord(); | |
} catch (ClassNotFoundException cnfe) { | |
cnfe.printStackTrace(); | |
JOptionPane.showMessageDialog(null, "数据源错误", "错误", JOptionPane.ERROR_MESSAGE); | |
} catch (SQLException sqle) { | |
sqle.printStackTrace(); | |
JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE); | |
} | |
} | |
public void historyRecord() throws SQLException, ClassNotFoundException { | |
//.加载驱动 | |
//DriverManager.registerDriver(new com.mysql.jdbc.Driver()); | |
Class.forName("com.mysql.jdbc.Driver");//固定写法 | |
//.用户信息和 | |
url="jdbc:mysql://.0.0.1:3306/os?useUnicode=true&characterEncoding=utf8&useSSL=true"; | |
username="root"; | |
password=""; | |
//.连接成功,数据库对象 Connection 代表数据库 | |
Connection conn=DriverManager.getConnection(url,username,password); | |
//.执行SQL的对象,去执行SQL,可能存在结果,返回结果信息 | |
String sql = "select * from test"; | |
PreparedStatement pstm = conn.prepareStatement(sql); | |
ResultSet rs = pstm.executeQuery(); | |
// ResultSet rs=statement.executeQuery(sql); | |
int count =; | |
while (rs.next()) { | |
count++; | |
} | |
rs = pstm.executeQuery(); | |
// 将查询获得的记录数据,转换成适合生成JTable的数据形式 | |
Object[][] info = new Object[count][]; | |
Object[] title = {"内存块数", "页面大小", " 页号序列", "内存存取时间", "快表存取时间", | |
"中断执行时间", "是否勾选快表", "FIFO总时间", "FIFO平均时间", "FIFO缺页率", | |
"FIFO中断次数", "FIFO执行过程", "LRU总时间", "LRU平均时间", "LRU缺页率", | |
"LRU中断次数", "LRU执行过程", "LFU总时间", "LFU平均时间", "LFU缺页率", | |
"LFU中断次数", "LFU执行过程", "OPT总时间", "OPT平均时间", "OPT缺页率", | |
"OPT中断次数", "OPT执行过程"}; | |
count =; | |
while (rs.next()) { | |
// info[count][] = rs.getObject("id"); | |
info[count][] = rs.getObject("memory_n"); | |
info[count][] = rs.getObject("page_n"); | |
info[count][] = rs.getObject("page_list"); | |
info[count][] = rs.getObject("memory_t"); | |
info[count][] = rs.getObject("quick_table_t"); | |
info[count][] = rs.getObject("interrupt_t"); | |
info[count][] = rs.getObject("is_check"); | |
info[count][] = rs.getObject("fifo_total_t"); | |
info[count][] = rs.getObject("fifo_ave_t"); | |
info[count][] = rs.getObject("fifo_miss_page"); | |
info[count][] = rs.getObject("fifo_interrupt_n"); | |
info[count][] = rs.getObject("fifo_text"); | |
info[count][] = rs.getObject("lru_total_t"); | |
info[count][] = rs.getObject("lru_ave_t"); | |
info[count][] = rs.getObject("lru_miss_page"); | |
info[count][] = rs.getObject("lru_interrupt_n"); | |
info[count][] = rs.getObject("lru_text"); | |
info[count][] = rs.getObject("lfu_total_t"); | |
info[count][] = rs.getObject("lfu_ave_t"); | |
info[count][] = rs.getObject("lfu_miss_page"); | |
info[count][] = rs.getObject("lfu_interrupt_n"); | |
info[count][] = rs.getObject("lfu_text"); | |
info[count][] = rs.getObject("opt_total_t"); | |
info[count][] = rs.getObject("opt_ave_t"); | |
info[count][] = rs.getObject("opt_miss_page"); | |
info[count][] = rs.getObject("opt_interrupt_n"); | |
info[count][] = rs.getObject("opt_text"); | |
count++; | |
} | |
JTable table = new JTable(info, title); | |
// 设置表格内容颜色 | |
table.setForeground(Color.BLACK); // 字体颜色 | |
table.setFont(new Font(null, Font.PLAIN,)); // 字体样式 | |
table.setSelectionForeground(Color.DARK_GRAY); // 选中后字体颜色 | |
table.setSelectionBackground(Color.LIGHT_GRAY); // 选中后字体背景 | |
table.setGridColor(Color.GRAY); // 网格颜色 | |
// 设置表头 | |
table.getTableHeader().setFont(new Font(null, Font.BOLD,)); // 设置表头名称字体样式 | |
table.getTableHeader().setForeground(Color.RED); // 设置表头名称字体颜色 | |
table.getTableHeader().setReorderingAllowed(false); // 设置不允许拖动重新排序各列 | |
// 设置行高 | |
table.setRowHeight(); | |
// 第一列列宽设置为 | |
table.getColumnModel().getColumn().setPreferredWidth(40); | |
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); | |
JScrollPane scroll = new JScrollPane(table); | |
frame.add(scroll); | |
frame.setVisible(true); | |
frame.pack(); | |
} | |
public static void main(String[] args) { | |
new History(); | |
} | |
} |
GUI界面
Page
package com.lr.frame; | |
import com.lr.algurithm.FIFO; | |
import com.lr.algurithm.LFU; | |
import com.lr.algurithm.LRU; | |
import com.lr.algurithm.OPT; | |
import com.lr.algurithm.config; | |
import com.lr.db.History; | |
import com.lr.db.Save; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.util.Arrays; | |
import java.util.List; | |
public class Page { | |
public JFrame frame; | |
private JPanel panel; | |
public static TextArea FIFOtest; | |
public static TextArea LRUtest; | |
public static TextArea LFUtest; | |
public static TextArea OPTtest; | |
public static JTextField task_edit; | |
public static JTextField memory_num_edit; | |
public static JTextField taskSize_edit; | |
public static JTextField taskSize_low_edit; | |
public static JTextField taskSize_high_edit; | |
public static JTextField task_low_edit; | |
public static JTextField task_high_edit; | |
public static JCheckBox quick_table_check; | |
public static JTextField memory_access_edit; | |
public static JTextField quick_table_access_edit; | |
public static JTextField miss_page_interrupt_edit; | |
public static JTextField run_time_edit; | |
public static JLabel fifo_interrupt_label; | |
public static JLabel fifo_miss_page_label; | |
public static JLabel fifo_total_time_label; | |
public static JLabel fifo_ave_time_label; | |
public static JLabel lru_interrupt_label; | |
public static JLabel lru_miss_page_label; | |
public static JLabel lru_total_time_label; | |
public static JLabel lru_ave_time_label; | |
public static JLabel lfu_interrupt_label; | |
public static JLabel lfu_miss_page_label; | |
public static JLabel lfu_total_time_label; | |
public static JLabel lfu_ave_time_label; | |
public static JLabel opt_interrupt_label; | |
public static JLabel opt_miss_page_label; | |
public static JLabel opt_total_time_label; | |
public static JLabel opt_ave_time_label; | |
public static JTextField quick_table_num_edit; | |
FIFO fifo = new FIFO(); | |
LRU lru = new LRU(); | |
LFU lfu = new LFU(); | |
OPT opt = new OPT(); | |
public Page() { | |
frame = new JFrame(); | |
frame.setTitle("页面置换算法"); | |
//获取与系统相关的默认工具类对象 | |
Toolkit toolkit = Toolkit.getDefaultToolkit(); | |
//获取屏幕分辨率 | |
Dimension d = toolkit.getScreenSize(); | |
frame.setBounds((int)(d.getWidth() -) / 2,(int)(d.getHeight() - 895) / 2, | |
, 895); | |
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 退出应用程序默认窗口关闭 | |
frame.getContentPane().setLayout(null); | |
// frame.setVisible(true); | |
panel = new JPanel(); | |
panel.setBounds(, 10, 1165, 890); | |
frame.getContentPane().add(panel); | |
panel.setLayout(null); | |
JLabel label_ = new JLabel("内存块数:"); | |
label_.setBounds(35, 20, 100, 20); | |
label_.setFont(new Font("宋体", Font.PLAIN, 20)); | |
// frame.getContentPane().add(label_); | |
panel.add(label_); | |
memory_num_edit = new JTextField(); | |
memory_num_edit.setBounds(, 20, 180, 20); | |
// frame.getContentPane().add(memory_num_edit); | |
panel.add(memory_num_edit); | |
// memory_num_edit.setColumns(); | |
JLabel label_ = new JLabel("页面数量:"); | |
label_.setBounds(35, 60, 100, 20); | |
label_.setFont(new Font("宋体", Font.PLAIN, 20)); | |
// frame.getContentPane().add(label_); | |
panel.add(label_); | |
taskSize_edit = new JTextField(); | |
taskSize_edit.setBounds(, 60, 180, 20); | |
// frame.getContentPane().add(taskSize_edit); | |
panel.add(taskSize_edit); | |
taskSize_low_edit = new JTextField(); | |
taskSize_low_edit.setBounds(, 60, 40, 20); | |
// frame.getContentPane().add(taskSize_low_edit); | |
panel.add(taskSize_low_edit); | |
JLabel label_ = new JLabel("-"); | |
label_.setBounds(525, 60, 10, 20); | |
label_.setFont(new Font("宋体", Font.PLAIN, 20)); | |
// frame.getContentPane().add(label_); | |
panel.add(label_); | |
taskSize_high_edit = new JTextField(); | |
taskSize_high_edit.setBounds(, 60, 40, 20); | |
// frame.getContentPane().add(taskSize_high_edit); | |
panel.add(taskSize_high_edit); | |
JButton taskSize_btn = new JButton("随机生成"); | |
taskSize_btn.setBounds(, 55, 120, 30); | |
taskSize_btn.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(taskSize_btn); | |
panel.add(taskSize_btn); | |
taskSize_btn.addActionListener(e -> { | |
if (!taskSize_low_edit.getText().isEmpty()) { | |
config.page_low = Integer.parseInt(taskSize_low_edit.getText()); | |
} else { | |
config.page_low =; | |
} | |
if (!taskSize_high_edit.getText().isEmpty()) | |
config.page_high = Integer.parseInt(taskSize_high_edit.getText()); | |
else | |
config.page_high =; | |
// int r = (int) Math.random(); | |
config.taskSize = (int) (config.page_low + Math.random() * (config.page_high - config.page_low +)); | |
taskSize_edit.setText(String.valueOf(config.taskSize)); | |
}); | |
JLabel label_ = new JLabel("页面序号:"); | |
label_.setBounds(35, 100, 100, 20); | |
label_.setFont(new Font("宋体", Font.PLAIN, 20)); | |
// frame.getContentPane().add(label_); | |
panel.add(label_); | |
task_low_edit = new JTextField(); | |
task_low_edit.setBounds(, 100, 40, 20); | |
// frame.getContentPane().add(task_low_edit); | |
panel.add(task_low_edit); | |
JLabel label_ = new JLabel("-"); | |
label_.setBounds(525, 100, 10, 20); | |
label_.setFont(new Font("宋体", Font.PLAIN, 20)); | |
// frame.getContentPane().add(label_); | |
panel.add(label_); | |
task_high_edit = new JTextField(); | |
task_high_edit.setBounds(, 100, 40, 20); | |
// frame.getContentPane().add(task_high_edit); | |
panel.add(task_high_edit); | |
task_edit = new JTextField(); | |
task_edit.setBounds(, 100, 180, 20); | |
// frame.getContentPane().add(task_edit); | |
panel.add(task_edit); | |
JButton task_btn = new JButton("随机生成"); | |
task_btn.setBounds(, 95, 120, 30); | |
task_btn.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(task_btn); | |
panel.add(task_btn); | |
task_btn.addActionListener(e -> { | |
if (!task_low_edit.getText().isEmpty()) { | |
config.page_list_low = Integer.parseInt(task_low_edit.getText()); | |
} else { | |
config.page_list_low =; | |
} | |
if (!task_high_edit.getText().isEmpty()) | |
config.page_list_high = Integer.parseInt(task_high_edit.getText()); | |
else | |
config.page_list_high =; | |
config.taskSize = Integer.parseInt(taskSize_edit.getText()); | |
String list = ""; | |
config.task = new int[config.taskSize]; | |
int i =; | |
for (; i < config.taskSize -; i ++ ) { | |
config.task[i] = (int) (config.page_list_low + Math.random() * (config.page_list_high - config.page_list_low +)); | |
list += config.task[i] + ","; | |
} | |
config.task[i] = (int) (config.page_list_low + Math.random() * (config.page_list_high - config.page_list_low +)); | |
list += config.task[i]; | |
task_edit.setText(list); | |
}); | |
JLabel label_ = new JLabel("快表大小:"); | |
label_.setBounds(35, 145, 100, 20); | |
label_.setFont(new Font("宋体", Font.PLAIN, 20)); | |
// frame.getContentPane().add(label_); | |
panel.add(label_); | |
quick_table_num_edit = new JTextField(); | |
quick_table_num_edit.setBounds(, 145, 180, 20); | |
// frame.getContentPane().add(quick_table_num_edit); | |
panel.add(quick_table_num_edit); | |
quick_table_num_edit.addActionListener(e -> { | |
config.quick_table_num = Integer.parseInt(quick_table_num_edit.getText()); | |
}); | |
quick_table_num_edit.setEnabled(false); | |
quick_table_check = new JCheckBox("启用快表"); | |
quick_table_check.setBounds(, 145, 130, 30); | |
quick_table_check.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(quick_table_check); | |
panel.add(quick_table_check); | |
quick_table_check.addActionListener(e -> { | |
on_quick_table_check_clicked(quick_table_check.isSelected()); | |
}); | |
JLabel label_ = new JLabel("内存存取时间:"); | |
label_.setBounds(700, 20, 140, 20); | |
label_.setFont(new Font("宋体", Font.PLAIN, 20)); | |
// frame.getContentPane().add(label_); | |
panel.add(label_); | |
memory_access_edit = new JTextField(); | |
memory_access_edit.setBounds(, 20, 50, 20); | |
// frame.getContentPane().add(memory_access_edit); | |
panel.add(memory_access_edit); | |
JLabel label_ = new JLabel("ms"); | |
label_.setBounds(900, 20, 20, 20); | |
label_.setFont(new Font("宋体", Font.PLAIN, 20)); | |
// frame.getContentPane().add(label_); | |
panel.add(label_); | |
JLabel label_ = new JLabel("快表存取时间:"); | |
label_.setBounds(700, 60, 140, 20); | |
label_.setFont(new Font("宋体", Font.PLAIN, 20)); | |
// frame.getContentPane().add(label_); | |
panel.add(label_); | |
quick_table_access_edit = new JTextField(); | |
quick_table_access_edit.setBounds(, 60, 50, 20); | |
// frame.getContentPane().add(quick_table_access_edit); | |
panel.add(quick_table_access_edit); | |
JLabel label_ = new JLabel("ms"); | |
label_.setBounds(900, 60, 20, 20); | |
label_.setFont(new Font("宋体", Font.PLAIN, 20)); | |
// frame.getContentPane().add(label_); | |
panel.add(label_); | |
JLabel label_ = new JLabel("缺页中断时间:"); | |
label_.setBounds(700, 100, 140, 20); | |
label_.setFont(new Font("宋体", Font.PLAIN, 20)); | |
// frame.getContentPane().add(label_); | |
panel.add(label_); | |
miss_page_interrupt_edit = new JTextField(); | |
miss_page_interrupt_edit.setBounds(, 100, 50, 20); | |
// frame.getContentPane().add(miss_page_interrupt_edit); | |
panel.add(miss_page_interrupt_edit); | |
JLabel label_ = new JLabel("ms"); | |
label_.setBounds(900, 100, 20, 20); | |
label_.setFont(new Font("宋体", Font.PLAIN, 20)); | |
// frame.getContentPane().add(label_); | |
panel.add(label_); | |
JLabel label_ = new JLabel("执行时间控制:"); | |
label_.setBounds(700, 140, 140, 20); | |
label_.setFont(new Font("宋体", Font.PLAIN, 20)); | |
// frame.getContentPane().add(label_); | |
panel.add(label_); | |
run_time_edit = new JTextField(); | |
run_time_edit.setBounds(, 140, 50, 20); | |
// frame.getContentPane().add(run_time_edit); | |
panel.add(run_time_edit); | |
JLabel label_ = new JLabel("ms"); | |
label_.setBounds(900, 140, 20, 20); | |
label_.setFont(new Font("宋体", Font.PLAIN, 20)); | |
// frame.getContentPane().add(label_); | |
panel.add(label_); | |
JButton start_btn = new JButton("开始执行"); | |
start_btn.setBounds(, 185, 120, 30); | |
start_btn.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(start_btn); | |
panel.add(start_btn); | |
start_btn.addActionListener(e -> { | |
on_clear_btn_clicked(); | |
config.memory_num = Integer.parseInt(memory_num_edit.getText()); | |
if (quick_table_check.isSelected()) { | |
config.quick_table_num = Integer.parseInt(quick_table_num_edit.getText()); | |
if (quick_table_check.isSelected() && config.quick_table_num >= config.memory_num) { | |
JOptionPane.showMessageDialog(null, "快表大小应该小于内存大小", "warning", JOptionPane.ERROR_MESSAGE); | |
return; | |
} | |
} | |
config.memory_access = Double.parseDouble(memory_access_edit.getText()); | |
config.quick_table_access = Double.parseDouble(quick_table_access_edit.getText()); | |
config.miss_page_interrupt = Double.parseDouble(miss_page_interrupt_edit.getText()); | |
config.is_check = Boolean.parseBoolean(String.valueOf(quick_table_check.isSelected())); | |
String str = task_edit.getText(); | |
List<String> lst = Arrays.asList(str.split(",")); | |
if (run_time_edit.getText().isEmpty()) { | |
run_time_edit.setText(""); | |
} | |
config.run_time = Integer.parseInt(run_time_edit.getText()); | |
config.taskSize = lst.size(); | |
config.task = new int[config.taskSize]; | |
for (int i =; i < config.taskSize; ++ i) { | |
str = lst.get(i); | |
config.task[i] = Integer.parseInt(str); | |
} | |
new Thread(() -> fifo.run()).start(); | |
new Thread(() -> lru.run()).start(); | |
new Thread(() -> lfu.run()).start(); | |
new Thread(() -> opt.run()).start(); | |
}); | |
JButton stop_continue_btn = new JButton("暂停执行"); | |
stop_continue_btn.setBounds(, 185, 120, 30); | |
stop_continue_btn.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(stop_continue_btn); | |
panel.add(stop_continue_btn); | |
stop_continue_btn.addActionListener(e -> { | |
if (stop_continue_btn.getText() == "暂停执行") { | |
fifo.pause.lock(); | |
lru.pause.lock(); | |
lfu.pause.lock(); | |
opt.pause.lock(); | |
stop_continue_btn.setText("继续执行"); | |
} else { | |
fifo.pause.unlock(); | |
lru.pause.unlock(); | |
lfu.pause.unlock(); | |
opt.pause.unlock(); | |
stop_continue_btn.setText("暂停执行"); | |
} | |
}); | |
JButton stop_btn = new JButton("结束执行"); | |
stop_btn.setBounds(, 185, 120, 30); | |
stop_btn.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(stop_btn); | |
panel.add(stop_btn); | |
stop_btn.addActionListener(e -> { | |
System.exit(); | |
}); | |
// FIFO | |
JLabel lblFifo = new JLabel("FIFO"); | |
title(lblFifo,, 250, 230, 50, 15); | |
FIFOtest = new TextArea(); | |
textBox(FIFOtest,, 180, 250, 200, 250); | |
// JScrollPane scroll_fifo = new JScrollPane(FIFOtest); | |
// scroll(scroll_fifo,, 250, 200, 250); | |
// LRU | |
JLabel lblLru = new JLabel("LRU"); | |
title(lblLru,, 490, 230, 50, 15); | |
LRUtest = new TextArea(); | |
textBox(LRUtest,, 420, 250, 200, 250); | |
// JScrollPane scroll_lru = new JScrollPane(LRUtest); | |
// scroll(scroll_lru,, 250, 200, 250); | |
// LFU | |
JLabel lblLfu = new JLabel("LFU"); | |
title(lblLfu,, 730, 230, 50, 15); | |
LFUtest = new TextArea(); | |
textBox(LFUtest,, 660, 250, 200, 250); | |
// JScrollPane scroll_lfu = new JScrollPane(LFUtest); | |
// scroll(scroll_lfu,, 250, 200, 250); | |
//OPT | |
JLabel lblOpt = new JLabel("OPT"); | |
title(lblOpt,, 970, 230, 50, 15); | |
OPTtest = new TextArea(); | |
textBox(OPTtest,, 900, 250, 200, 250); | |
// JScrollPane scroll_opt= new JScrollPane(OPTtest); | |
// scroll(scroll_opt,, 250, 200, 250); | |
JLabel label_ = new JLabel("中断次数:"); | |
label_.setBounds(35, 550, 100, 20); | |
label_.setFont(new Font("宋体", Font.PLAIN, 20)); | |
// frame.getContentPane().add(label_); | |
panel.add(label_); | |
fifo_interrupt_label = new JLabel(""); | |
fifo_interrupt_label.setBounds(, 550, 100, 20); | |
fifo_interrupt_label.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(fifo_interrupt_label); | |
panel.add(fifo_interrupt_label); | |
lru_interrupt_label = new JLabel(""); | |
lru_interrupt_label.setBounds(, 550, 100, 20); | |
lru_interrupt_label.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(lru_interrupt_label); | |
panel.add(lru_interrupt_label); | |
lfu_interrupt_label = new JLabel(""); | |
lfu_interrupt_label.setBounds(, 550, 100, 20); | |
lfu_interrupt_label.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(lfu_interrupt_label); | |
panel.add(lfu_interrupt_label); | |
opt_interrupt_label = new JLabel(""); | |
opt_interrupt_label.setBounds(, 550, 100, 20); | |
opt_interrupt_label.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(opt_interrupt_label); | |
panel.add(opt_interrupt_label); | |
JLabel label_ = new JLabel("总时间:"); | |
label_.setBounds(35, 600, 100, 20); | |
label_.setFont(new Font("宋体", Font.PLAIN, 20)); | |
// frame.getContentPane().add(label_); | |
panel.add(label_); | |
fifo_total_time_label = new JLabel(""); | |
fifo_total_time_label.setBounds(, 600, 100, 20); | |
fifo_total_time_label.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(fifo_total_time_label); | |
panel.add(fifo_total_time_label); | |
lru_total_time_label = new JLabel(""); | |
lru_total_time_label.setBounds(, 600, 100, 20); | |
lru_total_time_label.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(lru_total_time_label); | |
panel.add(lru_total_time_label); | |
lfu_total_time_label = new JLabel(""); | |
lfu_total_time_label.setBounds(, 600, 100, 20); | |
lfu_total_time_label.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(lfu_total_time_label); | |
panel.add(lfu_total_time_label); | |
opt_total_time_label = new JLabel(""); | |
opt_total_time_label.setBounds(, 600, 100, 20); | |
opt_total_time_label.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(opt_total_time_label); | |
panel.add(opt_total_time_label); | |
JLabel label_ = new JLabel("平均时间:"); | |
label_.setBounds(35, 650, 100, 20); | |
label_.setFont(new Font("宋体", Font.PLAIN, 20)); | |
// frame.getContentPane().add(label_); | |
panel.add(label_); | |
fifo_ave_time_label = new JLabel(""); | |
fifo_ave_time_label.setBounds(, 650, 100, 20); | |
fifo_ave_time_label.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(fifo_ave_time_label); | |
panel.add(fifo_ave_time_label); | |
lru_ave_time_label = new JLabel(""); | |
lru_ave_time_label.setBounds(, 650, 100, 20); | |
lru_ave_time_label.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(lru_ave_time_label); | |
panel.add(lru_ave_time_label); | |
lfu_ave_time_label = new JLabel(""); | |
lfu_ave_time_label.setBounds(, 650, 100, 20); | |
lfu_ave_time_label.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(lfu_ave_time_label); | |
panel.add(lfu_ave_time_label); | |
opt_ave_time_label = new JLabel(""); | |
opt_ave_time_label.setBounds(, 650, 100, 20); | |
opt_ave_time_label.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(opt_ave_time_label); | |
panel.add(opt_ave_time_label); | |
JLabel label_ = new JLabel("缺页率:"); | |
label_.setBounds(35, 700, 100, 20); | |
label_.setFont(new Font("宋体", Font.PLAIN, 20)); | |
// frame.getContentPane().add(label_); | |
panel.add(label_); | |
fifo_miss_page_label = new JLabel(""); | |
fifo_miss_page_label.setBounds(, 700, 100, 20); | |
fifo_miss_page_label.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(fifo_miss_page_label); | |
panel.add(fifo_miss_page_label); | |
lru_miss_page_label = new JLabel(""); | |
lru_miss_page_label.setBounds(, 700, 100, 20); | |
lru_miss_page_label.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(lru_miss_page_label); | |
panel.add(lru_miss_page_label); | |
lfu_miss_page_label = new JLabel(""); | |
lfu_miss_page_label.setBounds(, 700, 100, 20); | |
lfu_miss_page_label.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(lfu_miss_page_label); | |
panel.add(lfu_miss_page_label); | |
opt_miss_page_label = new JLabel(""); | |
opt_miss_page_label.setBounds(, 700, 100, 20); | |
opt_miss_page_label.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(opt_miss_page_label); | |
panel.add(opt_miss_page_label); | |
JButton save_btn = new JButton("保存"); | |
save_btn.setBounds(, 760, 120, 30); | |
save_btn.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(save_btn); | |
panel.add(save_btn); | |
save_btn.addActionListener(e -> { | |
new Save(); | |
}); | |
JButton open_btn = new JButton("打开"); | |
open_btn.setBounds(, 760, 120, 30); | |
open_btn.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(open_btn); | |
panel.add(open_btn); | |
open_btn.addActionListener(e -> { | |
new History(); | |
}); | |
JButton clear_btn = new JButton("清空"); | |
clear_btn.setBounds(, 760, 120, 30); | |
clear_btn.setFont(new Font("宋体", Font.PLAIN,)); | |
// frame.getContentPane().add(clear_btn); | |
panel.add(clear_btn); | |
clear_btn.addActionListener(e->{ | |
on_clear_btn_clicked(); | |
clear_parameter(); | |
}); | |
} | |
/** | |
* 四种算法标题 | |
* @param label | |
* @param size | |
* @param x | |
* @param y | |
* @param w | |
* @param h | |
*/ | |
public void title(JLabel label, int size, int x, int y ,int w, int h) { | |
label.setFont(new Font("宋体", Font.PLAIN, size)); | |
label.setBounds(x, y, w, h); | |
// frame.getContentPane().add(label); | |
panel.add(label); | |
} | |
/** | |
* 四种算法运行结果显示框 | |
* @param textArea | |
* @param size | |
* @param x | |
* @param y | |
* @param w | |
* @param h | |
*/ | |
public void textBox(TextArea textArea, int size, int x, int y ,int w, int h) { | |
textArea.setFont(new Font("宋体", Font.PLAIN, size)); | |
textArea.setBackground(SystemColor.controlHighlight); | |
textArea.setBounds(x, y, w, h); | |
// frame.getContentPane().add(textArea); | |
panel.add(textArea); | |
} | |
/** | |
* 分别设置水平和垂直滚动条自动出现 | |
* @param scrollPane | |
* @param x | |
* @param y | |
* @param w | |
* @param h | |
*/ | |
public void scroll(JScrollPane scrollPane,int x, int y, int w, int h) { | |
scrollPane.setLocation(x, y); | |
scrollPane.setSize(w, h); | |
scrollPane.setHorizontalScrollBarPolicy( | |
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); | |
scrollPane.setVerticalScrollBarPolicy( | |
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); | |
// frame.getContentPane().add(scrollPane); | |
panel.add(scrollPane); | |
} | |
public void on_clear_btn_clicked() { | |
// fifo.task[] = new int[fifo.taskSize]; | |
final int[] task = config.task; | |
FIFOtest.setText(""); | |
fifo.fifo_lackpage =; | |
fifo.fifo_ave_time=; | |
fifo.fifo_total_time=; | |
fifo.fifo_memory_page.clear(); | |
fifo.fifo_quick_table_page.clear(); | |
fifo.fifo_memory_page.clear(); | |
fifo_interrupt_label.setText(""); | |
fifo_miss_page_label.setText(""); | |
fifo_total_time_label.setText(""); | |
fifo_ave_time_label.setText(""); | |
LRUtest.setText(""); | |
lru.lru_lackpage =; | |
lru.lru_ave_time=; | |
lru.lru_total_time=; | |
lru.lru_memory_page.clear(); | |
lru.lru_quick_table_page.clear(); | |
lru_interrupt_label.setText(""); | |
lru_miss_page_label.setText(""); | |
lru_total_time_label.setText(""); | |
lru_ave_time_label.setText(""); | |
LFUtest.setText(""); | |
lfu.lfu_lackpage =; | |
lfu.lfu_ave_time=; | |
lfu.lfu_total_time=; | |
lfu.lfu_memory_page.clear(); | |
lfu.lfu_quick_table_page.clear(); | |
lfu_interrupt_label.setText(""); | |
lfu_miss_page_label.setText(""); | |
lfu_total_time_label.setText(""); | |
lfu_ave_time_label.setText(""); | |
OPTtest.setText(""); | |
opt.opt_lackpage =; | |
opt.opt_ave_time=; | |
opt.opt_total_time=; | |
opt.opt_memory_page.clear(); | |
opt.opt_quick_table_page.clear(); | |
opt_interrupt_label.setText(""); | |
opt_miss_page_label.setText(""); | |
opt_total_time_label.setText(""); | |
opt_ave_time_label.setText(""); | |
} | |
public static void on_quick_table_check_clicked(boolean checked) { | |
if (checked) | |
quick_table_num_edit.setEnabled(true); | |
else | |
quick_table_num_edit.setEnabled(false); | |
} | |
public static void clear_parameter() { | |
memory_num_edit.setText(""); | |
taskSize_edit.setText(""); | |
taskSize_low_edit.setText(""); | |
taskSize_high_edit.setText(""); | |
task_low_edit.setText(""); | |
task_high_edit.setText(""); | |
task_edit.setText(""); | |
quick_table_num_edit.setText(""); | |
memory_access_edit.setText(""); | |
quick_table_access_edit.setText(""); | |
miss_page_interrupt_edit.setText(""); | |
run_time_edit.setText(""); | |
quick_table_check.setSelected(false); | |
quick_table_num_edit.setEnabled(false); | |
} | |
public static void main(String[] args) { | |
Page window = new Page(); | |
window.frame.setVisible(true); | |
} | |
} |