123.png
基本框架
屏幕快照 2017-11-24 12.41.42.png
page.java
//page类
package pagedStorageManagement;
public class page {
private int pageNumber;//页号
private int physicsNumber;//物理页号
private boolean state;//状态位
private int visitCount;//访问字段
private boolean change;//修改位
private int CRTAddress;//外存地址
public page()
{
this.pageNumber = -1;
this.physicsNumber = -1;
this.state = false;
this.visitCount = 0;
this.change = false;
this.CRTAddress = -1;
}
public page( int pageNumber, int physicsNumber, boolean state, int visitCount, boolean change, int CRTAddress)
{
this.pageNumber = pageNumber;
this.physicsNumber = physicsNumber;
this.state = state;
this.visitCount = visitCount;
this.change = change;
this.CRTAddress = CRTAddress;
}
public void setPageNumber( int pageNumber )
{
this.pageNumber = pageNumber;
}
public void setPhysicsNumber( int physicsNumber )
{
this.physicsNumber = physicsNumber;
}
public void setState (boolean state )
{
this.state=state;
}
public void setVisitCount( int visitCount )
{
this.visitCount = visitCount;
}
public void setChange( boolean change )
{
this.change = change;
}
public void setCRTAddress( int CRTAddress )
{
this.CRTAddress = CRTAddress;
}
public int getPageNumber( )
{
return this.pageNumber;
}
public int getPhysicsNumber()
{
return this.physicsNumber;
}
public boolean getState()
{
return this.state;
}
public int getVisitCount()
{
return this.visitCount;
}
public boolean getChange()
{
return this.change;
}
public int getCRTAddress()
{
return this.CRTAddress;
}
}
shell.java
//shell类
package pagedStorageManagement;
import java.util.Scanner;
public class shell {
page shell[];
private int current;
private int length;
public shell(){};
public shell( int length )
{
this.length = length;
this.current = 0;
shell = new page[length];
for( int i = 0; i < length ; i++ )
{
this.shell[i] = new page();
}
}
public void setCurrent( int current )
{
this.current = current;
}
public int getCurrent()
{
return this.current;
}
public int searchPage( int pageNumber )
{
int i = 0;
if( this.current == 0 )
{
return -2;
}
else
{
while( i < this.current )
{
if( this.shell[i].getPageNumber() == pageNumber )
{
return i;
}
i++;
}
return -1;
}
}
public void inChange( int b[], String ch, int number )
{
Scanner a = new Scanner( System.in );
switch( ch )
{
case "yes":
{
System.out.println("请输入一个新的数据");
b[this.shell[number].getPhysicsNumber() ] = a.nextInt();
this.shell[number].setChange( true );
System.out.println("修改成功");
break;
}
case "no":
{
break;
}
default:
{
System.out.println("输入字符有误,将退出程序!");
System.exit(0);
}
}
}
public int isOver()
{
if( this.current >= this.length )
{
return 1;
}
else
return 0;
}
public int minVisitCount()
{
int i,t=0;
for (i=1; i= 20 || k1 < 0 )
{
System.out.println("输入数据有错,将退出程序!");
System.exit(0);
}
//检测快表,快表存储当前的页表项,即当快表满时采用最近最久未被使用算法置换快表
System.out.println("进入快表检测");
if( TLB.getCurrent() > 0 )
{
number = TLB.searchPage(k1);
if( number!=-1 && number!=-2 )
{
result = b[TLB.shell[number].getPhysicsNumber()];
System.out.println("在快表中找到,结果为:" + result );
//找出该页号在页表中的位置并修改访问字段
number = TLB.shell[number].getCRTAddress();
pageShell.shell[number].setVisitCount(pageShell.shell[number].getVisitCount()+1 );
}
}
if( TLB.getCurrent() <= 0 || number == -1 )
{
System.out.println("在快表中找不到!" + "进入内存检测:");
//在快表中找不到,去内存区的页表找
if( pageShell.current>0 )
{
number = pageShell.searchPage(k1);//页号k1所在的下标
if( number !=-1 && number!=-2 )
{
result = b[pageShell.shell[number].getPhysicsNumber()];
System.out.println("在页表中找到,结果为:" + result );
//修改访问字段和状态位
pageShell.shell[number].setVisitCount(pageShell.shell[number].getVisitCount()+1);
//修改快表
TLB.changeKShell( pageShell,number );
}
}
if( pageShell.current <= 0 || number== -1 )
{
System.out.println("在内存中找不到!");
System.out.println("从外存中调入内存:");
//在页表找不到,去外存区找
for( i=0; i6 )
{
System.out.println("输入有误,请重新输入(1-6):");
i = a.nextInt();
}
pageShell.dealFunction( i,TLB,s,b );
}while( i != 5 );
}while(i==5);
System.out.println("退出程序!");
}
}
KShell.java
//KShell类
package pagedStorageManagement;
public class KShell
{
page shell[];
private int current;
private int length;
private int changeNumber;//修改快表的次数
public KShell(){};
public KShell( int length )
{
this.length = length;
this.current = 0;
this.changeNumber = 0;
shell = new page[length];
for (int i=0; i= this.getLength() )
{
if ( this.getCurrent() == this.getLength() )
{
this.setCurrent(0);
}
System.out.println("快表已满,快表中即将调出页号" + this.shell[ this.current ].getPageNumber() );
}
if ( this.getCurrent() < this.getLength() )
{
this.shell[this.getCurrent()].setCRTAddress(number);
this.shell[this.getCurrent()].setPageNumber(pageShell.shell[number].getPageNumber());
this.shell[this.getCurrent()].setPhysicsNumber(pageShell.shell[number].getPhysicsNumber());
this.setCurrent( this.getCurrent()+1 );
this.setChangeNumber( this.getChangeNumber() + 1 );
}
}
public void printKShell()
{
System.out.println("快表:");
System.out.println("索引\t"+"页号\t"+"物理块号\t"+"在页表下的索引");
for( int i=0; i
source.java
//source类
package pagedStorageManagement;
public class source {
private int pageNumber;//页号
private int sts;//数据
private int length;
public source(){};
public source( int pageNumber , int sts )
{
this.pageNumber = pageNumber;
this.sts = sts;
}
public void setPageNumber( int pageNumber )
{
this.pageNumber = pageNumber;
}
public void setSts( int sts )
{
this.sts = sts;
}
public int getPageNumber()
{
return pageNumber;
}
public int getSts()
{
return sts;
}
public void printSource( int i )
{
System.out.println( i + "\t" + this.pageNumber + "\t" + this.sts + "\n" );
}
}
使用说明
本程序是使用java语言编写的实现请求分页存储管理的程序,是在eclipise下运行的结果,只需运行Shell类就行了。
首先先设置外存,页表和快表的大小,然后输入一个请求访问的页面。
测试样例
屏幕快照 2017-11-24 12.52.04.png
屏幕快照 2017-11-24 12.53.05.png
屏幕快照 2017-11-24 12.53.26.png
屏幕快照 2017-11-24 12.53.43.png
源码下载
请点击右侧地址:Paged Storage Management
#个人主页:www.iooy.com