📄 storagemanage.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.Rectangle;
import java.awt.event.*;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.table.*;
import javax.swing.table.AbstractTableModel;
import java.util.Vector;
public class StorageManage extends JFrame implements ActionListener{
float absent=0;
float testTime=0;
Vector v;
Vector v1;
JPanel o=new JPanel(new GridLayout(1,7));
String pnn=" ";
String pmm=" ";
String rr=" ";
JLabel lA=new JLabel("模拟内存");
JLabel lB=new JLabel("页表");
JLabel lC=new JLabel("堆栈");
JLabel lD=new JLabel("");
JLabel lE=new JLabel("");
JLabel lF=new JLabel("");
JLabel lG=new JLabel("");
JLabel lH=new JLabel("");
JLabel l2=new JLabel("页号: "+pnn);
JLabel l3=new JLabel("页内地址:"+pmm);
JLabel lJ=new JLabel("访问次数:"+" 0");
JLabel lI=new JLabel("缺页次数:"+" 0");
JLabel l4=new JLabel("缺 页 率:"+rr);
JLabel l5=new JLabel("块大小");
JLabel l6=new JLabel("页表长度");
JLabel l7=new JLabel("堆栈长度");
JLabel l8=new JLabel("逻辑地址");
JLabel l9=new JLabel("算 法");
String c11[]={"1024","2048","3072","4096","5120","6144","7168","8129","9216","10240"};
public JComboBox c1 = new JComboBox(c11);
public JTextField t2=new JTextField(5);
public JTextField t3=new JTextField(5);
public JTextField t4=new JTextField(5);
String c22[]={"FIFO","LRU"};
public JComboBox c2 = new JComboBox(c22);
public JButton b0=new JButton("定制数据");
public JButton b1=new JButton("请求分页");
public JButton b2=new JButton("结束测试");
JPanel p1=new JPanel(new GridLayout(1,5));
JPanel p2=new JPanel();
JPanel p0=new JPanel(new BorderLayout());
JPanel p3=new JPanel(new BorderLayout());
JPanel p4=new JPanel(new GridLayout(5,1));
JPanel p5=new JPanel(new GridLayout(4,2));
JPanel p6=new JPanel(new GridLayout(4,1));
JPanel p7=new JPanel(new BorderLayout());
String algorithm;
String bb;
int b;
int t=0;
int s=0;
String add;
int a;
int n;
Integer memory[][]=new Integer[8][8];
Random r=new Random();
int fillin=0;
int bn=0;
int init;
int isremovePn=0;
int removePn;
int clear=0;
Timer timer;
Timer timer1;
Timer timer2;
public StorageManage() {
for(int i=0;i<8;i++)
for(int j=0;j<8;j++){
memory[i][j]=0;
}
int sign;
init=1;
v=new Vector();
v1=new Vector();
b1.setEnabled(false);
p4.add(l2);
p4.add(l3);
p4.add(lJ);
p4.add(lI);
p4.add(l4);
p5.add(l9);
p5.add(c2);
p5.add(l5);
p5.add(c1);
p5.add(l6);
p5.add(t2);
p5.add(l7);
p5.add(t3);
p6.add(l8);
p6.add(t4);
p6.add(b1);
p6.add(b2);
p7.add(p5,BorderLayout.NORTH);
p7.add(b0,BorderLayout.CENTER);
p2.add(p0);
p2.add(p3);
p2.add(p4);
p2.add(p7);
p2.add(p6);
o.add(lH);
o.add(lA);
o.add(lD);
o.add(lB);
o.add(lC);
o.add(lE);
o.add(lF);
o.add(lG);
this.getContentPane().add(o,BorderLayout.NORTH);
this.getContentPane().add(p2,BorderLayout.EAST);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b0){
if(t2.getText().length()==0||t3.getText().length()==0){
if(t2.getText().length()==0){
JOptionPane.showMessageDialog(this,"you do not input the number of page!","error",JOptionPane.ERROR_MESSAGE );
}
if(t3.getText().length()==0){
JOptionPane.showMessageDialog(this,"you do not input the size of the stack!","error",JOptionPane.ERROR_MESSAGE );
}
clear=1;
}
else{
bb=(String)c1.getSelectedItem();
algorithm=(String)c2.getSelectedItem();
b=Integer.valueOf(bb).intValue();
t=Integer.valueOf(t2.getText()).intValue();
s=Integer.valueOf(t3.getText()).intValue();
while(bn<s){
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
if(j==0)
memory[i][j]=1;
else
memory[i][j]=new Integer(r.nextInt(2));
}
}
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
if(memory[i][j]==0) bn++;
}
}
}
b0.setEnabled(false);
c1.setEnabled(false);
c2.setEnabled(false);
b1.setEnabled(true);
this.repaint();
}
}
if(e.getSource()==b1){
testTime++;
if(t4.getText().length()==0){
JOptionPane.showMessageDialog(this,"you do not input the logical address!","error",JOptionPane.ERROR_MESSAGE );
}
else{
add=t4.getText();
a=Integer.parseInt(add,16);
n=(int)a/b;
if(n>=t){
JOptionPane.showMessageDialog(this,"outofbands of the address!","error",JOptionPane.ERROR_MESSAGE );
}
else{
fillin=1;
int m=a-n*b;
pnn=Integer.toString(n);
pmm=Integer.toString(m);
l2.setText("页号: "+pnn);
l3.setText("页内地址: "+pmm);
if(!v.contains(n)){
if(v.size()<s){
int i=0;
int j=0;
for(i=0;i<8;i++){
for(j=0;j<8;j++){
if(memory[j][i]==0){
break;
}
}
if(j<8)
break;
}
memory[j][i]=1;
bn=i*8+j;//suan kuai hao
v1.add(new Info(n,bn));//fang ru ye biao
v.add(n);//fang ru dui zhan
}
else {
int i=0;
while(i<v1.size()){
if(((Info)v1.elementAt(i)).getPn()==(removePn=(Integer)v.remove(0))){
v1.add(new Info(n,((Info)v1.remove(i)).getBn()));isremovePn=1;break;
}
else i++;
}
v.add(n);
}
absent++;
lJ.setText("访问次数: "+(int)testTime);
lI.setText("缺页次数: "+(int)absent);
l4.setText("缺 页 率: "+((absent/testTime)*100)+"%");
this.repaint();
}
else{
int w=0;
while(w<v1.size()){
if(((Info)v1.elementAt(w)).getPn()==n){
bn=((Info)v1.elementAt(w)).getBn();break;
}
else w++;
}
if(algorithm=="FIFO"){
}
if(algorithm=="LRU"){
int i=0;
while(i<v.size()){
if((Integer)v.elementAt(i)==n){
v.remove(i);
v.add(n);
break;
}
}
}
lJ.setText("访问次数: "+(int)testTime);
lI.setText("缺页次数: "+(int)absent);
l4.setText("缺 页 率: "+((absent/testTime)*100)+"%");
this.repaint();
}
}
}
}
if(e.getSource()==b2){
timer.stop();timer1.stop();
t2.setText("");
t3.setText("");
t4.setText("");
b0.setEnabled(true);
c1.setEnabled(true);
c2.setEnabled(true);
b1.setEnabled(false);
for(int i=0;i<8;i++)
for(int j=0;j<8;j++){
memory[i][j]=0;
}
clear=1;
bn=0;
l2.setText("页号: "+" ");
l3.setText("页内地址: "+" ");
l4.setText("缺 页 率: "+" ");
lJ.setText("访问次数: "+" 0");
lI.setText("缺页次数: "+" 0");
v.removeAllElements();
v1.removeAllElements();
this.repaint();
}
}
public void paint(Graphics g) {
final Graphics g1=g;
for(int i=0;i<8;i++)
for(int j=0;j<8;j++){
if(memory[i][j]==0)
g.setColor(Color.green);
else g.setColor(Color.red);
g.fill3DRect(30+30*i, 60+30*j, 28, 28, true);
}
//timer.add();;
if(t!=0||s!=0){//画出页表与堆栈
g.setColor(Color.blue);
for(int j=0;j<t;j++){
g.drawString(new Integer(j).toString(),310, 80+30*j);
}
for(int i=0;i<2;i++)
for(int j=0;j<t;j++){
g.draw3DRect(330+30*i, 60+30*j, 30, 30, true);
}
g.setColor(Color.black);
if(init==1){
for(int j=0;j<t;j++){
g.drawString(new Integer(0).toString(),370, 80+30*j);
}
init=0;
}
g.setColor(Color.blue);
for(int j=0;j<s;j++){
g.drawString(new Integer(j).toString(),410, 80+30*(s-1-j));
}
for(int i=0;i<s;i++)
g.draw3DRect(430, 60+30*i, 30, 30, true);
}
if(fillin==1){//更新页表和堆栈
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
memory[bn%8][bn/8]=0;
repaint();
}
};
ActionListener taskPerformer1 = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
memory[bn%8][bn/8]=1;
repaint();
}
};
ActionListener taskPerformer2 = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
timer.stop();
timer1.stop();
timer2.stop();
}
};
timer=new Timer(500,taskPerformer);
timer1=new Timer(500,taskPerformer1);
timer1.setInitialDelay(600);
timer2=new Timer(2000,taskPerformer2);
timer.start();
timer1.start();
timer2.start();
g.setColor(Color.black);
if(isremovePn==1){
g.clearRect(361,31+30*(removePn+1) ,28 ,28 );
g.drawString(new Integer(0).toString(),370,50+30*(removePn+1));
isremovePn=0;
}
for(int i=0;i<v1.size();i++){//更新页表
g.drawString(new Integer(((Info)v1.elementAt(i)).getBn()).toString(),340,50+30*((((Info)v1.elementAt(i)).getPn())+1));
g.clearRect(361,31+30*(n+1) ,28 ,28 );
g.drawString(new Integer(1).toString(),370,50+30*(n+1));
}
for(int i=0;i<v.size();i++){//更新堆栈
g.clearRect(431,31+30*(s-i) ,28 ,28 );
g.drawString(v.elementAt(i).toString(),440,50+30*(s-i));
}
}
if(clear==1){
g.clearRect(300,30 ,161 ,30*(t+1)+1);
clear=0;
}
fillin=0;
o.updateUI();
p2.updateUI();
}
public static void main(String args[]) {
StorageManage sm=new StorageManage();
sm.setSize(850,350);
sm.setVisible(true);
}
class Info{
int pn;
int bn;
public Info(int pn,int bn){
this.pn=pn;
this.bn=bn;
}
public int getPn(){
return pn;
}
public int getBn(){
return bn;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -