1、内存分配实验报告模板河 北 建 筑 工 程 学 院实 验 报 告 年 月 日班级物联142姓名孙胜杰学号20143260218评分实验台号同组人员实验名称可变分区存储管理的内存分配算法模拟实现-首次适应算法课程名称操作系统仪 器 名 称型号规格仪器编号装有eclipse和Java环境的PC机一台一 实验目的1 深刻理解可变分区的首次适应算法。2 掌握可变分区存储管理方式,使用可变分区的首次适应算法进行内存分配。二 实验设备PC机三 实验内容 在Java开发环境下模拟可变分区的内存分配,使用首次适应算法,内存分配。四 程序的主要代码package 首次适应算法;import java.awt.C
2、olor;import java.awt.Font;import java.awt.GridLayout;import java.awt.Label;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing
3、.JTextArea;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.swing.GroupLayout;import javax.swing.GroupLayout.Alignment;import javax.swing.LayoutStyle.ComponentPlacement;public class FFMethod extends JFrame /* * 首次适应算法 */ private static final long serialVersionUID = 188934
4、8358576796305L; protected JTextField blank0,blank1,blank2,blank3, blank4,blank5,blank6,blank7, blank8,blank9,blank10,blank11, blank12,blank13,blank14,blank15, blank16,blank17, blank18,blank19,blank20,blank21,blank22,blank23, blank24,blank25,blank26,blank27, blank28,blank29,blank30,blank31; /定义30个进程块
5、 protected JTextField applyMemTF,releaseMemTF; /申请大小、释放块号的编辑框 protected JTextArea showMemStatusTF; /内存分配情况显示框 protected JButton applyMemButton,releaseMemButton; /定义申请大小、释放块号和清空内存三个按钮 int processBlock = new int32; /表示进程块 int processBlockStartAdd = new int32; /表示存储起始地址 int processBlockLength = new int
6、32; /表示存储进程长度 public FFMethod() setForeground(Color.BLACK); setResizable(false); getContentPane().setBackground(Color.CYAN); setBackground(Color.WHITE); setFont(new Font(黑体, Font.PLAIN, 15); setTitle(u64CDu4F5Cu7CFBu7EDFu2014u2014u9996u6B21u9002u5E94u7B97u6CD5); setDefaultCloseOperation(JFrame.EXIT_
7、ON_CLOSE); JPanel p5 = new JPanel(); p5.setBackground(Color.YELLOW); Font font1 = new Font(SansSerif,Font.BOLD,16); GroupLayout groupLayout = new GroupLayout(getContentPane(); groupLayout.setHorizontalGroup( groupLayout.createParallelGroup(Alignment.LEADING) .addGroup(groupLayout.createSequentialGro
8、up() .addContainerGap() .addComponent(p5, GroupLayout.PREFERRED_SIZE, 612, GroupLayout.PREFERRED_SIZE) .addContainerGap(589, Short.MAX_VALUE) ); groupLayout.setVerticalGroup( groupLayout.createParallelGroup(Alignment.LEADING) .addGroup(groupLayout.createSequentialGroup() .addContainerGap() .addCompo
9、nent(p5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addContainerGap(81, Short.MAX_VALUE) ); JPanel p3 = new JPanel(new GridLayout(11,1,20,0); p3.setBackground(Color.YELLOW); JLabel label = new JLabel(u5185u5B58u5BB9u91CF32); label.setHorizontalAlignment(SwingC
10、onstants.LEFT); p3.add(label); p3.add(blank0 = new JTextField(3); p3.add(blank1 = new JTextField(3); p3.add(blank2 = new JTextField(3); p3.add(blank3 = new JTextField(3); p3.add(blank4 = new JTextField(3); p3.add(blank5 = new JTextField(3); p3.add(blank6 = new JTextField(3); p3.add(blank7 = new JTex
11、tField(3); p3.add(blank8 = new JTextField(3); p3.add(blank9 = new JTextField(3); p3.add(blank10 = new JTextField(3); p3.add(blank11 = new JTextField(3); p3.add(blank12 = new JTextField(3); p3.add(blank13 = new JTextField(3); p3.add(blank14 = new JTextField(3); p3.add(blank15 = new JTextField(3); p3.
12、add(blank16 = new JTextField(3); p3.add(blank17 = new JTextField(3); p3.add(blank18 = new JTextField(3); p3.add(blank19 = new JTextField(3); p3.add(blank20 = new JTextField(3); p3.add(blank21 = new JTextField(3); p3.add(blank22 = new JTextField(3); p3.add(blank23 = new JTextField(3); p3.add(blank24
13、= new JTextField(3); p3.add(blank25 = new JTextField(3); p3.add(blank26 = new JTextField(3); p3.add(blank27 = new JTextField(3); p3.add(blank28 = new JTextField(3); p3.add(blank29 = new JTextField(3); p3.add(blank30 = new JTextField(3); p3.add(blank31 = new JTextField(3); JPanel p1 = new JPanel(new
14、GridLayout(3,2,5,2); p1.setBackground(Color.YELLOW); p1.add(applyMemButton = new JButton(u7533u8BF7uFF08u5757u6570uFF09); applyMemButton.setBackground(Color.BLUE); p1.add(applyMemTF = new JTextField(3); applyMemTF.setToolTipText(); p1.add(releaseMemButton = new JButton(u91CAu653EuFF08u5757u53F7uFF09
15、); releaseMemButton.setBackground(Color.BLUE); p1.add(releaseMemTF = new JTextField(3); p1.add(new Label(tu5185u5B58u5206u914Du60C5u51B5uFF1A); JPanel p2 = new JPanel(); p2.setBackground(Color.YELLOW); p2.add(showMemStatusTF = new JTextArea(); GroupLayout gl_p2 = new GroupLayout(p2); gl_p2.setHorizo
16、ntalGroup( gl_p2.createParallelGroup(Alignment.LEADING) .addGroup(gl_p2.createSequentialGroup() .addComponent(p1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addGap(28) .addGroup(gl_p2.createSequentialGroup() .addComponent(showMemStatusTF, GroupLayout.DEFAULT_S
17、IZE, 265, Short.MAX_VALUE) .addContainerGap() ); gl_p2.setVerticalGroup( gl_p2.createParallelGroup(Alignment.LEADING) .addGroup(gl_p2.createSequentialGroup() .addComponent(p1, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(show
18、MemStatusTF, GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE) ); JButton button = new JButton(u6E05u7A7Au5185u5B58); button.setBackground(Color.RED); button.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) for(int cc = 0;cc 32 | n 0) JOptionPane.showMessageDialog(null
19、,进程大小违规,请重新输入!); outher:/向内存中添加进程 for(int i = 0;i 32; i+ ) /向内存中添加进程 if(processBlocki = 0 & Sum(processBlock,i,n) = 0) processBlockStartAddi = i; /存储起始地址 processBlockLengthi = n; /存储进程长度 for(int ss = i;ss (i + n);ss+) processBlockss = 1; /找到合适的位置,置1 colorr(); JOptionPane.showMessageDialog(null,成功分配到
20、内存!); showMemStatusTF.append(块号: + processBlockStartAddi + 起始位置: + processBlockStartAddi + 大小: + processBlockLengthi +n); break outher; if(i = 31) JOptionPane.showMessageDialog(null,内存不足,请等待.); break outher; ); /释放内存按钮监听 releaseMemButton.addActionListener(new ActionListener() public void actionPerfo
21、rmed(ActionEvent e) int m = Integer.parseInt(releaseMemTF.getText();/进程块的起始位置和长度 for(int ff = m;ff (m + processBlockLengthm);ff+) processBlockff = 0; processBlockStartAddm = 0; processBlockLengthm = 0; colorr(); showMemStatusTF.setText(); for(int bb = 0;bb 10; bb+) if(processBlockLengthbb != 0) show
22、MemStatusTF.append(块号: + processBlockbb + 起始位置: + processBlockStartAddbb + 大小:+ processBlockLengthbb + n); ); GroupLayout gl_p5 = new GroupLayout(p5); gl_p5.setHorizontalGroup( gl_p5.createParallelGroup(Alignment.LEADING) .addGroup(gl_p5.createSequentialGroup() .addGap(20) .addComponent(p2, GroupLay
23、out.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.UNRELATED) .addComponent(p3, GroupLayout.PREFERRED_SIZE, 273, Short.MAX_VALUE) .addGap(26) ); gl_p5.setVerticalGroup( gl_p5.createParallelGroup(Alignment.LEADING) .addGroup(gl_p5.createSeque
24、ntialGroup() .addGroup(gl_p5.createParallelGroup(Alignment.LEADING, false) .addComponent(p3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(gl_p5.createSequentialGroup() .addGap(26) .addComponent(p2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PR
25、EFERRED_SIZE) .addContainerGap(27, Short.MAX_VALUE) ); p5.setLayout(gl_p5); getContentPane().setLayout(groupLayout); /判断内存空间是否足够 public int Sum(int pp,int mm,int k) int sum = 0; if(mm + k) = 32) for(int zz = mm;zz (mm + k);zz+) sum+=ppzz; else sum = 1; return sum; /内存与processBlock数组相对应,占用颜色为绿色,空白为蓝色
26、 public void colorr() if(processBlock0=1) blank0.setBackground(Color.GREEN); else blank0.setBackground(Color.WHITE); if(processBlock1=1) blank1.setBackground(Color.GREEN); else blank1.setBackground(Color.WHITE); if(processBlock2=1) blank2.setBackground(Color.GREEN); else blank2.setBackground(Color.W
27、HITE); if(processBlock3=1) blank3.setBackground(Color.GREEN); else blank3.setBackground(Color.WHITE); if(processBlock4=1) blank4.setBackground(Color.GREEN); else blank4.setBackground(Color.WHITE); if(processBlock5=1) blank5.setBackground(Color.GREEN); else blank5.setBackground(Color.WHITE); if(processBlock6=1)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1