模拟操作系统的页面置换
2013-06-16 02:30
501 查看
这个实验要求有点神奇,所以按照书上的要求写了
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import java.util.*; /* * @author Anima_libera */ /* * display */ public class Display extends JFrame implements ActionListener{ /* * 程序入口 */ public static void main(String args[]){ new Display(); } /* * 变量 */ private int arr[],time[],blockArr[]; private int lack = 0; private JPanel centre = new JPanel(); private JPanel choice = new JPanel(); private JPanel top = new JPanel(); private JPanel cal = new JPanel(); private JPanel par = new JPanel(); private JPanel author = new JPanel(); private JPanel chopar = new JPanel(); private int pageNum,blockNum; private JButton create = new JButton("生成序列"); private ButtonGroup bg = new ButtonGroup(); private JLabel authorLab[] = new JLabel[3]; private JLabel parLab[] = new JLabel[2]; private JLabel calLab[] = new JLabel[2]; private JTextField parJTF[] = new JTextField[2]; private JTextField calJTF[] = new JTextField[2]; private JScrollPane JSP[] = new JScrollPane[2]; private JTextArea order = new JTextArea(); private JTextArea JTA = new JTextArea(); private JRadioButton choBut[] = new JRadioButton[3]; /* * 构造函数 */ Display(){ setTitle("页面置换"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(800,700); init(); setVisible(true); } /* * 初始化 */ public void init(){ initTop(); initCal(); initCent(); } /* * 事件处理 */ public void actionPerformed(ActionEvent e){ if(parJTF[0].getText().toString().matches("[0-9]+") && parJTF[1].getText().toString().matches("[0-9]+")){ blockNum = Integer.parseInt(parJTF[0].getText().toString()); pageNum = Integer.parseInt(parJTF[1].getText().toString()); if((pageNum >= blockNum) && pageNum > 2 && blockNum > 1){ orderChange(); } else{ JOptionPane.showMessageDialog(null, "页数要大于或等于块数且页数要大于2,块数大于1", "输入错误", JOptionPane.INFORMATION_MESSAGE); } }else{ JOptionPane.showMessageDialog(null, "输入格式错误或者无输入", "输入错误", JOptionPane.INFORMATION_MESSAGE); } } /* * initTop */ public void initTop(){ top.setLayout(new GridLayout(2,1)); author.setLayout(new FlowLayout(FlowLayout.CENTER,60,1)); for(int i = 0;i < authorLab.length;++ i){ authorLab[i] = new JLabel(); } authorLab[0].setText("王勇杰"); authorLab[1].setText("20112004037"); authorLab[2].setText("8班"); for(int i = 0;i < authorLab.length; ++ i){ author.add(authorLab[i]); } chopar.setLayout(new GridLayout(1,2)); par.setBorder(new TitledBorder("输入参数")); for(int i = 0;i < parJTF.length; ++ i){ parLab[i] = new JLabel(); parJTF[i] = new JTextField(5); } parLab[0].setText("分配块数:"); parLab[1].setText("程序页数:"); par.add(parLab[0]); par.add(parJTF[0]); par.add(parLab[1]); par.add(parJTF[1]); par.add(create); create.addActionListener(this); choice.setBorder(new TitledBorder("算法选择")); for(int i = 0;i < choBut.length;++ i){ choBut[i] = new JRadioButton(); choice.add(choBut[i]); bg.add(choBut[i]); } choBut[0].setSelected(true); choBut[0].setText("OPT"); choBut[1].setText("FIFO"); choBut[2].setText("LRU"); chopar.add(choice); chopar.add(par); top.add(author); top.add(chopar); Container con = getContentPane(); con.add(top,BorderLayout.NORTH); setVisible(true); } /* * initCent */ public void initCent(){ for(int i = 0;i < JSP.length;++ i){ JSP[i] = new JScrollPane(); } GridBagLayout GBL = new GridBagLayout(); GridBagConstraints GBC = new GridBagConstraints(); centre.setLayout(GBL); GBC.fill = GridBagConstraints.BOTH; GBC.weightx = 10; GBC.weighty = 2; GBC.gridwidth = GridBagConstraints.REMAINDER;//换行 GBL.setConstraints(JSP[0], GBC); GBC.weighty = 50; GBC.gridheight = 5; GBL.setConstraints(JSP[1], GBC); JSP[0].setBorder(new TitledBorder("页面序列")); JSP[1].setBorder(new TitledBorder("置换过程")); JSP[0].setViewportView(order); JSP[1].setViewportView(JTA); order.setEditable(false); JTA.setEditable(false); centre.add(JSP[0]); centre.add(JSP[1]);//JScrollPane是个面板,是容器,应该吧contenPane里面加上一个JScrollPane子容器 Container con = getContentPane(); con.add(centre,BorderLayout.CENTER); setVisible(true); } /* * initCal */ public void initCal(){ for(int i = 0;i < calJTF.length; ++ i){ calLab[i] = new JLabel(); calJTF[i] = new JTextField(10); } calLab[0].setText("缺页次数:"); calLab[1].setText("缺页率:"); for(int i = 0;i < calLab.length;++ i){ cal.add(calLab[i]); cal.add(calJTF[i]); calJTF[i].setEditable(false); } cal.setBorder(new TitledBorder("性能评估")); Container con = getContentPane(); con.add(cal,BorderLayout.SOUTH); setVisible(true); } /* * orderChange */ public void orderChange(){ order.setText(" "); int num = (pageNum + 1)/ 2; arr = new int[pageNum]; for(int i = 0 ;i < num;++ i){ arr[i] = (int)(Math.random() * Integer.MAX_VALUE + 1); } Arrays.sort(arr, 0, num ); for(int i = 0;i < num / 2;++ i){//25%前向取值 arr[num + i] = arr[(int)(Math.random() * num / 2)]; } for(int i = 0;i < (pageNum - num - num / 2);++ i){//25%后向取值 arr[num + i + (num / 2)] = arr[(int)(Math.random() * num / 2) + (num / 2)]; } for(int i = 0 ;i < arr.length;++ i){ order.append(arr[i] + " "); } blockChange(); } /* * JTAChange,参数用于直接添加还是晴空后添加 */ public void JTAChange(){ for(int i = 0;i < blockNum;++ i){ JTA.append(blockArr[i] + " "); } JTA.append("\n"); } /* * calChange */ public void calChange(){ calJTF[0].setText(Integer.toString(lack)); float lackp = (float)(lack) / (float)(pageNum); calJTF[1].setText(Float.toString(lackp) + "%"); } /* * blockChange */ public void blockChange(){ blockArr = new int[blockNum]; time = new int[blockNum]; boolean flag = true; for(int i = 0; i < blockNum;++ i){//第一次把整个队列填满 for(int j = 0;j < blockArr.length;++ j){ if(blockArr[j] == arr[i]) flag = false; } if(flag){ blockArr[i] = arr[i]; time[i] = blockNum - i - 1; } } JTA.setText(""); for(int i = 0 ;i < blockNum;++ i){ if(blockArr[i] == 0 || blockNum == ((pageNum + 1) / 2)){ JTA.append("无需替换:\n"); for(int j = 0;j < blockNum;++ j){ if(blockArr[j] != 0){ JTA.append(blockArr[j] + " "); } } lack = 0; calChange(); return ; } } if(choBut[0].isSelected()) OPT(); if(choBut[1].isSelected()) FIFO(); if(choBut[2].isSelected()) LRU(); } /* * OPT */ public void OPT(){ lack = 0; int cha = 0,j = 0; for(int i = 0;i < (pageNum - blockNum);++ i,cha ++){ j = cha + blockNum + 1; JTA.append("正在执行第" + j + "页:\n"); OPTRun(cha); JTAChange(); } calChange(); } /* * OPTrun */ public void OPTRun(int cha){ int tem = 0,tem1 = 0; boolean fl = false; boolean fla = false; for(int i = 0;i < blockNum;++ i){ if(arr[blockNum + cha] == blockArr[i]) fla = true; } if(fla) return; for(int i = 0;i < blockNum;++ i){ for(int j = blockNum + cha;j < pageNum;j ++){ if(blockArr[i] == arr[j]){ if(j > tem){ tem = j;//记录多久后交换 tem1 = i;//记录要换的序号 fl = true; break; } fl = true; } } if(!fl){//没找到 tem1 = i; break; } fl = false; } blockArr[tem1] = arr[blockNum + cha]; lack ++; } /* * FIFO */ public void FIFO(){ lack = 0; int cha = 0,j = 0,k = 0; for(int i = 0;i < (pageNum - blockNum);++ i,cha ++){ j = cha + blockNum + 1; JTA.append("正在执行第" + j + "页:\n"); boolean fla = false; for(int x = 0;x < blockNum;++ x){ if(arr[blockNum + cha] == blockArr[x]) fla = true; } if(!fla){ blockArr[k] = arr[blockNum + cha]; k = (k + 1) % blockNum; lack ++; } JTAChange(); } calChange(); } /* * LRU */ public void LRU(){ lack = 0; int cha = 0,j = 0; for(int i = 0;i < (pageNum - blockNum);++ i,cha ++){ j = cha + blockNum + 1; JTA.append("正在执行第" + j + "页:\n"); boolean fla = false; for(int x = 0;x < blockNum;++ x){ if(arr[blockNum + cha] == blockArr[x]){ fla = true; time[x] = 0; } else{ time[x]++; } } if(!fla){ int tem = 0,tem1 = 0; for(int y = 0;y < blockNum;y ++){ if(time[y] > tem){ tem = time[y]; tem1 = y; } } blockArr[tem1] = arr[blockNum + cha]; time[tem1] = 0; lack ++; } JTAChange(); } calChange(); } }
相关文章推荐
- 操作系统模拟置换页面的算法
- 操作系统页面置换模拟系统
- (操作系统)模拟操作系统的页面置换
- 操作系统实验(2)-- 模拟操作系统的页面置换 LRU OPT FIFO
- 操作系统页面置换FIFO算法中的Belady现象
- 页面置换 读操作系统概念第六版
- 操作系统模拟页面调度算法(OPT、FIFO、LRU)演示(vc6.0调试通过)
- 操作系统的页面置换C++算法:OPT FIFO LRU CLOCK 计算缺页率
- 操作系统页面置换
- 设某计算机的逻辑地址空间和物理地址空间均为128KB,按字节编址。若某进程最多需要6页数据存储空间,页面大小为1KB,操作系统采用固定分配局部置换策略为该进程分配4个页框(物理块)。
- 【操作系统】 置换策略模拟实现
- 操作系统实验 页面置换模拟 c++
- 操作系统的页面置换C++算法:OPT FIFO LRU CLOCK 计算缺页率
- 计算机操作系统的各种算法汇——作业、进程、页面置换、磁盘调度等。
- 操作系统之页面置换
- 操作系统页面置换fifo lru方式
- 案例二:动态页面模拟点击
- dojo小例子(2):dojox/widget/Standby,当前页面不可编辑,模拟进度提示
- strust2页面分发机制模拟
- 页面跳转小试 - 模拟a.click事件