操作系统课程设计银行家算法和页面置换算法Word文档格式.docx
《操作系统课程设计银行家算法和页面置换算法Word文档格式.docx》由会员分享,可在线阅读,更多相关《操作系统课程设计银行家算法和页面置换算法Word文档格式.docx(41页珍藏版)》请在冰豆网上搜索。
this.setLayout(newjava.awt.GridLayout(3,1));
panel1=newJPanel();
panel2=newJPanel();
panel3=newJPanel();
Fontf=newFont("
楷体"
1,60);
Fontf1=newFont("
宋体"
3,30);
Fontf2=newFont("
幼圆"
1,15);
label1=newJLabel("
label1.setFont(f);
label2=newJLabel("
算法模拟题目:
"
label2.setFont(f1);
panel1.add(label1);
panel2.add(label2);
ButtonGroupbgroup=newButtonGroup();
b1=newJRadioButton("
银行家算法"
true);
b1.setFont(f1);
bgroup.add(b1);
b2=newJRadioButton("
页面置换算法"
b2.setFont(f1);
bgroup.add(b2);
panel2.add(b1);
panel2.add(b2);
button1=newJButton("
开始演示"
button1.setFont(f2);
button1.addActionListener(this);
panel3.add(button1);
this.add(panel1);
this.add(panel2);
this.add(panel3);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
publicvoidactionPerformed(ActionEvente)
if(e.getSource()==button1)
{
if(b1.isSelected())
{
newBanker();
}
if(b2.isSelected())
{
newYemian();
}
publicstaticvoidmain(String[]args)
try{
for(LookAndFeelInfoinfo:
UIManager.getInstalledLookAndFeels()){
if("
Nimbus"
.equals(info.getName())){
UIManager.setLookAndFeel(info.getClassName());
break;
catch(Exceptione1)
{}
newMainFrame();
}
Banker.java:
importjava.awt.*;
importjava.awt.event.*;
importjava.util.ArrayList;
importjavax.swing.*;
importjavax.swing.table.DefaultTableCellRenderer;
importjavax.swing.table.DefaultTableModel;
publicclassBankerextendsJFrameimplementsActionListener
privateJTabletable1,table2;
privateJPanelp0,p1,p11,p12,p13,p14,p2,p3,p31,p32,p33,p34,p4,p5;
privateJLabelt1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11;
privateJButtonb1,b2,b3,b4,b5,b6;
privateTextFieldtext01,text02,text03,text04,text05,text06;
//编辑框
privateJTextField[]text1,text2,text3,text4,text5;
//进程组的数据
DefaultTableModeltablemodel,tablemodel1;
ArrayList<
Integer>
list;
int[][]claim,allocation,need;
int[]available;
int[]request,temavail,release;
boolean[]possible;
intm,n,index=0;
publicBanker()
{
super("
银行家算法模拟"
p0=newJPanel();
p1=newJPanel();
p11=newJPanel();
p12=newJPanel();
p13=newJPanel();
p14=newJPanel();
p2=newJPanel();
p3=newJPanel();
p31=newJPanel();
p32=newJPanel();
p33=newJPanel();
p34=newJPanel();
p4=newJPanel();
p5=newJPanel();
p0.setLayout(newGridLayout(5,1));
p1.setLayout(newGridLayout(4,1));
p3.setLayout(newGridLayout(4,1));
p1.add(p11);
p1.add(p12);
p1.add(p13);
p1.add(p14);
p3.add(p31);
p3.add(p32);
p3.add(p33);
p3.add(p34);
p0.add(p1);
p0.add(p2);
p0.add(p3);
p0.add(p4);
p0.add(p5);
t1=newJLabel("
进程数"
t2=newJLabel("
资源数"
t3=newJLabel("
进程号"
t4=newJLabel("
已分配资资源:
t5=newJLabel("
资源最大需求:
t6=newJLabel("
可用资源:
t7=newJLabel("
请求资源进程号"
t8=newJLabel("
请求资源为"
t9=newJLabel("
释放资源"
t10=newJLabel("
安全序列"
t11=newJLabel("
释放资源进程号"
b1=newJButton("
确定"
b2=newJButton("
添加"
b3=newJButton("
b4=newJButton("
请求"
b5=newJButton("
开始检测"
b6=newJButton("
释放"
text1=newJTextField[6];
text2=newJTextField[6];
text3=newJTextField[6];
text4=newJTextField[6];
text5=newJTextField[6];
for(inti=0;
i<
5;
i++)
text1[i]=newJTextField(4);
text2[i]=newJTextField(4);
text3[i]=newJTextField(4);
text4[i]=newJTextField(4);
text5[i]=newJTextField(4);
text01=newTextField(4);
text02=newTextField(4);
text03=newTextField(4);
text04=newTextField(4);
text05=newTextField(4);
text06=newTextField(20);
String[]columnNames1={"
"
allocation"
"
claim"
need"
available"
};
tablemodel=newDefaultTableModel(columnNames1,0);
table1=newJTable(tablemodel);
table1.setPreferredScrollableViewportSize(newDimension(700,200));
table1.setRowHeight(20);
table1.doLayout();
DefaultTableCellRendererr=newDefaultTableCellRenderer();
r.setHorizontalAlignment(JLabel.CENTER);
table1.setDefaultRenderer(Object.class,r);
JScrollPanepane1=newJScrollPane(table1);
p11.add(t1);
p11.add(text01);
p11.add(t2);
p11.add(text02);
p11.add(b1);
p12.add(t3);
p12.add(text03);
p12.add(b2);
p13.add(t4);
p13.add(text1[i]);
p14.add(t5);
p14.add(text2[i]);
p2.add(pane1);
p31.add(t6);
p31.add(text3[i]);
p31.add(b3);
p32.add(t7);
p32.add(text04);
p32.add(t8);
p32.add(text4[i]);
p32.add(b4);
p33.add(t11);
p33.add(text05);
p33.add(t9);
for(inti=0;
p33.add(text5[i]);
p33.add(b6);
p34.add(b5);
String[]columnNames2={"
currentavail"
curr...+allo..."
possible"
tablemodel1=newDefaultTableModel(columnNames2,0);
table2=newJTable(tablemodel1);
table2.setPreferredScrollableViewportSize(newDimension(700,200));
table2.setRowHeight(20);
table2.doLayout();
DefaultTableCellRendererr1=newDefaultTableCellRenderer();
r1.setHorizontalAlignment(JLabel.CENTER);
table2.setDefaultRenderer(Object.class,r1);
JScrollPanepane2=newJScrollPane(table2);
p4.add(pane2);
p5.add(t10);
p5.add(text06);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
p0.setBackground(Color.lightGray);
list=newArrayList<
();
this.setContentPane(p0);
this.setVisible(true);
this.pack();
this.setLocation(300,10);
this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
if(e.getSource()==b1)
m=Integer.parseInt(text01.getText());
}catch(NumberFormatExceptione1)
{
JOptionPane.showMessageDialog(this,"
进程数不能为空"
return;
}
n=Integer.parseInt(text02.getText());
资源数不能为空"
claim=newint[m][n];
need=newint[m][n];
allocation=newint[m][n];
available=newint[n];
request=newint[n];
release=newint[n];
for(inti=0;
5-n;
p13.remove(text1[4-i]);
p13.updateUI();
p14.remove(text2[4-i]);
p14.updateUI();
p31.remove(text3[4-i]);
p31.updateUI();
p32.remove(text4[4-i]);
p32.updateUI();
p33.remove(text5[4-i]);
p33.updateUI();
temavail=newint[n];
Stringstr[]={"
m;
tablemodel1.addRow(str);
if(e.getSource()==b2)
Stringname[]=newString[m];
if(index>
=m)
JOptionPane.showMessageDialog(this,"
进程个数已满"
return;
try{
for(intj=0;
j<
n;
j++)
allocation[index][j]=Integer.parseInt(text1[j].getText());
claim[index][j]=Integer.parseInt(text2[j].getText());
need[index][j]=claim[index][j]-allocation[index][j];
}catch(Exceptionf){}
name[index]="
P"
+index;
Stringstrd[]={"
strd[0]=name[index];
for(intj=0;
j++)
{
strd[1]+=allocation[index][j]+"
"
;
strd[2]+=claim[index][j]+"
strd[3]+=need[index][j]+"
strd[4]="
tablemodel.addRow(strd);
index++;
if(e.getSource()==b3)
{
for(inti=0;
available[i]=Integer.parseInt(text3[i].getText());
temavail[i]=available[i];
}catch(NumberFormatExceptionf)
JOptionPane.showMessageDialog(this,"
可用资源不能为空,请重新输入"
return;
Stringstr="
str+=available[i]+"
tablemodel.setValueAt(str,0,4);
if(e.getSource()==b5)
list.clear();
safe();
table1.getRowCount();
tablemodel1.removeRow(0);
update_table2();
if(e.getSource()==b4)
try{
request[i]=Integer.parseInt(text4[i].getText());
}catch(NumberFormatExceptionf1)
{
请求资源不能为空,请重新输入"
return;
}
requestresource();
if(e.getSource()==b6)
release[i]=Integer.parseInt(text5[i].getText());
JOptionPane.showMessageD