页面置换算法

  • G3_419312
    了解作者
  • 28.3KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-06 05:54
    上传日期
使用简单的图形化界面展示了FIFO、LRU、SC、Clock四种页面置换算法的运行结果,可以接受任意长度的作业序列,并统计缺页中断次数以及缺页中断率。
OSCourseDesign.zip
  • OSCourseDesign
  • src
  • osCourseDesign
  • PageReplacement.java
    818B
  • SC.java
    1.2KB
  • FIFO.java
    903B
  • Page.java
    378B
  • Clock.java
    1.3KB
  • LRU.java
    1.9KB
  • MyTableModel.java
    239B
  • OSFrame.java
    10.3KB
  • MemoryList.java
    1KB
  • .settings
  • org.eclipse.jdt.core.prefs
    598B
  • org.eclipse.core.resources.prefs
    80B
  • bin
  • osCourseDesign
  • OSFrame$3.class
    982B
  • OSFrame.class
    11.7KB
  • OSFrame$2.class
    861B
  • Clock.class
    1.8KB
  • FIFO.class
    1.5KB
  • buffer.class
    395B
  • OSFrame$1.class
    1.4KB
  • MyIntComparator.class
    783B
  • MemoryList.class
    2.1KB
  • MyTableModel.class
    432B
  • Page.class
    625B
  • PageReplacement.class
    1.4KB
  • LRU.class
    2.2KB
  • SC.class
    1.8KB
  • OSFrame$MTableCellRenderer.class
    1.3KB
  • .project
    390B
  • .classpath
    301B
