模拟操作系统的请求分页存储管理JAVA代码
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
System.out.println("请输入一个页面的页号(0-19):"); k1=a.nextInt(); if(k1>=20||k1<0){
System.out.println("输入数据有错,将退出程序!!"); System.exit(0); }
//检测快表,快表存储当前的页表项,即当快表满时采用最近最久未被使用算法置换快表 System.out.println("进入快表检测"); if(TLB.getCurrent()>0){
return this.change; } public int getCRTaddress(){
return this.CRTaddress; } }
(2)Shell类:页表 package homework;
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 pagenumb){
} } public void programFunction(){
System.out.println("****************************请求分页存储系统 ****************************");
System.out.println("功能:"); System.out.println("\t 1.查看页表"); System.out.println("\t 2.查看快表"); System.out.println("\t 3.查看外存"); System.out.println("\t 4.在内存修改数据"); System.out.println("\t 5.继续访问页面"); System.out.println("\t 6.退出程序"); } public void Dealfunction(int i,KShell TLB,Source s[],int b[]){ if(i==1){
s[k].printSource(k); } }else if(i==4){ String ch="yes";
int pageNumb; Scanner a=new Scanner(System.in); System.out.print("请输入一个页号:"); pageNumb=a.nextInt(); int numb=this.searchpage(pageNumb); if(numb<0){
} public void setCRTaddress(int CRTaddress){
this.CRTaddress=CRTaddress; } public int getPagenumb(){
return this.pagenumb; } public int getPhysicsnumb(){
请求分页存储管理的实现
写出方案实现代码 (1)page 类
package homework;
public class Page { private int pagenumb;//页号 private int physicsnumb;//物理块号 private boolean state;//状态位 private int visitcount;//访问字段 private boolean change;//修改位 private int CRTaddress;//外存地址
System.out.println(i+"\t"+this.shell[i].getPagenumb()+"\t"+this.shell[i] .getPhysicsnumb()+"\t"+this.shell[i].getState()+"\t"+this.shell[i].getVisit count()+"\t"+this.shell[i].getChange()+"\t"+this.shell[i].getCRTaddress());
System.out.println("内存中没有此页号"); }else{
this.Inchange(b, ch,numb); } }else if(i==6){ System.out.println("结束程序"); System.exit(0); } }
public static void main(String[] args){
System.out.print("请输入外存大小:"); slength=a.nextInt(); System.out.print("请输入页表大小:"); plength=a.nextInt(); System.out.print("请输入快表大小:"); Tlength=a.nextInt(); //定义页表,快表,外存 Shell pageshell=new Shell(plength);//页表 Source s[]=new Source[slength];//外存 KShell TLB=new KShell(Tlength);//快表 System.out.println("产生一个随机序列作为外存数据!"); //录入外存地址和数据 for(i=0;i<slength;i++){
number=TLB.searchpage(k1); if(number!=-1&&number!=-2){
result=b[TLB.shell[number].getPhysicsnumb()]; System.out.println("在快表中找到,结果为:"+result); //找出该页号在页表中的位置并修改访问字段 number=TLB.shell[number].getIndex();
this.pagenumb=pagenumb; this.physicsnumb=physicsnumb; this.state=state; this.visitcount=visitcount; this.change=change; this.CRTaddress=CRTaddress; } public void setPagenumb(int pagenumb){ this.pagenumb=pagenumb; } public void setPhysicsnumb(int physicsnumb){ this.physicsnumb=physicsnumb; } public void setState(boolean state){ this.state=state; } public void setVisitcount(int visitcount){ this.visitcount=visitcount; } public void setChange(boolean change){ this.change=change;
data=(int)(100*Math.random()); System.out.print(data+"\t"); s[i]=new Source(i,data); }
System.out.println("\n外存设置成功"); //请求页面 do{
//TLB.printKShell();//打印当前快表的情况 //pageshell.printPageShell();//打印当前页表的情况
this.printPageShell(); }else if(i==2){
TLB.printKShell(); }else if(i==3){
System.out.println("外存:"); System.out.println("外存地址\t"+"页号\t"+"数据\n"); for(int k=0;k<20;k++){
public Page(){ this.pagenumb=-1; this.physicsnumb=-1; this.state=false; this.visitcount=0; this.change=false; this.CRTaddress=-1;
} public Page(int pagenumb,int physicsnumb,boolean state,int visitcount,boolean change,int CRTaddress){
t=i; }
} return t; } public int Ischange(int number){ if(this.shell[number].getChange()==true){
return 1; } else return 0; } public void printPageShell(){ System.out.println("页表:"); System.out.println("索引\t"+"页号\t"+"物理块号\t"+"状态\t"+"访问次数 \t"+"修改\t"+"外存地址\t"); for(int i=0;i<this.length;i++){
Scanner a=new Scanner(System.in); int i,number=-10,k1,k2,result; int k3=0;//当前存储的内存地址 int t;//页表中访问次数最小的索引 int b[]=new int[10];//内存中存储的数据 String ch; int slength,plength,Tlength,data;
return this.physicsnumb; } public boolean getState(){
return this.state; } public int getVisitcount(){
return this.visitcount; } public boolean getChange(){
int i=0; if(this.current==0){
return -2; } else{
while(i<this.current){ if(this.shell[i].getPagenumb()==pagenumb){百度文库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].getPhysicsnumb()]=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<this.current;i++){ if(this.shell[i].getVisitcount()<this.shell[t].getVisitcount()){
System.out.println("输入数据有错,将退出程序!!"); System.exit(0); }
//检测快表,快表存储当前的页表项,即当快表满时采用最近最久未被使用算法置换快表 System.out.println("进入快表检测"); if(TLB.getCurrent()>0){
return this.change; } public int getCRTaddress(){
return this.CRTaddress; } }
(2)Shell类:页表 package homework;
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 pagenumb){
} } public void programFunction(){
System.out.println("****************************请求分页存储系统 ****************************");
System.out.println("功能:"); System.out.println("\t 1.查看页表"); System.out.println("\t 2.查看快表"); System.out.println("\t 3.查看外存"); System.out.println("\t 4.在内存修改数据"); System.out.println("\t 5.继续访问页面"); System.out.println("\t 6.退出程序"); } public void Dealfunction(int i,KShell TLB,Source s[],int b[]){ if(i==1){
s[k].printSource(k); } }else if(i==4){ String ch="yes";
int pageNumb; Scanner a=new Scanner(System.in); System.out.print("请输入一个页号:"); pageNumb=a.nextInt(); int numb=this.searchpage(pageNumb); if(numb<0){
} public void setCRTaddress(int CRTaddress){
this.CRTaddress=CRTaddress; } public int getPagenumb(){
return this.pagenumb; } public int getPhysicsnumb(){
请求分页存储管理的实现
写出方案实现代码 (1)page 类
package homework;
public class Page { private int pagenumb;//页号 private int physicsnumb;//物理块号 private boolean state;//状态位 private int visitcount;//访问字段 private boolean change;//修改位 private int CRTaddress;//外存地址
System.out.println(i+"\t"+this.shell[i].getPagenumb()+"\t"+this.shell[i] .getPhysicsnumb()+"\t"+this.shell[i].getState()+"\t"+this.shell[i].getVisit count()+"\t"+this.shell[i].getChange()+"\t"+this.shell[i].getCRTaddress());
System.out.println("内存中没有此页号"); }else{
this.Inchange(b, ch,numb); } }else if(i==6){ System.out.println("结束程序"); System.exit(0); } }
public static void main(String[] args){
System.out.print("请输入外存大小:"); slength=a.nextInt(); System.out.print("请输入页表大小:"); plength=a.nextInt(); System.out.print("请输入快表大小:"); Tlength=a.nextInt(); //定义页表,快表,外存 Shell pageshell=new Shell(plength);//页表 Source s[]=new Source[slength];//外存 KShell TLB=new KShell(Tlength);//快表 System.out.println("产生一个随机序列作为外存数据!"); //录入外存地址和数据 for(i=0;i<slength;i++){
number=TLB.searchpage(k1); if(number!=-1&&number!=-2){
result=b[TLB.shell[number].getPhysicsnumb()]; System.out.println("在快表中找到,结果为:"+result); //找出该页号在页表中的位置并修改访问字段 number=TLB.shell[number].getIndex();
this.pagenumb=pagenumb; this.physicsnumb=physicsnumb; this.state=state; this.visitcount=visitcount; this.change=change; this.CRTaddress=CRTaddress; } public void setPagenumb(int pagenumb){ this.pagenumb=pagenumb; } public void setPhysicsnumb(int physicsnumb){ this.physicsnumb=physicsnumb; } public void setState(boolean state){ this.state=state; } public void setVisitcount(int visitcount){ this.visitcount=visitcount; } public void setChange(boolean change){ this.change=change;
data=(int)(100*Math.random()); System.out.print(data+"\t"); s[i]=new Source(i,data); }
System.out.println("\n外存设置成功"); //请求页面 do{
//TLB.printKShell();//打印当前快表的情况 //pageshell.printPageShell();//打印当前页表的情况
this.printPageShell(); }else if(i==2){
TLB.printKShell(); }else if(i==3){
System.out.println("外存:"); System.out.println("外存地址\t"+"页号\t"+"数据\n"); for(int k=0;k<20;k++){
public Page(){ this.pagenumb=-1; this.physicsnumb=-1; this.state=false; this.visitcount=0; this.change=false; this.CRTaddress=-1;
} public Page(int pagenumb,int physicsnumb,boolean state,int visitcount,boolean change,int CRTaddress){
t=i; }
} return t; } public int Ischange(int number){ if(this.shell[number].getChange()==true){
return 1; } else return 0; } public void printPageShell(){ System.out.println("页表:"); System.out.println("索引\t"+"页号\t"+"物理块号\t"+"状态\t"+"访问次数 \t"+"修改\t"+"外存地址\t"); for(int i=0;i<this.length;i++){
Scanner a=new Scanner(System.in); int i,number=-10,k1,k2,result; int k3=0;//当前存储的内存地址 int t;//页表中访问次数最小的索引 int b[]=new int[10];//内存中存储的数据 String ch; int slength,plength,Tlength,data;
return this.physicsnumb; } public boolean getState(){
return this.state; } public int getVisitcount(){
return this.visitcount; } public boolean getChange(){
int i=0; if(this.current==0){
return -2; } else{
while(i<this.current){ if(this.shell[i].getPagenumb()==pagenumb){百度文库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].getPhysicsnumb()]=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<this.current;i++){ if(this.shell[i].getVisitcount()<this.shell[t].getVisitcount()){