操作系统-(动态分区算法java实现)

动态分区存储管理
实验目的:熟悉并掌握动态分区分配的各种算法。
熟悉并掌握动态分区中分区回收的各种情况,并能够实现分区合并。
实验内容:用高级语言模拟实现动态分区存储管理,要求:
1、 分区分配算法至少实现首次适应算法、最佳适应算法和最坏适应算法中的至少一种。熟悉并掌握各种算法的空闲区组织方式。
2、 分区的初始化——可以由用户输入初始分区的大小。(初始化后只有一个空闲分区,起始地址为0,大小是用户输入的大小)
3、 分区的动态分配过程:由用户输入作业号和作业的大小,实现分区过程。
4、 分区的回收:用户输入作业号,实现分区回收,同时,分区的合并要体现出来。(注意:不存在的作业号要给出错误提示!)
5、 分区的显示:任何时刻,可以查看当前内存的情况(起始地址是什么,大小多大的分区时空闲的,或者占用的,能够显示出来)
6、 要求考虑:(1)内存空间不足的情况,要有相应的显示;
(2)作业不能同名,但是删除后可以再用这个名字;
(3)作业空间回收是输入作业名,回收相应的空间,如果这个作业名不存在,也要有相应的提示。

算法的难点和结局方法:

  1. 首先是程序最开始时候算法的选择,我是先将所有的分区当做一个对象,对象中包括分区名称,分区开始地址,分区结束地址,分区容量,分区状态,已经最后一个所使用的算法,这三个算法有一些共性,首先是首次适应算法,它是根据地址的大小进行排序的,所以我对list中所有的分区进行空间开始地址进行排序。其次是最佳适应算法是根据分区容量的大小进行排序,所以我对list中所有的分区按照capacity进行排序,每一个都是去从小到达的顺序进行查找,最后是最坏适应算法,他是根据分区容量大小的的逆序排序,所以我只需要将上一步在最佳适应算法的前提下在做一下逆序就行,也就是每次从最后一个开始查找。
  2. 在对list中复杂对象中的某一个属性进行排序的时候,需要实现implements Comparable方法,重写CompareTo方法。原来使用了Compareto方法,之后又想了一下,不用进行排序就行,只用遍历一下就可以找到当前算法情况下的一个分区。
  3. 在者就是在分区回收的时候所遇到的事情,当在已存在的分区中找到一个分区,如何记录这个分区的上一个分区和下一个分区的状态呢,这里我选择新建一个数组将所有分区都加入到数组中,每一个遍历的时候找到的话,就找下面的一个分区是否下面一个分区是空闲分区,在遍历上面一个是否是空闲分区,是的话那么就直接使用合并。
  4. 另外就是如果要回收的分区上下有空闲分区,那么就将这个分区当中的一个分区的名字设置成为-1,再次添加到list中的时候,通过判断将名称是-1的分区不加入到list中。
    这些就是这次实验所遇到的一些问题和解决方法。
    源代码:
package Blue;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class 动态分区存储管理 {
	public static int len,p;
	public static String[] string = {"空闲","已占用"};
	public static List<storge> list = new LinkedList<storge>();
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入初始空间的大小:");
		len = input.nextInt();
		System.out.println("请输入选择的算法:1——FF,2——BF,3——WF");
		p = input.nextInt();
		list.add(new storge("NULL",0,len-1,len,0,p));//首先将初始空间加入
		while(true) {
			System.out.println("请输入下一步所要进行的操作1——分配,2——回收,3——退出当前算法");
			int key = input.nextInt();
			switch (key) {
			case 1://分配分区的操作
				System.out.println("请输入分区号,分区大小");
				String name1 = input.next();
				int large = input.nextInt();
				allocation(name1,large);
				show();
				break;
			case 2://回收分区的操作
				System.out.println("请输入将要回收的分区");
				String name2 = input.next();
				release(name2);
				show();
				break;
			case 3:
				list.clear();
				System.out.println("请输入初始空间的大小:");
				len = input.nextInt();
				System.out.println("请输入选择的算法:1——FF,2——BF,3——WF");
				p = input.nextInt();
				list.add(new storge("NULL",0,len-1,len,0,p));//首先将初始空间加入
				break;
			default:
				System.out.println("无效操作,请重新输入");
				break;
			}
		}
	}
	public static void release(String name) {
		boolean flag = false;
		int t = 0;
		List<storge> cop = new LinkedList<>();
		for (int j = 0; j < len; j++) {
			for (int i = 0; i < list.size(); i++) {
				storge temporacy = list.get(i);
				if (j == temporacy.start && temporacy.capacity!=0) {
					cop.add(temporacy);
				}
			}
		}
		list.clear();
		storge shu [] =new storge[cop.size()];
		for (int i = 0; i < cop.size(); i++) {
			shu[i] = cop.get(i);
		}
		for (int i = 0; i < cop.size(); i++) {
			if (shu[i].name.equals(name)) {
				if (i !=0  && i !=shu.length-1 ) {//说明是在中间
					System.out.println("                            中间");
					if (shu[i-1].status != 0 && shu[i+1].status != 0) {
						System.out.println("                            中间这一层");
						shu[i].status = 0;
						shu[i].name = "NULL";
					}else {
						if (shu[i+1].status == 0) {
							System.out.println("                            中间二");
							shu[i].capacity = shu[i].capacity + shu[i+1].capacity;
							shu[i].end = shu[i].end + shu[i+1].capacity;
							shu[i].status = 0;
							shu[i].name = "NULL";
							shu[i+1].status = 0;
							shu[i+1].name = "-1";
						}
						if (shu[i-1].status == 0) {
							System.out.println("                            中间三");
							shu[i-1].capacity = shu[i-1].capacity + shu[i].capacity;
							shu[i-1].end = shu[i-1].end + shu[i].capacity;
							shu[i].status = 0;
							shu[i-1].name = "NULL";
							shu[i].name = "-1";
						}
					}
				}else if (i == 0 && i !=shu.length-1) {//说明是在开头
					System.out.println("                            开头");
					if (shu[i+1].status == 0) {
						shu[i].capacity = shu[i].capacity + shu[i+1].capacity;
						shu[i].end = shu[i].end + shu[i+1].capacity;
						shu[i].status = 0;
						shu[i].name = "NULL";
						shu[i+1].name = "-1";
					}else {
						shu[i].status = 0;
						shu[i].name = "NULL";
					}
				}else if (i !=0 && i ==shu.length-1) {//说明是在结尾
					System.out.println("                            结尾");
					if (shu[i-1].status == 0) {
						shu[i-1].capacity = shu[i-1].capacity + shu[i].capacity;
						shu[i-1].end = shu[i-1].end + shu[i].capacity;
						shu[i].status = 0;
						shu[i-1].name = "NULL";
						shu[i].name = "-1";
					}else {
						shu[i].status = 0;
						shu[i].name = "NULL";
					}
				}
				flag = true;
			}	
		}
		list.clear();
		for (int j = 0; j < shu.length; j++) {
			if (!shu[j].name.equals("-1") && shu[j].capacity != 0) {
				list.add(shu[j]);
				System.out.println(shu[j].name);
			}
		}
		if (flag) {
			System.out.println("释放成功");
		}else {
			System.out.println("释放失败,没有此分区");
		}
	}
	public static void allocation(String name,int large) {
		boolean falg = true;
		for(storge key:list) {
			if (key.name.equals(name)) {
				System.out.println(name+" 已经存在,分配失败,");
				return;
			}
			if (key.capacity >= large) {
			falg = false;
			}
		}
		if (falg == true) {
			System.out.println("剩余空间不足,分配失败");
			return;
		}
		if (p == 1) {
			for (int i = 0; i < len; i++) {
				for (int j = 0; j < list.size(); j++) {
					storge tems = list.get(j);
					if (j == tems.start && tems.status == 0 && tems.capacity >= large) {
						list.add(new storge(name, tems.start, tems.start+large, large, 1,tems.algorithm));//表示现在将这一个分区加入
						storge temp = new storge("NULL", tems.start+large, tems.end, tems.capacity-large, 0,tems.algorithm);
						list.remove(tems);
						list.add(temp);
						System.out.println("分区分配成功");
						return;
					}
				}
			}
		}else if (p == 2) {
			int min = 10000;
			storge copy = new storge("", 1, 1, 1, 1, 2);
			for (int i = 0; i < len; i++) {
				for (int j = 0; j < list.size(); j++) {
					storge tems = list.get(j);
					if (tems.status == 0 && tems.capacity >= large && tems.start == i) {
						if (tems.capacity < min) {
							min = tems.capacity;
							copy = tems;
						}
					}
				}
			}
			System.out.println(copy.start+" "+copy.name);
			list.add(new storge(name, copy.start, copy.start+large, large, 1,copy.algorithm));//表示现在将这一个分区加入
			storge temp = new storge("NULL", copy.start+large, copy.end, copy.capacity-large, 0,copy.algorithm);
			list.remove(copy);
			list.add(temp);
			System.out.println("分区分配成功");
			return;
		}else  if (p == 3) {
			int max = -10000;
			for (int i = 0; i < list.size(); i++) {
				System.out.println(list.get(i).name+"    "+list.get(i).capacity);
			}
			storge copy = new storge("", 1, 1, 1, 1, 3);
			for (int i = 0; i < len; i++) {
				for (int j = 0; j < list.size(); j++) {
					storge tems = list.get(j);
					if (tems.status == 0 && tems.capacity >= large && tems.start == i) {
						if (tems.capacity > max) {
							max = tems.capacity;
							copy = tems;
						}
					}
				}
			}
			list.add(new storge(name, copy.start, copy.start+large, large, 1,copy.algorithm));//表示现在将这一个分区加入
			storge temp = new storge("NULL", copy.start+large, copy.end, copy.capacity-large, 0,copy.algorithm);
			list.remove(copy);
			list.add(temp);
			System.out.println("分区分配成功");
			return;
		}	
	}
	public static void show() {
		System.out.println("分区名\t\t始址\t\t终址\t\t分区容量\t\t分区状态\t\t");
		for (int j = 0; j < len; j++) {
			for (int i = 0; i < list.size(); i++) {
				storge temporacy = list.get(i);
				if (j == temporacy.start && temporacy.capacity!=0) {
					System.out.println(temporacy.name+"\t\t"+temporacy.start+"\t\t"+
							+temporacy.end+"\t\t"+temporacy.capacity+"\t\t"+string[temporacy.status]+"\t\t");
				}
			}
		}
	}
}
/*
 * name代表分区名
 * algorithm 代表使用的是什么算法
 * capacity代表当前分区的容量
 * start代表分区的开始地址
 * end代表分区的结束地址
 * status代表当前的分区是空闲分区还是已占用分区,0代表空闲分区,1代表已经占用的分区
 */
class storge {
	String name;
	int start;
	int end;
	int capacity;
	int status;
	int algorithm;
	public storge(String name,int start,int end, int capacity,int status,int algorithm) {
		super();
		this.name=name;
		this.start=start;
		this.end=end;
		this.capacity=capacity;
		this.status=status;
		this.algorithm=algorithm;
	}
}


  • 7
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WeChat098

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值