内容介绍
package osCourseDesign; import java.awt.*; import java.awt.event.*; import java.text.NumberFormat; import java.util.ArrayList; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; @SuppressWarnings("serial") public class OSFrame extends JFrame implements ActionListener { private JPanel[] panel; private JRadioButton[] radio; private ButtonGroup bg; private JLabel[] label; private JTextField[] text; public JSplitPane[] split; private JButton button; private static int select=1; private JTable table; private JComboBox<Integer> BlockCount; private double SequenceLength; private JScrollPane scroll; private MyTableModel model; public OSFrame() { super("页面置换算法"); this.setSize(800, 300); this.setLocationRelativeTo(null); panel=new JPanel[7]; for(int i=0;i<panel.length;i++) panel[i]=new JPanel(); split=new JSplitPane[4]; radio=new JRadioButton[4]; radio[0]=new JRadioButton("FIFO",true); radio[1]=new JRadioButton("LRU"); radio[2]=new JRadioButton("SC"); radio[3]=new JRadioButton("Clock"); bg=new ButtonGroup(); for(int i=0;i<radio.length;i++) bg.add(radio[i]); panel[0].setLayout(new GridLayout(4,1)); for(int i=0;i<radio.length;i++) panel[0].add(radio[i]); scroll=new JScrollPane(); scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); text=new JTextField[3]; for(int i=0;i<text.length;i++) text[i]=new JTextField(); text[0].setText("请输入正整数并以空格隔开"); text[1].setEnabled(false); text[2].setEnabled(false); split[0]=new JSplitPane(1,panel[0],panel[1]); split[0].setDividerLocation((int)(this.getWidth()*0.1)); split[0].setContinuousLayout(true); split[0].setDividerSize(1); split[0].setEnabled(false); this.add(split[0]); split[1]=new JSplitPane(0,panel[2],panel[3]); split[1].setDividerLocation((int)(this.getHeight()*0.2)); split[1].setContinuousLayout(true); split[1].setDividerSize(1); split[1].setEnabled(false); split[0].setRightComponent(split[1]); label=new JLabel[4]; label[0]=new JLabel("作业序列:",JLabel.CENTER); label[1]=new JLabel("分配的内存块数量:",JLabel.CENTER); label[2]=new JLabel("缺页中断次数:",JLabel.CENTER); label[3]=new JLabel("缺页中断率:",JLabel.CENTER); BlockCount=new JComboBox<Integer>(); for(int i=3;i<=8;i++) BlockCount.addItem(i); panel[4].setLayout(new GridLayout(2,2)); panel[4].add(label[0]); panel[4].add(text[0]); panel[4].add(label[1]); panel[4].add(BlockCount); button=new JButton("开始"); panel[5].setLayout(new GridLayout(1,1)); panel[5].add(button); split[2]=new JSplitPane(1,panel[4],panel[5]); split[2].setDividerLocation((int)(this.getWidth()*0.75)); split[2].setContinuousLayout(true); split[2].setDividerSize(1); split[2].setEnabled(false); split[1].setLeftComponent(split[2]); panel[6].setLayout(new GridLayout(1,4)); panel[6].add(label[2]); panel[6].add(text[1]); panel[6].add(label[3]); panel[6].add(text[2]); split[3]=new JSplitPane(0,scroll,panel[6]); split[3].setDividerLocation((int)(this.getHeight()*0.55)); split[3].setContinuousLayout(true); split[3].setDividerSize(1); split[3].setEnabled(false); split[1].setRightComponent(split[3]); for(int i=0;i<radio.length;i++) radio[i].addActionListener(this); this.setVisible(true); text[0].getDocument().addDocumentListener(new DocumentListener() { public void insertUpdate(DocumentEvent e) { String str=text[0].getText().replaceAll("\\s*", ""); if(!OSFrame.isNumeric(str)) JOptionPane.showMessageDialog(null, "作业序列只允许输入正整数和空格"); } public void changedUpdate(DocumentEvent e){} public void removeUpdate(DocumentEvent e){} }); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { switch(select) { case 1:FIFO();break; //FIFO case 2:LRU();break; //LRU case 3:SC();break; //SC case 4:Clock();break; //Clock default:;break; } } }); } public static boolean isNumeric(String str) { int asc; for(int i=0;i<str.length();i++) { asc=str.charAt(i); if(asc<48||asc>57) return false; } return true; } public void actionPerformed(ActionEvent e) { for(int i=0;i<radio.length;i++) if(e.getSource()==radio[i]) select=i+1; } public ArrayList<Integer> getSequence() { SequenceLength=0.0; String sequence=text[0].getText()+" "; String temp=""; ArrayList<Integer> list=new ArrayList<Integer>(); for(int i=0;i<sequence.length();i++) { if(sequence.charAt(i)!=' ') temp+=sequence.charAt(i); else { try { Integer Page_no=Integer.parseInt(temp); list.add(Page_no); temp=""; SequenceLength++; } catch(NumberFormatException e) { JOptionPane.showMessageDialog(null, "作业序列只允许输入正整数和空格"); return null; } } } if(SequenceLength<=(Integer)BlockCount.getSelectedItem()) { JOptionPane.showMessageDialog(null, "作业序列长度应至少大于内存块数量"); return null; } return list; } public void createTable(MyTableModel model) { table=new JTable(model); table.setDefaultRenderer(Object.class, new MTableCellRenderer()); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); scroll.setViewportView(table); } public void setData(int PageBreakCount) { text[1].setText(PageBreakCount+""); double num=PageBreakCount/SequenceLength; NumberFormat nt = NumberFormat.getPercentInstance(); nt.setMinimumFractionDigits(2); text[2].setText(nt.format(num)); } public <T> void setPageBreak(MemoryList<T> memoryList,int i,int BlockCount) { if(memoryList.getIsPageBreak()) model.setValueAt("×", BlockCount, i); else model.setValueAt("√", BlockCount, i); } public <T> ArrayList<MemoryList<T>> getMemoryList(PageReplacement<T> algorithm) { if(algorithm instanceof FIFO) for(int i=0;i<SequenceLength;i++) model.addColumn("页号"); else for(int i=0;i<SequenceLength;i++) model.addColumn("(页号,标记)"); model.setRowCount(algorithm.getBlockCount()+1); return algorithm.execute(); } public void FIFO() { model=new MyTableModel(); ArrayList<Integer> isNull=getSequence(); if(isNull==null) return; FIFO fifo=new FIFO(isNull,(Integer)BlockCount.getSelectedItem()); ArrayList<MemoryList<Integer>> list=getMemoryList(fifo); for(int i=0;i<list.size();i++) { MemoryList<Integer> memoryList=list.get(i); ArrayList<Integer> block=memoryList.getList(); for(int j=0;j<block.size();j++) model.setValueAt(block.get(j), j, i); this.setPageBreak(memoryList, i, fifo.getBlockCount()); } createTable(model); setData(fifo.getPageBreakCount()); } public void LRU() { model=new MyTableModel(); ArrayList<Integer> isNull=getSequence(); if(isNull==null) return; LRU lru=new LRU(isNull,(Integer)BlockCount.getSelectedItem()); ArrayList<MemoryList<Page>> list=getMemoryList(lru); for(int i=0;i<list.size();i++) { MemoryList<Page> memoryList=list.get(i); ArrayList<Page> block=memoryList.getList(); for(int j=0;j<block.size();j++) model.setValueAt("("+block.get(j).getPage_no()+","+block.get(j).getFlagbit()+")", j, i); this.setPageBreak(memoryList, i, lru.getBlockCount()); } createTable(model); setData(lru.getPageBreakCount()); } public void SC() { model=new MyTableModel(); ArrayList<Integer> isNull=getSequence(); if(isNull==null) return; SC sc=new SC(isNull,(Int
评论