⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mallocator.java

📁 操作系统课程设计之内存分配模拟,采用首次适应算法实现(JAVA版)
💻 JAVA
字号:
/**
 * MAllocator类
 * 带main()入口方法
 * @author linpeitian
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MAllocator extends JFrame implements ActionListener{
	
	/**
	 * UI 组件
	 */
	private JTextField name = null;   //进程名输入域
	private JTextField mem = null;    //分配内存值输入域
	private int memory = 0;
	
	private JSeparator separator = null;
	
	private JButton allocate = null;    //分配按钮
	private JButton release = null;     //释放按钮
	
	private JTextArea field = null;
	
	private StatusPanel status = null;
	
	/**
	 * 逻辑控制
	 */
	private int num = 0;
	private int waitNum = 0;
	
	private int[] block = new int[128];   //模拟内存块
	
	private ProcessBean[] process = new ProcessBean[30];
	private ProcessBean[] waitProcess = new ProcessBean[30];
	
	/**
	 * 构造函数
	 * 构架程序界面
	 */
	public MAllocator(){
		init();
		setTitle("内存分配模拟");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//UI布局
		GridBagConstraints cst = null;
		GridBagLayout layout = new GridBagLayout();
		getContentPane().setLayout(layout);
		
		JLabel nameLabel = new JLabel("进程名:");
		cst = new GridBagConstraints();
		cst.anchor = GridBagConstraints.EAST;
		cst.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(nameLabel, cst);
		getContentPane().add(nameLabel);
		
		name = new JTextField(20);
		cst = new GridBagConstraints();
		cst.fill = GridBagConstraints.HORIZONTAL;
		cst.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(name, cst);
		getContentPane().add(name);
		
		allocate = new JButton("分配");
		allocate.addActionListener(this);
		cst = new GridBagConstraints();
		cst.gridwidth = GridBagConstraints.REMAINDER;
		cst.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(allocate, cst);
		getContentPane().add(allocate);
		
		JLabel memLabel = new JLabel("内存值:");
		cst = new GridBagConstraints();
		cst.anchor = GridBagConstraints.EAST;
		cst.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(memLabel, cst);
		getContentPane().add(memLabel);
		
		mem = new JTextField(10);
		cst = new GridBagConstraints();
		cst.fill = GridBagConstraints.HORIZONTAL;
		cst.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(mem, cst);
		getContentPane().add(mem);
		
		release = new JButton("释放");
		release.addActionListener(this);
		cst = new GridBagConstraints();
		cst.gridwidth = GridBagConstraints.REMAINDER;
		cst.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(release, cst);
		getContentPane().add(release);
		
		separator = new JSeparator();
		cst = new GridBagConstraints();
		cst.fill = GridBagConstraints.HORIZONTAL;
		cst.gridwidth = GridBagConstraints.REMAINDER;
		cst.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(separator, cst);
		getContentPane().add(separator);
		
		field = new JTextArea(15, 40);
		field.setText("进程名字      " +
				"进程内存大小        " +
				"运行否           " +
				"内存开始地址     " +
				"内存结束地址\n");
		field.append("--------------------------------" +
				"--------------------------------" +
				"--------------------------------\n");
		field.setEditable(false);
		JScrollPane bar = new JScrollPane(field);
		cst = new GridBagConstraints();
		cst.gridwidth = GridBagConstraints.REMAINDER;
		cst.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(bar, cst);
		getContentPane().add(bar);
		
		status = new StatusPanel(this);
		cst = new GridBagConstraints();
		cst.fill = GridBagConstraints.HORIZONTAL;
		cst.gridwidth  = GridBagConstraints.REMAINDER;
		cst.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(status, cst);
		getContentPane().add(status);
		
		setSize(550, 450);
		setResizable(false);
		setVisible(true);
	}
	
	/**
	 * 初始化模拟内存快
	 */
	public void init(){
		for(int i = 0; i < 128; i++){
			block[i] = 0;
		}
	}
	
	/**
	 * 内存分配函数
	 */
	public void memoryAllocate(){
		int count = 0;
		int i = 0;
		for( ; i < 128; i++){
			if(block[i] == 0){
				count ++;
				//if成立则表示可以分配内存
				if(count == memory){
					//具体分配内存
					process[num] = new ProcessBean(name.getText(), memory);
					for(int j = 0; j < memory; j++, i--)
						block[i] = 1;
					process[num].setRunning(true);
					process[num].setStart(i + 1);
					process[num].setEnd(i + memory);
					num ++;
					//跳出循环
					break;
				}
		    }else
			    count = 0;
	    }
		if(i == 128){
			waitProcess[waitNum] = new ProcessBean(name.getText(), memory);
			waitNum ++;
		}
	}
	
	/**
	 * 内存回收函数
	 * @param id
	 */
	public void memoryRelease(int id){
		//释放空间
		int i = process[id].getStart();
		for(int j = 0 ; j < process[id].getMem(); j++ )
			block[i+j] = 0;
		num --;
		//操作进程序列
		process[id] = null;
		process[id] = process[num];
		//激活因分配不到相应内存的进程
		for(int j = 0; j < waitNum; j++){
			int count = 0;
			if(!waitProcess[j].isRunning()){
				for(int t = 0; t < 128; t++){
					if(block[t] == 0){
						count ++;
						//if成立则表示可以分配内存
						if(count == waitProcess[j].getMem()){
							//具体分配内存
							for(int p = 0; p < waitProcess[j].getMem(); p++, t--)
								block[t] = 1;
							waitProcess[j].setRunning(true);
							waitProcess[j].setStart(t + 1);
							waitProcess[j].setEnd(t + memory);
							process[num] = waitProcess[j];
							num++;
							for(int k = j; k < waitNum-1; k++){
								waitProcess[k] = waitProcess[k+1];
							}
							waitNum --;
							//跳出循环
							break;
						}
				    }else
					    count = 0;
				}
			}
		}
	}
	
	/**
	 * 事件相应处理
	 * 实现ActionListener接口
	 */
	public void actionPerformed(ActionEvent e){
		//获取事件源
		Object oj = e.getSource();
		if(oj == allocate){
			//分配按钮事件
			if(name.getText().trim().equals("")){
				JOptionPane.showMessageDialog(this, 
						"进程名为空,请输入进程名字",
						"非法输入",
						JOptionPane.ERROR_MESSAGE);
			}else if(mem.getText().trim().equals("")){
				JOptionPane.showMessageDialog(this,
						"分配内存数为空,请输入分配内存数",
						"非法输入",
						JOptionPane.ERROR_MESSAGE);
			}else{
				try{
					memory = Integer.parseInt(mem.getText());
					if(memory <=0 || memory >128){
						JOptionPane.showMessageDialog(this,
								"分配内存过大或者小于等于0",
								"非法输入",
								JOptionPane.ERROR_MESSAGE);
					}else{
						//内存分配
						memoryAllocate();
					}
				}catch(NumberFormatException ee){
					JOptionPane.showMessageDialog(this,
							"内存输入值不是整数,请重新输入",
							"非法输入",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		}else if(oj == release){
			//释放按钮事件
			if(name.getText().trim().equals("")){
				JOptionPane.showMessageDialog(this, 
						"进程名为空,请输入进程名字",
						"出错",
						JOptionPane.ERROR_MESSAGE);
			}else{
				int i = 0;
				int tmp = num;
				for( ; i < num; i++){
					if(process[i].getName().equals(name.getText())){
						//释放内存
						memoryRelease(i);
						break;
					}
				}
				if(i == tmp){
					JOptionPane.showMessageDialog(this,
							"进程不存在,请确认您的输入,或者还没进入内存",
							"出错",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		}
		status.repaint();
		field.setText("");
		field.append("进程名字      " +
				"进程内存大小        " +
				"运行否           " +
				"内存开始地址     " +
				"内存结束地址\n");
		for(int k = 0; k < num; k++){
			field.append(process[k].toString());
		}
		field.append("--------------------------------" +
				"--------------------------------" +
				"--------------------------------\n");
		for(int k = 0; k< waitNum; k++){
			field.append(waitProcess[k].toString());
		}
		name.setText("");
		mem.setText("");
	}
	
	/**
	 * 获取内存块分配状态
	 * @return
	 */
	public int[] getBlock(){
		return block;
	}
	
	/**
	 * 程序入口函数main
	 * @param args
	 */
	public static void main(String[] args){
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
		new MAllocator();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -