计算机信息学院
操作系统综合设计报告
设计题目:页面置换算法
学生姓名:
学 号:
专业班级:计算机科学技术班
2015 年 X月
设计题目 3
二开发环境工具 3
三设计原理 3
1佳(Optimal)置换算法 3
2先进先出(FIFO)页面置换算法 4
3久未(LRU)页面置换算法 4
四重算法 5
1算法流程图 5
2 关键代码 8
五设计结果 22
六设计体会 24
七附录 24
设计题目
第15题:页面置换算法
1 建立相应数结构
2 屏幕显示页面状况
3 时间流逝面种方法模拟:
(a) 键盘次认时间单位
(b) 相应WM_TIMER
4 批页置换情况存入磁盘文件读出重放
5 计算页面缺页次数缺页页面置换次数
6 支持算法:FIFOLRU佳置换算法
二开发环境工具
开发环境:Windows
开发工具:VC60 Eclipse
三设计原理
1佳(Optimal)置换算法
佳置换算法Belady1966年提出种理算法选择淘汰页面永未长时间访问页面采佳置换算法通常保证获低缺页率目前法预知进程存干页面中页面未长时间访问该算法法实现该算法评价算法现举例说明:
假定系统某进程分配三物理快考虑页面号引串:
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
进程运行时先701三页面放入存进程访问页面2时会产生缺页中断时OS根佳置换算法选择页面7予淘汰页面0作第5访问页面页面1第14访问页面页面7第18次页面访问需调入次访问页面0时已存必产生缺页中断进程访问页面3时讲引起页面1淘汰现120三页面中晚访问图1示出采佳置换算法时置换图图出采佳置换算法发生6次页面置换
页面号
7
0
1
2
0
3
0
4
2
3
0
3
2
1
2
0
1
7
0
1
物理快1
7
7
7
2
2
2
2
2
7
物理快2
0
0
0
0
4
0
0
0
物理快3
1
1
3
3
3
1
1
2先进先出(FIFO)页面置换算法
FIFO算法早出现置换算法该算法总淘汰先进入存页面选择存中驻留时间长页面予淘汰该算法实现简单需已调入存页面先次序排成队列发生缺页时队首页面出队找页面入队样完成页面置换务书例表格:
7 0 1 2 0 3 4 2 3 0 3 2 1 2 0 1 7 0
7
7
7
2
2
2
4
4
4
0
0
0
0
0
0
0
7
7
0
0
0
0
3
3
2
2
2
2
2
1
1
1
1
1
0
1
1
1
1
0
0
3
3
3
3
3
2
2
2
2
2
该算法程序运行规律相适应忽视程序运行局部性原理造成需访问页面常性置换
3久未(LRU)页面置换算法
FIFO算法性较差根页面进入先次序进行置换反映页面情况久未(LRU)算法基思想利预测未根程序运行局部性原理佳置换算法理价值实际系统中法利预测未该算法访问字段记录次访问历时间t需淘汰页面时选择页面中t值久未页面予置换
书例表格:
7 0 1 2 0 3 4 2 3 0 3 2 1 2 0 1 7 0
7
7
7
2
2
2
4
4
4
0
0
0
1
1
1
1
1
1
0
0
0
0
0
0
0
3
3
3
3
3
3
0
0
0
0
1
1
1
3
3
2
2
2
2
2
2
2
2
2
7
7
四重算法
1算法流程图
FIFO算法流程图:
Y
i++
p[i]容直接装入面空存块i++
page[]中先装入页面置换出i++
Page[]否空
前p[]中第i元素否已存中
页面走存入数组p[]中存块page[]表示初始化0
开始
N
Y
N
输出前存块状态
结束
LRU算法流程图:
开始
页面走存入数组p[]中存块page[]表示初始化0
Y
前p[]中第i元素否已存
i++
N
N
Page[]否空
Y
结束
p[i]容直接装入面空存块i++
输出前存块状态
page[]中久未页面置换出i++
OPT算法流程图:
开始
Y
i++
结束
输出前存块状态
page[]中段时间时间离现远换出i++
p[i]容直接装入面空存块i++
Page[]否空
前p[]中第i元素否已存
页面走存入数组p[]中存块page[]表示初始化0
N
N
Y
2 关键代码
1 FIFO算法代码:
public class FIFOControl {
private int[] num {70120304230321201701}参构造函数
private int[] num new int[50]
private int numLength
private int[] pg new int[10]
private int[] timepg new int[10]
private int PageNum
public int[][] store
FIFOControl()
{
thisPageNum 3
numLength 20
int[] numtemp {70120304230321201701}
for( int i 0 i < numLength i++ )
{
num[i] numtemp[i]
}
for(int i 0 i < 10 i++)
{
pg[i] 1
timepg[i] 1
}
store new int[numLength][PageNum+1]
for( int i 1 i < PageNum+1 i++)
{
for( int j 0 j < numLength j++ )
{
store[j][i] 1
}
}
for( int k 0 k < numLength k++ )
{
store[k][0] 0
}
}
FIFOControl( int nump int[] n int nlength )参数:1物理块数 2页面序列号 3页面序列号数组长度 meiyong
{
thisPageNum nump
numLength nlength
for(int j 0 j < nlength j++)
{
num[j] n[j]
}
for(int i 0 i < 10 i++)
{
pg[i] 1
timepg[i] 1
}
store new int[numLength][PageNum+1]
for( int i 1 i < PageNum+1 i++)
{
for( int j 0 j < numLength j++ )
{
store[j][i] 1
}
}
for( int k 0 k < numLength k++ )
{
store[k][0] 0
}
}
private boolean checkInPage( int[] p int plength int keynum )
{
for( int jj 0 jj < plength jj++)
{
timepg[jj]++
}
for(int j 0 j < plength j++)
{
if( keynum p[j] )
{
return true
}
}
return false
}
private void caculate()
{
物理页框装满
int timer 0横着指针
for( int i 0 i < PageNum i++ )
{
pg[i] num[i]
for( int j 0 j < i j++)加时间
{
timepg[j]++
}
for(int jj 0 jj < PageNum jj++)数放store中
{
store[timer][jj+1] pg[jj]
}
timer++
}
面数先进先出序放物理块中
for( int i PageNum i < numLength i++ )
{
if( checkInPage( pg PageNum num[i] ) )果物理框中应位时间置1
{
for(int j 0 j < PageNum j++)次物理块中数存储起
{
store[timer][j+1] pg[j]
}
timer++
}
else否找出时间长置换应位时间置1
{
int max timepg[0]
int maxvalue pg[0]
int maxindex 0
int temp
for( int n 1 n < PageNum n++ )找时间物理块
{
temp timepg[n]
if( temp > max )
{
max temp
maxvalue pg[n]
maxindex n
}
}
Systemoutprintln( max + max )
Systemoutprintln( maxindex + maxindex )
pg[maxindex] num[i]
timepg[maxindex] 1
for(int j 0 j < PageNum j++)次数存储起
{
store[timer][j+1] pg[j]
}
store[timer][0] maxindex+1
timer++
}
}
测试查结果
for( int i 0 i < PageNum+1 i++)se shi
{
for( int j 0 j < numLength j++ )
{
Systemoutprint(store[j][i] + )
}
Systemoutprintln()
}
}
public int[][] getStore()
{
thiscaculate()
return store
}public class FIFOControl {
private int[] num {70120304230321201701}参构造函数
private int[] num new int[50]
private int numLength
private int[] pg new int[10]
private int[] timepg new int[10]
private int PageNum
public int[][] store
FIFOControl()
{
thisPageNum 3
numLength 20
int[] numtemp {70120304230321201701}
for( int i 0 i < numLength i++ )
{
num[i] numtemp[i]
}
for(int i 0 i < 10 i++)
{
pg[i] 1
timepg[i] 1
}
store new int[numLength][PageNum+1]
for( int i 1 i < PageNum+1 i++)
{
for( int j 0 j < numLength j++ )
{
store[j][i] 1
}
}
for( int k 0 k < numLength k++ )
{
store[k][0] 0
}
}
FIFOControl( int nump int[] n int nlength )参数:1物理块数 2页面序列号 3页面序列号数组长度 meiyong
{
thisPageNum nump
numLength nlength
for(int j 0 j < nlength j++)
{
num[j] n[j]
}
for(int i 0 i < 10 i++)
{
pg[i] 1
timepg[i] 1
}
store new int[numLength][PageNum+1]
for( int i 1 i < PageNum+1 i++)
{
for( int j 0 j < numLength j++ )
{
store[j][i] 1
}
}
for( int k 0 k < numLength k++ )
{
store[k][0] 0
}
}
private boolean checkInPage( int[] p int plength int keynum )
{
for( int jj 0 jj < plength jj++)
{
timepg[jj]++
}
for(int j 0 j < plength j++)
{
if( keynum p[j] )
{
return true
}
}
return false
}
private void caculate()
{
物理页框装满
int timer 0横着指针
for( int i 0 i < PageNum i++ )
{
pg[i] num[i]
for( int j 0 j < i j++)加时间
{
timepg[j]++
}
for(int jj 0 jj < PageNum jj++)数放store中
{
store[timer][jj+1] pg[jj]
}
timer++
}
面数先进先出序放物理块中
for( int i PageNum i < numLength i++ )
{
if( checkInPage( pg PageNum num[i] ) )果物理框中应位时间置1
{
for(int j 0 j < PageNum j++)次物理块中数存储起
{
store[timer][j+1] pg[j]
}
timer++
}
else否找出时间长置换应位时间置1
{
int max timepg[0]
int maxvalue pg[0]
int maxindex 0
int temp
for( int n 1 n < PageNum n++ )找时间物理块
{
temp timepg[n]
if( temp > max )
{
max temp
maxvalue pg[n]
maxindex n
}
}
Systemoutprintln( max + max )
Systemoutprintln( maxindex + maxindex )
pg[maxindex] num[i]
timepg[maxindex] 1
for(int j 0 j < PageNum j++)次数存储起
{
store[timer][j+1] pg[j]
}
store[timer][0] maxindex+1
timer++
}
}
}
public int[][] getStore()
{
thiscaculate()
return store
}
2 LRU算法代码:
public class LRUControl {
private int[] num {70120304230321201701}参构造函数
private int[] num new int[50]参构造函数
private int numLength
private int PageNum
private Page[] pg new Page[10]
public int[][] store
public int[][] temp1
LRUControl()
{
PageNum 3
numLength numlength
store new int[numLength][PageNum]
for( int i 0 i
pg[i] new Page()
if( thispg[i] null )
{
thispg[i]setValue(1)
thispg[i]setTime(0)
}
else
{
Systemoutprintln( 象创建失败 +\\r\\n)
}
}
}
LRUControl( int nump int[] n int nlength )参数:1物理块数 2页面序列号 3页面序列号数组长度 meiyong
{
thisPageNum nump
numLength nlength
for(int j 0 j < nlength j++)
{
num[j] n[j]
}
store new int[numLength][PageNum+1]gai dong
temp1 new int[numLength][PageNum+1]
for( int i 0 i
pg[i] new Page()
if( thispg[i] null )
{
thispg[i]setValue(1)
thispg[i]setTime(0)
}
else
{
Systemoutprintln( 象创建失败 +\\r\\n)
}
}
}
private boolean checkInPage( Page[] p int plength int keynum )
{
for( int jj 0 jj < plength jj++)
{
p[jj]setTime( p[jj]getTime()+1 )
}
for(int j 0 j < plength j++)
{
if( keynum p[j]getValue() )
{
return true
}
}
return false
}
void caculate()
{
int timer 0
前PageNum先物理快装满
for( int i 0 i < PageNum i++ )
{
pg[i]setValue( num[i] )
pg[i]setTime( 1 )
for( int j 0 j < i j++)
{
pg[j]setTime( pg[j]getTime()+1 )
}
for(int j 0 j < PageNum j++)
{
store[timer][j] pg[j]getValue()
}
timer++
}
for( int i 0 i < PageNum i++)
{
store[i][PageNum] 0
}
num数组面页面检查否前面出现置该页框时间1没原基础加1
for( int i PageNum i < numLength i++ )
{
if( checkInPage( pg PageNum num[i] ) )果物理框中应位时间置1
{
for( int m 0 m < PageNum m++ )num[i]pg[]中相等物理块时间置1
{
if( num[i] pg[m]getValue() )
{
pg[m]setTime(1)
break
}
}
for(int j 0 j < PageNum j++)次物理块中数存储起
{
store[timer][j] pg[j]getValue()
}
store[timer][PageNum] 0
timer++
}
else否找出时间长置换应位时间置1
{
int max pg[0]getTime()
int maxindex 0
int temp
for( int n 1 n < PageNum n++ )找时间物理块
{
temp pg[n]getTime()
if( temp > max )
{
max temp
maxindex n
}
}
Systemoutprintln( max + max )
Systemoutprintln( maxindex + maxindex )
pg[maxindex]setTime(1)
pg[maxindex]setValue(num[i])
for(int j 0 j < PageNum j++)次数存储起
{
store[timer][j] pg[j]getValue()
}
store[timer][PageNum] maxindex+1
timer++
}
}
}
void display()
{
for( int i 0 i
for(int j 0 j < numLength j++)
{
Systemoutprint( store[j][i] + )
}
Systemoutprintln()
}
}
private void adjustStore()
{
for( int i 1 i
for(int j 0 j < numLength j++)
{
temp1[j][i] store[j][i1]
}
}
for(int k 0 k < numLength k++)
{
temp1[k][0] store[k][PageNum]
}
}
public int[][] getStore()
{
adjustStore()
return temp1
}
}
3 OPT算法代码:
int Optimal(int page[] int page_num int memory_num int process[][]){参数(访问页面数组访问页面总数存块数)
int max 未长时间访问页面距次访问时间
int max_page 未长时间访问页面号
int memory_null 存中空位号(没空位时1)
int lack 0 记录缺页次数
int[][] process new int[50][50]记录置换程
Memory[] memory new Memory[10] 创建存
记录置换程数组初始化
for(int a0a
}
}
存块初始化
for(int m0m
memory[m]page 1
memory[m]next 0
}
for(int i0i
if(isMemory(page[i] memory memory_num)){页面已存中
Systemoutprintln(页面已存中)
记录
process[0][i] 0没置换
for(int n1n
}
}else{ 页面存中
memory_null isNull(memory memory_num)获取存中空位号(没空位时1)
if(memory_null1){ [存中没空位]
获取存块中页面距次访问时间
for(int j0j
}
输出存块中页面距次访问时间
Systemoutprint(next)
for(int y0y
}
Systemoutprintln()
获取未长时间访问页面号
max memory[0]next
max_page 0
for(int j0j
max memory[j]next
max_page j
}
}
未长时间访问页面号置换新页面
Systemoutprintln(置换+memory[max_page]page+>+page[i])
memory[max_page]page page[i]置换
lack++更新缺页次数
记录
process[0][i] max_page+1发生置换
process[max_page+1][i] page[i]
for(int n1n
process[n][i] process[n][i1]
}
}
}else { [存中空位]
Systemoutprintln(存中空位+memory_null)
memory[memory_null]page page[i]
新页面放入存块空位中
for(int x0x
}
Systemoutprintln()
记录
process[0][i] 0没置换
process[memory_null+1][i] page[i]
for(int n1n
if(i>0){
process[n][i] process[n][i1]
}else{
process[n][i] 1
}
}
}
}
}
}
Systemoutprintln(\n缺页数+lack)
Systemoutprintln(缺页率+(double)lackpage_num)
Systemoutprintln(佳页面置换算法置换表)
for(int a0a
}
Systemoutprintln()
}
return 0
}
五设计结果
图1 页面号文件
图2 读入页面号
图3 动运行
图4 单步运行
六设计体会
次课程设计仅操作系统门课程更深入研究重概念巩固掌握通努力三页面置换算法程序已完成然做少够完善毕竟努力结果点收获:
1通网书查阅相关资料JAVA语言基框架新解加深视化程序认识
2Java语言实现功时语言较简练更容易理解实性强
3先进先出页面置换LRUOPT算法特点实践起页面置换算法新认识
外学合作收获许做界面短板学强项合作方面收获学少东西
七附录
代码:(总4Java文件)
第
FIFOControljava
public class FIFOControl {
private int[] num {70120304230321201701}参构造函数
private int[] num new int[50]
private int numLength
private int[] pg new int[10]
private int[] timepg new int[10]
private int PageNum
public int[][] store
FIFOControl()
{
thisPageNum 3
numLength 20
int[] numtemp {70120304230321201701}
for( int i 0 i < numLength i++ )
{
num[i] numtemp[i]
}
for(int i 0 i < 10 i++)
{
pg[i] 1
timepg[i] 1
}
store new int[numLength][PageNum+1]
for( int i 1 i < PageNum+1 i++)
{
for( int j 0 j < numLength j++ )
{
store[j][i] 1
}
}
for( int k 0 k < numLength k++ )
{
store[k][0] 0
}
}
FIFOControl( int nump int[] n int nlength )参数:1物理块数 2页面序列号 3页面序列号数组长度 meiyong
{
thisPageNum nump
numLength nlength
for(int j 0 j < nlength j++)
{
num[j] n[j]
}
for(int i 0 i < 10 i++)
{
pg[i] 1
timepg[i] 1
}
store new int[numLength][PageNum+1]
for( int i 1 i < PageNum+1 i++)
{
for( int j 0 j < numLength j++ )
{
store[j][i] 1
}
}
for( int k 0 k < numLength k++ )
{
store[k][0] 0
}
}
private boolean checkInPage( int[] p int plength int keynum )
{
for( int jj 0 jj < plength jj++)
{
timepg[jj]++
}
for(int j 0 j < plength j++)
{
if( keynum p[j] )
{
return true
}
}
return false
}
private void caculate()
{
物理页框装满
int timer 0横着指针
for( int i 0 i < PageNum i++ )
{
pg[i] num[i]
for( int j 0 j < i j++)加时间
{
timepg[j]++
}
for(int jj 0 jj < PageNum jj++)数放store中
{
store[timer][jj+1] pg[jj]
}
timer++
}
面数先进先出序放物理块中
for( int i PageNum i < numLength i++ )
{
if( checkInPage( pg PageNum num[i] ) )果物理框中应位时间置1
{
for(int j 0 j < PageNum j++)次物理块中数存储起
{
store[timer][j+1] pg[j]
}
timer++
}
else否找出时间长置换应位时间置1
{
int max timepg[0]
int maxvalue pg[0]
int maxindex 0
int temp
for( int n 1 n < PageNum n++ )找时间物理块
{
temp timepg[n]
if( temp > max )
{
max temp
maxvalue pg[n]
maxindex n
}
}
Systemoutprintln( max + max )
Systemoutprintln( maxindex + maxindex )
pg[maxindex] num[i]
timepg[maxindex] 1
for(int j 0 j < PageNum j++)次数存储起
{
store[timer][j+1] pg[j]
}
store[timer][0] maxindex+1
timer++
}
}
测试查结果
for( int i 0 i < PageNum+1 i++)se shi
{
for( int j 0 j < numLength j++ )
{
Systemoutprint(store[j][i] + )
}
Systemoutprintln()
}
}
public int[][] getStore()
{
thiscaculate()
return store
}
}
2 第二
LRUControljava
public class LRUControl {
private int[] num {70120304230321201701}参构造函数
private int[] num new int[50]参构造函数
private int numLength
private int PageNum
private Page[] pg new Page[10]
public int[][] store
public int[][] temp1
LRUControl()
{
PageNum 3
numLength numlength
store new int[numLength][PageNum]
for( int i 0 i
pg[i] new Page()
if( thispg[i] null )
{
thispg[i]setValue(1)
thispg[i]setTime(0)
}
else
{
Systemoutprintln( 象创建失败 +\\r\\n)
}
}
}
LRUControl( int nump int[] n int nlength )参数:1物理块数 2页面序列号 3页面序列号数组长度 meiyong
{
thisPageNum nump
numLength nlength
for(int j 0 j < nlength j++)
{
num[j] n[j]
}
store new int[numLength][PageNum+1]gai dong
temp1 new int[numLength][PageNum+1]
for( int i 0 i
pg[i] new Page()
if( thispg[i] null )
{
thispg[i]setValue(1)
thispg[i]setTime(0)
}
else
{
Systemoutprintln( 象创建失败 +\\r\\n)
}
}
}
private boolean checkInPage( Page[] p int plength int keynum )
{
for( int jj 0 jj < plength jj++)
{
p[jj]setTime( p[jj]getTime()+1 )
}
for(int j 0 j < plength j++)
{
if( keynum p[j]getValue() )
{
return true
}
}
return false
}
void caculate()
{
int timer 0
前PageNum先物理快装满
for( int i 0 i < PageNum i++ )
{
pg[i]setValue( num[i] )
pg[i]setTime( 1 )
for( int j 0 j < i j++)
{
pg[j]setTime( pg[j]getTime()+1 )
}
for(int j 0 j < PageNum j++)
{
store[timer][j] pg[j]getValue()
}
timer++
}
for( int i 0 i < PageNum i++)
{
store[i][PageNum] 0
}
num数组面页面检查否前面出现置该页框时间1没原基础加1
for( int i PageNum i < numLength i++ )
{
if( checkInPage( pg PageNum num[i] ) )果物理框中应位时间置1
{
for( int m 0 m < PageNum m++ )num[i]pg[]中相等物理块时间置1
{
if( num[i] pg[m]getValue() )
{
pg[m]setTime(1)
break
}
}
for(int j 0 j < PageNum j++)次物理块中数存储起
{
store[timer][j] pg[j]getValue()
}
store[timer][PageNum] 0
timer++
}
else否找出时间长置换应位时间置1
{
int max pg[0]getTime()
int maxindex 0
int temp
for( int n 1 n < PageNum n++ )找时间物理块
{
temp pg[n]getTime()
if( temp > max )
{
max temp
maxindex n
}
}
Systemoutprintln( max + max )
Systemoutprintln( maxindex + maxindex )
pg[maxindex]setTime(1)
pg[maxindex]setValue(num[i])
for(int j 0 j < PageNum j++)次数存储起
{
store[timer][j] pg[j]getValue()
}
store[timer][PageNum] maxindex+1
timer++
}
}
}
void display()
{
for( int i 0 i
for(int j 0 j < numLength j++)
{
Systemoutprint( store[j][i] + )
}
Systemoutprintln()
}
}
private void adjustStore()
{
for( int i 1 i
for(int j 0 j < numLength j++)
{
temp1[j][i] store[j][i1]
}
}
for(int k 0 k < numLength k++)
{
temp1[k][0] store[k][PageNum]
}
}
public int[][] getStore()
{
adjustStore()
return temp1
}
}
3 第三第二关联第二page类实现
Pagejava
public class Page {
private int time
private int value
public void setTime(int t)
{
thistime t
}
public void setValue(int v)
{
thisvalue v
}
public int getTime()
{
return thistime
}
public int getValue()
{
return thisvalue
}
Page()
{
time 0
value 0
}
}
4 第四
MyFramejava
import javaawtColor
import javaawtDimension
import javaawtFileDialog
import javaawtFont
import javaawtGraphics
import javaawtTextArea
import javaawtToolkit
import javaawteventActionEvent
import javaawteventActionListener
import javaawtimageBufferedImage
import javaioBufferedReader
import javaioBufferedWriter
import javaioFile
import javaioFileReader
import javaioFileWriter
import javaioIOException
import javaxswingButtonGroup
import javaxswingJOptionPane
import javaxswingJPanel
import javaxswingJButton
import javaxswingJFrame
import javaxswingJLabel
import javaxswingJRadioButton
import javaxswingJScrollPane
import javaxswingJSlider
import javaxswingJTextField
import javaxswingSwingConstants
页面置换算法
public class MyFrame extends JFrame implements ActionListener Runnable{
int width ToolkitgetDefaultToolkit()getScreenSize()width 取屏幕宽度
int height ToolkitgetDefaultToolkit()getScreenSize()height 取屏幕高度
int size10字体
private JTextField textField1textField2
private JButton button1button2button3button4button5button6
private JRadioButton radioButton1 radioButton2
private ButtonGroup buttongroup
private JLabel label1 label2 label3 label4 label5 label6
private JLabel[] label_page new JLabel[20]
private JLabel label_sign new JLabel(▲)记录运行位置
private JLabel[][] label_memory new JLabel[50][50]记录存块变化情况
private JLabel[][] label_memory_LRU new JLabel[50][50]记录存块变化情况
private JLabel[][] label_memory_FIFO new JLabel[50][50]记录存块变化情况
private JSlider slider滑动条
private JPanel panel
private JScrollPane scrollPane滚动面板
定义开保存话框
private FileDialog openDiasaveDia
private TextArea textArea
private File file
private File saveFile
前点击钮
private int button_chose 0
记录动运行前运行位置
private static int location 0
Thread thread new Thread(this)
输入页面号
private String[] page new String[20]
private int[] page_int new int[20]
输入页面号数
private int page_num 0
存块数
private int memory_num 3
佳置换算法缺页次数
private int Optimal_lack 0
记录置换程
private int[][] process_memory new int[50][50]
创建LRU象带入参数
LRUControl lru
int[][] process_memory_LRU
LRU置换算法缺页次数
private int Optimal_lack_LRU 0
FIFO
创建象带入参数
FIFOControl fifo
创建数组接收数
int[][] process_memory_FIFO
FIFO置换算法缺页次数
private int Optimal_lack_FIFO 0
private Color[] colors {Colorcyan Colororange ColorgreenColorpink Coloryellow
Colorcyan Colororange ColorgreenColorpink Coloryellow
Colorcyan Colororange ColorgreenColorpink Coloryellow
Colorcyan Colororange ColorgreenColorpink Coloryellow
}
MyFrame(){构造函数
thissetTitle(页面置换算法)
thissetSize(950 600)
thissetLocation((width950)2 (height600)2)
thissetLayout(null)设置布局方式
label1new JLabel(源文件)
label1setBounds(30 30 60 30)
thisadd(label1)
textField1new JTextField(15)
textField1setBounds(100 30 300 30)
thisadd(textField1)
button1new JButton(开文件)
button1setBounds(430 30 100 30)
button1addActionListener(this)
thisadd(button1)
radioButton1 new JRadioButton(读入页面 true)
radioButton2 new JRadioButton(置换情况)
buttongroup new ButtonGroup()
buttongroupadd(radioButton1)
buttongroupadd(radioButton2)
radioButton1setBounds(600 30 100 30)
radioButton2setBounds(700 30 100 30)
thisadd(radioButton1)
thisadd(radioButton2)
label2new JLabel(存块数)
label2setBounds(30 80 60 30)
thisadd(label2)
slider new JSlider(JSliderHORIZONTAL1101)
slidersetBounds(100 80 300 60)
slidersetValue(3)设置滑动条初始值
slidersetMajorTickSpacing(9)设置刻度标记间隔
slidersetMinorTickSpacing(1)设置次刻度标记间隔
slidersetPaintTicks(true)设置滑块绘制刻度标记显示划分刻度
slidersetPaintLabels(true)
slidersetSnapToTicks(true)
thisadd(slider)
textArea new TextArea()
textAreasetBounds(30 160 700 400)
thisadd(textArea)
panel new JPanel()
panelsetBounds(30 160 700 400)
panelsetBackground(ColorWHITE)
panelsetPreferredSize(new Dimension(1000800))句代码设置panel首选时保证宽高JScrollPane宽高样面JScrollPane会出现滚动条
panelsetLayout(null)设置panel布局方式
thisadd(panel)
scrollPane new JScrollPane(panel)
scrollPanesetBackground(ColorWHITE)
scrollPanesetBounds(30 160 700 400)
scrollPanesetVerticalScrollBarPolicy(JScrollPaneVERTICAL_SCROLLBAR_AS_NEEDED)
scrollPanegetHorizontalScrollBar()setValue(0)设置滚动条水位置
scrollPanegetVerticalScrollBar()setValue(0)设置滚动条竖直位置
thisadd(scrollPane)
thisgetContentPane()add(scrollPane)
label3new JLabel(页面输入序)
label3setFont(new Font(SerifFontPLAINsize))
label3setBounds(10 20 70 30)
paneladd(label3)
label4new JLabel(佳置换算法)
label4setFont(new Font(SerifFontPLAINsize))
label4setBounds(10 90 70 30)
paneladd(label4)
label5new JLabel(LRU置换算法)
label5setFont(new Font(SerifFontPLAINsize))
label5setBounds(10 190 70 30)
paneladd(label5)
label6new JLabel(FIFO置换算法)
label6setFont(new Font(SerifFontPLAINsize))
label6setBounds(10 290 70 30)
paneladd(label6)
button3new JButton(重置)
button3setBounds(780 160 100 30)
button3addActionListener(this)
thisadd(button3)
button4new JButton(动运行)
button4setBounds(780 210 100 30)
button4addActionListener(this)
thisadd(button4)
button5new JButton(单步运行)
button5setBounds(780 260 100 30)
button5addActionListener(this)
thisadd(button5)
textArea new TextArea()
textAreasetText(\t缺页率)
textAreasetBounds(755 310 170 200)
thisadd(textArea)
button6new JButton(保存)
button6setBounds(780 520 100 30)
button6addActionListener(this)
thisadd(button6)
thissetVisible(true)
thissetResizable(false)
threadstart()
for(int i0i<50i++){
for(int j0j<50j++){
label_memory[i][j] new JLabel(7)
label_memory_LRU[i][j] new JLabel()
label_memory_FIFO[i][j] new JLabel()
}
}
默认模式 FileDialogLOAD
openDia new FileDialog(this开源文件FileDialogLOAD)
saveDia new FileDialog(this保存页面置换结果FileDialogSAVE)
}
清空面板
public void removePanel(){
panelremoveAll()清空面板
paneladd(label3)
paneladd(label4)
paneladd(label5)
paneladd(label6)
panelrepaint()刷新面板
}
public void actionPerformed(ActionEvent e){
char ch
String str
int page2[] {70120304230321201701}
int page_num 20
int memory_num 3
Optimal(page2 page_num memory_num process_memory)
if(egetSource()button1){开源文件
removePanel()清空面板
openDiasetVisible(true)
String dirPath openDiagetDirectory()获取文件路径
String fileName openDiagetFile()获取文件名称
Systemoutprintln(dirPath ++++ fileName)
textField1setText(dirPath + fileName)
果开路径 目录空 返回空
if(dirPath null || fileName null)
return
textAreasetText()清空文
file new File(dirPathfileName)
try {
BufferedReader bufr new BufferedReader(new FileReader(file))
String line null
int n 0
while( (line bufrreadLine()) null){
输入页面号保存page数组中
for(int i0i
if(ch' '){
str str + ch
if(ilinelength()1){获取字符
page[n] str
}
}else{
if(strequals()){
page[n] IntegerparseInt(str)
page[n] str
n++
str
}
}
if(n>19) break
}
if(n>19) break
}
bufrclose()
page_num n
Systemoutprintln(页面数+page_num)
for(int k0k
if(page[k]equals()){
label_page[k] new JLabel(page[k])
label_page[k]setFont(new Font(SerifFontBOLD12))
label_page[k]setBounds(100+40*k 20 40 20)
label_page[k]setOpaque(true)设置透明显示出背景颜色
label_page[k]setBackground(colors[k])
label_page[k]setHorizontalAlignment(SwingConstantsCENTER)容居中
paneladd(label_page[k])
}
}
panelrepaint()刷新面板
文件中读入页面号信息转化整形
for(int i0i
page_int[i] IntegerparseInt(page[i])
}catch(NumberFormatException e1){
e1printStackTrace()
JOptionPaneshowMessageDialog(this 什鬼法正确读取源文件信息~)
page_num 0
break
}
}
获取存块数
memory_num slidergetValue()
Systemoutprintln(存块数+memory_num)
佳置换算法
Optimal(page_int page_num memory_num process_memory)
} catch (IOException ex) {
throw new RuntimeException(文件读取失败)
}
}else if(egetSource()button3){重置
removePanel()清空面板
for(int k0k
}
panelrepaint()刷新面板
location0
}else if(egetSource()button4){动运行
button_chose 4
获取存块数
memory_num slidergetValue()
佳置换算法
Optimal(page_int page_num memory_num process_memory)
LRU
lru new LRUControl( memory_num page_int page_num)
调象计算函数
lrucaculate()
传出二维数组
process_memory_LRU new int[page_num][memory_num+1]
调象返回参数方法
process_memory_LRU lrugetStore()
Systemoutprintln(LRU)
for(int i0i
}
Systemoutprintln()
}
FIFO
fifo new FIFOControl( memory_num page_int page_num)
process_memory_FIFO new int[page_num][memory_num+1]
process_memory_FIFO fifogetStore()
Systemoutprintln(FIFO)
for(int i0i
}
Systemoutprintln()
}
if(page_num0){
JOptionPaneshowMessageDialog(this 先开源文件哦~)}
else{
removePanel()清空面板
for(int k0k
}
panelrepaint()刷新面板
new Thread(new Runnable() {
public void run() {
button4setEnabled(false)动运行钮设置点击防止次点击造成显示混乱
button5setEnabled(false)
根存块少动态调整显示标签位置(改变Y轴起始值)
int frist_y 0
if(memory_num>4){
frist_y memory_num 4
label5setBounds(10 190 + frist_y*20 70 30)
label6setBounds(10 290 + frist_y*20*2 70 30)
}else{
label5setBounds(10 190 70 30)
label6setBounds(10 290 70 30)
}
scrollPanegetHorizontalScrollBar()setValue(0)设置滚动条水位置
Optimal_lack 0佳置换缺页数置零
Optimal_lack_LRU 0LRU缺页数置零
Optimal_lack_FIFO 0FIFO缺页数置零
绘制页面置换情况
for(int k0k
label_signsetHorizontalAlignment(SwingConstantsCENTER)容居中
paneladd(label_sign)
佳置换算法
for(int j1j
label_memory[j][k]setText()
}else{
label_memory[j][k]setText(process_memory[j][k]+)process_memory[k][0]+
}
label_memory[j][k]setBounds(105+40*k 70+20*j 30 20)
label_memory[j][k]setHorizontalAlignment(SwingConstantsCENTER)容居中
label_memory[j][k]setOpaque(true)设置透明显示出背景颜色
if(process_memory[0][k]0 && jprocess_memory[0][k]){发生置换前存块发生置换存块
label_memory[j][k]setBackground(ColorRED)
Optimal_lack++发生置换缺页数加
}else{
label_memory[j][k]setBackground(colors[j])
}
paneladd(label_memory[j][k])
}
LRU
for(int j1j
if(process_memory_LRU[k][j]1){存块空
label_memory_LRU[j][k]setText()
}else{
label_memory_LRU[j][k]setText(process_memory_LRU[k][j]+)process_memory[k][0]+
}
label_memory_LRU[j][k]setBounds(105+40*k (170+frist_y*20) + 20*j 30 20)
label_memory_LRU[j][k]setHorizontalAlignment(SwingConstantsCENTER)容居中
label_memory_LRU[j][k]setOpaque(true)设置透明显示出背景颜色
if(process_memory_LRU[k][0]0 && jprocess_memory_LRU[k][0]){发生置换前存块发生置换存块
label_memory_LRU[j][k]setBackground(ColorRED)
Optimal_lack_LRU++发生置换缺页数加
}else{
label_memory_LRU[j][k]setBackground(colors[j])
}
paneladd(label_memory_LRU[j][k])
}
FIFO
for(int j1j
label_memory_FIFO[j][k]setText()
}else{
label_memory_FIFO[j][k]setText(process_memory_FIFO[k][j]+)process_memory[k][0]+
}
label_memory_FIFO[j][k]setBounds(105+40*k (270+frist_y*20*2) + 20*j 30 20)
label_memory_FIFO[j][k]setHorizontalAlignment(SwingConstantsCENTER)容居中
label_memory_FIFO[j][k]setOpaque(true)设置透明显示出背景颜色
if(process_memory_FIFO[k][0]0 && jprocess_memory_FIFO[k][0]){发生置换前存块发生置换存块
label_memory_FIFO[j][k]setBackground(ColorRED)
Optimal_lack_FIFO++发生置换缺页数加
}else{
label_memory_FIFO[j][k]setBackground(colors[j])
}
paneladd(label_memory_FIFO[j][k])
}
if(k>13){输出超定范围时滚动条显示输出动态变化
scrollPanegetHorizontalScrollBar()setValue(40*(k13))设置滚动条水位置
}
panelrepaint()刷新面板
延时
try {
Threadsleep(500)
} catch (InterruptedException e1) {
e1printStackTrace()
}
thissetVisible(true)
}
button4setEnabled(true)动运行钮恢复点击
button5setEnabled(true)
textAreaappend(\n存块数+memory_num+\n页面数+page_num+\n佳置换算法缺页数+Optimal_lack+\n佳置换算法缺页率+(double)Optimal_lackpage_num+
\nLRU置换算法缺页数+Optimal_lack_LRU+\nLRU置换算法缺页率+(double)Optimal_lack_LRUpage_num +
\nFIFO置换算法缺页数+Optimal_lack_FIFO+\nFIFO置换算法缺页率+(double)Optimal_lack_FIFOpage_num + \n)
}
})start()
}
}else if(egetSource()button5){单步运行
获取存块数
memory_num slidergetValue()
佳置换算法
Optimal(page_int page_num memory_num process_memory)
if(page_num0){
JOptionPaneshowMessageDialog(this 先开源文件哦~)}
else{
Systemoutprintln(location)
if(location0){
removePanel()清空面板
for(int k0k
}
panelrepaint()刷新面板
Optimal_lack 0佳置换缺页数置零
Optimal_lack_LRU 0LRU缺页数置零
Optimal_lack_FIFO 0FIFO缺页数置零
}
根存块少动态调整显示标签位置(改变Y轴起始值)
int frist_y 0
if(memory_num>4){
frist_y memory_num 4
label5setBounds(10 190 + frist_y*20 70 30)
label6setBounds(10 290 + frist_y*20*2 70 30)
}else{
label5setBounds(10 190 70 30)
label6setBounds(10 290 70 30)
}
scrollPanegetHorizontalScrollBar()setValue(0)设置滚动条水位置
绘制页面置换情况
if(location
label_signsetHorizontalAlignment(SwingConstantsCENTER)容居中
paneladd(label_sign)
佳置换算法
for(int j1j
label_memory[j][location]setText()
}else{
label_memory[j][location]setText(process_memory[j][location]+)process_memory[k][0]+
}
label_memory[j][location]setBounds(105+40*location 70+20*j 30 20)
label_memory[j][location]setHorizontalAlignment(SwingConstantsCENTER)容居中
label_memory[j][location]setOpaque(true)设置透明显示出背景颜色
if(process_memory[0][location]0 && jprocess_memory[0][location]){发生置换前存块发生置换存块
label_memory[j][location]setBackground(ColorRED)
Optimal_lack++发生置换缺页数加
}else{
label_memory[j][location]setBackground(colors[j])
}
paneladd(label_memory[j][location])
}
LRU
for(int j1j
if(process_memory_LRU[location][j]1){存块空
label_memory_LRU[j][location]setText()
}else{
label_memory_LRU[j][location]setText(process_memory_LRU[location][j]+)process_memory[k][0]+
}
label_memory_LRU[j][location]setBounds(105+40*location (170+frist_y*20) + 20*j 30 20)
label_memory_LRU[j][location]setHorizontalAlignment(SwingConstantsCENTER)容居中
label_memory_LRU[j][location]setOpaque(true)设置透明显示出背景颜色
if(process_memory_LRU[location][0]0 && jprocess_memory_LRU[location][0]){发生置换前存块发生置换存块
label_memory_LRU[j][location]setBackground(ColorRED)
Optimal_lack_LRU++发生置换缺页数加
}else{
label_memory_LRU[j][location]setBackground(colors[j])
}
paneladd(label_memory_LRU[j][location])
}
FIFO
for(int j1j
if(process_memory_FIFO[location][j]1){存块空
label_memory_FIFO[j][location]setText()
}else{
label_memory_FIFO[j][location]setText(process_memory_FIFO[location][j]+)process_memory[k][0]+
}
label_memory_FIFO[j][location]setBounds(105+40*location (270+frist_y*20*2) + 20*j 30 20)
label_memory_FIFO[j][location]setHorizontalAlignment(SwingConstantsCENTER)容居中
label_memory_FIFO[j][location]setOpaque(true)设置透明显示出背景颜色
if(process_memory_FIFO[location][0]0 && jprocess_memory_FIFO[location][0]){发生置换前存块发生置换存块
label_memory_FIFO[j][location]setBackground(ColorRED)
Optimal_lack_FIFO++发生置换缺页数加
}else{
label_memory_FIFO[j][location]setBackground(colors[j])
}
paneladd(label_memory_FIFO[j][location])
}
if(location>13){输出超定范围时滚动条显示输出动态变化
scrollPanegetHorizontalScrollBar()setValue(40*(location13))设置滚动条水位置
}
panelrepaint()刷新面板
location++
}else{
textAreaappend(\n存块数+memory_num+\n页面数+page_num+\n佳置换算法缺页数+Optimal_lack+\n佳置换算法缺页率+(double)Optimal_lackpage_num+
\nLRU置换算法缺页数+Optimal_lack_LRU+\nLRU置换算法缺页率+(double)Optimal_lack_LRUpage_num +
\nFIFO置换算法缺页数+Optimal_lack_FIFO+\nFIFO置换算法缺页率+(double)Optimal_lack_FIFOpage_num + \n)
location 0
}
}
}else if(egetSource()button6){保存
if(saveFile null)文件存情况 创建文件
{
saveDiasetVisible(true)
String dirPath saveDiagetDirectory()
String fileName saveDiagetFile()
if(dirPath null || fileName null)
return
saveFile new File(dirPathfileName)
}
try
{
BufferedWriter bufw new BufferedWriter(new FileWriter(saveFile))
String text
for(int i0i
}
text text + *\n
}
bufwwrite(text)
bufwclose()
}
catch (IOException ex)
{
throw new RuntimeException(文件保存失败)
}
}
}
public static void main(String args[]) {
MyFrame landnew MyFrame()
}
public void run(){
死循环
while(true){
if(button_chose4){
for(int k0k<20k++){
label_signsetBounds(100+40*k 40 40 20)
label_signsetHorizontalAlignment(SwingConstantsCENTER)容居中
paneladd(label_sign)
for(int j0j<3j++){
label_memory[k][0] new JLabel(7)process_memory[k][0]+
label_memory[k][0]setBounds(100+40*k 80+20*0 40 20)
label_memory[k][0]setHorizontalAlignment(SwingConstantsCENTER)容居中
paneladd(label_memory[k][0])
}
延时
try {
Threadsleep(500)
} catch (InterruptedException e1) {
e1printStackTrace()
}
}
button_chose 0
}else {
}
}
}
***************************************佳置换算法***************************************************
存块结构体
class Memory{
int page 1页面号
int next 0该页面距次访问时间
}
判断页面否已存中
boolean isMemory(int page Memory memory[] int memory_num){参数(前访问页面号存块数组存块数)
boolean isTrue false
for(int i0i
isTrue true
}
return isTrue
}
判断存中否空位
int isNull(Memory memory[] int memory_num){参数(存块数组存块数)
int isNull 1
for(int i0i
isNull i
break
}
}
return isNull
}
页面距次访问时间
int nextAccess(int page_now int page[] int page_num int now){参数(前访问页面号访问页面数组访问页面总数前访问页面序数)
int access 0
for(int inow+1i
access inow
break
}
if(i (page_num1)){页面访问
access 99
}
}
return access
}
佳置换算法
int Optimal(int page[] int page_num int memory_num int process[][]){参数(访问页面数组访问页面总数存块数)
int max 未长时间访问页面距次访问时间
int max_page 未长时间访问页面号
int memory_null 存中空位号(没空位时1)
int lack 0 记录缺页次数
int[][] process new int[50][50]记录置换程
Memory[] memory new Memory[10] 创建存
记录置换程数组初始化
for(int a0a
}
}
存块初始化
for(int m0m
memory[m]page 1
memory[m]next 0
}
for(int i0i
if(isMemory(page[i] memory memory_num)){页面已存中
Systemoutprintln(页面已存中)
记录
process[0][i] 0没置换
for(int n1n
}
}else{ 页面存中
memory_null isNull(memory memory_num)获取存中空位号(没空位时1)
if(memory_null1){ [存中没空位]
获取存块中页面距次访问时间
for(int j0j
}
输出存块中页面距次访问时间
Systemoutprint(next)
for(int y0y
}
Systemoutprintln()
获取未长时间访问页面号
max memory[0]next
max_page 0
for(int j0j
max memory[j]next
max_page j
}
}
未长时间访问页面号置换新页面
Systemoutprintln(置换+memory[max_page]page+>+page[i])
memory[max_page]page page[i]置换
lack++更新缺页次数
记录
process[0][i] max_page+1发生置换
process[max_page+1][i] page[i]
for(int n1n
process[n][i] process[n][i1]
}
}
}else { [存中空位]
Systemoutprintln(存中空位+memory_null)
memory[memory_null]page page[i]
新页面放入存块空位中
for(int x0x
}
Systemoutprintln()
记录
process[0][i] 0没置换
process[memory_null+1][i] page[i]
for(int n1n
if(i>0){
process[n][i] process[n][i1]
}else{
process[n][i] 1
}
}
}
}
}
}
Systemoutprintln(\n缺页数+lack)
Systemoutprintln(缺页率+(double)lackpage_num)
Systemoutprintln(佳页面置换算法置换表)
for(int a0a
}
Systemoutprintln()
}
return 0
}
*************************************************************************************************
}
文档香网(httpswwwxiangdangnet)户传
《香当网》用户分享的内容,不代表《香当网》观点或立场,请自行判断内容的真实性和可靠性!
该内容是文档的文本内容,更好的格式请下载文档