操作系统课程设计银行家算法和页面置换算法.docx
南京工程学院操作系统课程设计说明书题 目 操作系统银行家算法和页面置换算法 班 级 软件101 学 生 姓 名 刘 洋 学 号 202100320 课设截图:源代码:3个类:MainFrame.java:import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.ButtonGroup;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JRadioButton;import javax.swing.UIManager;import javax.swing.UIManager.LookAndFeelInfo;public class MainFrame extends JFrame implements ActionListener /* * */ private static final long serialVersionUID = 1L; private JPanel panel1,panel2,panel3; private JLabel label1,label2; private JButton button1; private JRadioButton b1,b2; public MainFrame() super("操作系统课程设计"); this.setBounds(300, 150, 800, 500); this.setBackground(java.awt.Color.LIGHT_GRAY); this.setLayout(new java.awt.GridLayout(3, 1); panel1=new JPanel(); panel2=new JPanel(); panel3=new JPanel(); Font f =new Font("楷体",1,60); Font f1=new Font("宋体",3,30); Font f2=new Font("幼圆",1,15); label1=new JLabel("操作系统课程设计"); label1.setFont(f); label2=new JLabel("算法模拟题目:"); label2.setFont(f1); panel1.add(label1); panel2.add(label2); ButtonGroup bgroup=new ButtonGroup(); b1=new JRadioButton("银行家算法",true); b1.setFont(f1); bgroup.add(b1); b2=new JRadioButton("页面置换算法"); b2.setFont(f1); bgroup.add(b2); panel2.add(b1); panel2.add(b2); button1=new JButton("开始演示"); 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); public void actionPerformed(ActionEvent e) if(e.getSource()=button1)if(b1.isSelected() new Banker();if(b2.isSelected()new Yemian();public static void main (String args) tryfor(LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()if("Nimbus".equals(info.getName()UIManager.setLookAndFeel(info.getClassName();break;catch(Exception e1) new MainFrame(); Banker.java:import java.awt.*;import java.awt.event.*;import java.util.ArrayList;import javax.swing.*;import javax.swing.table.DefaultTableCellRenderer;import javax.swing.table.DefaultTableModel; public class Banker extends JFrame implements ActionListener /* * */private static final long serialVersionUID = 1L;private JTable table1,table2; private JPanel p0,p1,p11,p12,p13,p14,p2,p3,p31,p32,p33,p34,p4,p5; private JLabel t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11; private JButton b1,b2,b3,b4,b5,b6; private TextField text01,text02,text03,text04,text05,text06;/编辑框 private JTextField text1,text2,text3,text4,text5;/进程组的数据 DefaultTableModel tablemodel,tablemodel1; ArrayList<Integer> list ; int claim,allocation,need; int available;int request,temavail,release; boolean possible; int m,n,index=0; public Banker() super("银行家算法模拟"); p0=new JPanel(); p1=new JPanel(); p11=new JPanel(); p12=new JPanel(); p13=new JPanel(); p14=new JPanel(); p2=new JPanel(); p3=new JPanel(); p31=new JPanel(); p32=new JPanel(); p33=new JPanel(); p34=new JPanel(); p4=new JPanel(); p5=new JPanel(); p0.setLayout(new GridLayout(5,1); p1.setLayout(new GridLayout(4,1); p3.setLayout(new GridLayout(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=new JLabel("进程数"); t2=new JLabel("资源数"); t3=new JLabel("进程号"); t4=new JLabel("已分配资资源:"); t5=new JLabel("资源最大需求:"); t6=new JLabel("可用资源:"); t7=new JLabel("请求资源进程号"); t8=new JLabel("请求资源为"); t9=new JLabel("释放资源"); t10=new JLabel("安全序列"); t11=new JLabel("释放资源进程号"); b1=new JButton("确定"); b2=new JButton("添加"); b3=new JButton("确定"); b4=new JButton("请求"); b5=new JButton("开始检测"); b6=new JButton("释放"); text1=new JTextField6; text2=new JTextField6; text3=new JTextField6; text4=new JTextField6; text5=new JTextField6; for(int i=0;i<5;i+) text1i=new JTextField(4); text2i=new JTextField(4); text3i=new JTextField(4); text4i=new JTextField(4); text5i=new JTextField(4); text01=new TextField(4); text02=new TextField(4); text03=new TextField(4); text04=new TextField(4); text05=new TextField(4); text06=new TextField(20); String columnNames1= "进程号", "allocation","claim","need","available" tablemodel=new DefaultTableModel(columnNames1,0); table1 = new JTable (tablemodel); table1.setPreferredScrollableViewportSize(new Dimension(700, 200); table1.setRowHeight (20); table1.doLayout (); DefaultTableCellRenderer r = new DefaultTableCellRenderer(); r.setHorizontalAlignment(JLabel.CENTER); table1.setDefaultRenderer(Object.class,r); JScrollPane pane1 = new JScrollPane (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); for(int i=0;i<5;i+) p13.add(text1i); p14.add(t5); for(int i=0;i<5;i+) p14.add(text2i); p2.add (pane1); p31.add(t6); for(int i=0;i<5;i+) p31.add(text3i); p31.add(b3); p32.add(t7); p32.add(text04); p32.add(t8); for(int i=0;i<5;i+) p32.add(text4i); p32.add(b4); p33.add(t11); p33.add(text05); p33.add(t9); for(int i=0;i<5;i+) p33.add(text5i); p33.add(b6); p34.add(b5); String columnNames2= "进程号", "currentavail","need","allocation","curr.+allo.","possible" tablemodel1=new DefaultTableModel(columnNames2,0); table2 = new JTable (tablemodel1); table2.setPreferredScrollableViewportSize(new Dimension(700, 200); table2.setRowHeight (20); table2.doLayout (); DefaultTableCellRenderer r1 = new DefaultTableCellRenderer(); r1.setHorizontalAlignment(JLabel.CENTER); table2.setDefaultRenderer(Object.class,r1); JScrollPane pane2 = new JScrollPane (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 = new ArrayList<Integer>(); this.setContentPane (p0); this.setVisible(true); this.pack(); this.setLocation(300, 10); this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); public void actionPerformed(ActionEvent e) if(e.getSource()=b1) try m= Integer.parseInt(text01.getText(); catch(NumberFormatException e1) JOptionPane.showMessageDialog(this,"进程数不能为空");return; try n = Integer.parseInt(text02.getText(); catch(NumberFormatException e1) JOptionPane.showMessageDialog(this,"资源数不能为空");return; claim= new intmn; need=new intmn; allocation = new intmn; available = new intn; request = new intn; release=new intn; for(int i=0;i<5-n;i+) p13.remove(text14-i); p13.updateUI(); for(int i=0;i<5-n;i+) p14.remove(text24-i); p14.updateUI(); for(int i=0;i<5-n;i+) p31.remove(text34-i); p31.updateUI(); for(int i=0;i<5-n;i+) p32.remove(text44-i); p32.updateUI(); for(int i=0;i<5-n;i+) p33.remove(text54-i); p33.updateUI(); temavail=new intn; String str="","","","","","" for(int i=0;i<m;i+)tablemodel1.addRow(str); if(e.getSource()=b2) String name=new Stringm;if(index>=m) JOptionPane.showMessageDialog(this,"进程个数已满");return;tryfor(int j=0;j<n;j+) allocationindexj=Integer.parseInt(text1j.getText();claimindexj=Integer.parseInt(text2j.getText();needindexj=claimindexj-allocationindexj; catch(Exception f)nameindex="P"+index; String strd="","","","",""strd0=nameindex; for(int j=0;j<n;j+)strd1+=allocationindexj+" " for(int j=0;j<n;j+) strd2+=claimindexj+" " for(int j=0;j<n;j+) strd3+=needindexj+" " strd4=" " tablemodel.addRow(strd); index+; if(e.getSource()=b3) try for(int i=0;i<n;i+) availablei=Integer.parseInt(text3i.getText(); temavaili=availablei; catch(NumberFormatException f) JOptionPane.showMessageDialog(this,"可用资源不能为空,请重新输入");return; String str="" for(int i=0;i<n;i+) str+=availablei+" " tablemodel.setValueAt(str, 0, 4); if(e.getSource()=b5) list.clear(); safe(); for(int i=0;i<table1.getRowCount();i+) tablemodel1.removeRow(0); update_table2(); if(e.getSource()=b4) for(int i=0;i<n;i+) try requesti=Integer.parseInt(text4i.getText(); catch(NumberFormatException f1) JOptionPane.showMessageDialog(this,"请求资源不能 为空,请重新输入");return; requestresource(); if(e.getSource()=b6) for(int i=0;i<n;i+) try releasei=Integer.parseInt(text5i.getText(); catch(NumberFormatException f1) JOptionPane.showMessageDialog(this,"释放资源不能 为空,请重新输入"); return; releaseresource(); private boolean safe() boolean result =false;for(int i=0;i<m;i+) int j=0; while(j<m) boolean can =true; for(int k=0;k<n;k+) if(needjk>availablek)can=false; if(can&&!list.contains(Object)j) list.add(j); for(int k=0;k<n;k+) availablek+=allocationjk; j+; if(j>=m) break; if(i=m-1)break; if(list.size()=m)for(int k=0;k<n;k+) availablek=temavailk;result=true;return result; public void update_table1() int index1=Integer.parseInt(text04.getText(); String str="","","" for(int k=0;k<n;k+) str0+=allocationindex1k+" " tablemodel.setValueAt(str0, index1, 1); for(int k=0;k<n;k+) str1+=needindex1k+" " tablemodel.setValueAt(str1, index1, 3); for(int k=0;k<n;k+) str2+=availablek+" " tablemodel.setValueAt(str2, 0, 4); public void update_table2() if(safe() String temp = "" for(Integer x:list) temp += ("P"+x + ","); String str="","","","","","" str0="P"+x; for(int k=0;k<n;k+) str1+=availablek+" " for(int k=0;k<n;k+) str2+=needxk+" " for(int k=0;k<n;k+) str3+=allocationxk+" " for(int k=0;k<n;k+) availablek+=allocationxk; for(int k=0;k<n;k+) str4+=availablek+" " str5="True" tablemodel1.addRow(str); for(int k=0;k<n;k+) availablek=temavailk; text06.setText(temp); else text06.setText("不存在安全序列"); JOptionPane.showMessageDialog(this, "系统处于不安全状态,不能分配资源!"); return; public void requestresource() int index1=Integer.parseInt(text04.getText(); for(int i=0;i<n;i+) if(requesti<=needindex1i&&requesti<=availablei) temavaili-=requesti; allocationindex1i+=requesti; needindex1i-=requesti; if(safe() update_table1(); else temavaili+=requesti; else JOptionPane.showMessageDialog(this, "系统资源不足,申请被拒绝!"); return; public void releaseresource() int index2=Integer.parseInt(text05.getText(); for(int i=0;i<n;i+) availablei+= releasei; temavaili+=releasei; allocationindex2i-=releasei; needindex2i+=releasei; update_table1(); Yemian.java:import java.awt.Color;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.ArrayList;import java.util.LinkedList;import javax.swing.*;public class Yemian extends JFrame implements ActionListener/* * */private static final long serialVersionUID = 1L;private int mSIZE=20;/页框最大个数private static int pSIZE=20;/页面最大个数private static int process=new intpSIZE;/随机产生的页面走向static String memString=""private JButton demoFIFO;private JButton demoLRU;private JComboBox produce;/随机产生或输入数据private JTextArea fifoArea;private JTextArea lruArea;private JButton makeProcess;/页面private JButton makememery;/页框private JLabel processLabel;private JLabel makePLabel;private JLabel makeMLabel;private JTextField processField;private JTextField memeryField;private JPanel producePanel;private JPanel inputme