操作系统之可变分区存储管理模拟代码

1、分区表:

       系统设置空闲分区表和已分配分区表,为系统空间分配提供依据。已分配分区表记录已装入的作业占用分区的始址和长度,用标志位指出占用该分区的作业名。空闲分区表记录主存中可供分配的空闲分区的始址和长度。

2、分区链:

       为了实现对空闲分区的分配和链接,在每个分区的起始部分设置一些用于控制分区分配的信息以及用于连接前一个分区的前向指针;在分区尾部则设置一个后向指针,通过前、后链接指针,可以将所有的空闲分区链接成一个双向链。

3、可变分区分配算法

最先适应算法:在空闲区间中查询满足作业需要的空间,并将作业装入第一个满足条件的空间中去。

● 最佳适应算法:在空闲区间中查询满足作业需要的空间,并将作业装入满足条件的空闲空间中最小的一个空间中去。

● 最坏适应算法:在空闲区间中查询满足作业需要的空间,并将作业装入满足条件的空闲空间中最大的一个空间中去。

以下代码用了分区链以及三种分配算法模拟了可变分区存储管理的过程

package MemoryManagement;

import java.io.IOException;
import java.util.Scanner;

//1、初始化用户分区-》显示分区
//2、选择功能
//有以下功能
//1)、存储作业
	//(1)、二级菜单,选择算法
	//执行完显示分区
//2)、回收作业
	//执行完显示分区

public class MemoryManagement {
	 public static void main(String args[]) {
		 MainClass c = new MainClass();
		 c.execInstruction();
	 }
}
class MainClass{
	static int SystemMemory = 500;//系统初始容量
	//单个作业信息类
	class Job{
		String name;
		int begin;
		int lenth;
		Job lastJob;
		Job nextJob;
	}
	//空闲分区信息类
	class freeSpace{
		freeSpace lastSpace;//指向上一个空间
		freeSpace nextSpace;//指向下一个空间
		int begin;
		int lenth;
		
	}
	//作业链表类
	class JobLine{
		Job firstJob;
		boolean isFull = true;
	}
	//空闲区域链表类
	class freeSpaceLine{
		freeSpace firstSpace;
		boolean isFull = true;
	}
	//定义一个选择菜单
		public int getMenu() {
			System.out.println("请你选择需要进行的操作:");
			System.out.println("1:装入作业");
			System.out.println("2:回收作业");
			System.out.println("3:显示系统当前分区情况");
			System.out.println("4:退出程序");
			Scanner scan = new Scanner(System.in);
			int select  = scan.nextInt();
			if(1 == select) {
				System.out.println("请你选择装入作业时要使用的算法:");
				System.out.println("1:最先适应分配算法");
				System.out.println("2:最优适应分配算法");
				System.out.println("3:最坏适应分配算法");
				Scanner scan1 = new Scanner(System.in);
				int select1  = scan1.nextInt();
				if(1 == select1)
					return 11;
				else if(2 == select1)
					return 12;
				else if(3 == select1)
					return 13;
				else{
					System.out.println("你想干嘛");	
					return -1;
				}
			}
			else if(2 == select) {
				return 2;
			}
			else if(3 == select) {
				return 3;
			}
			else if(4 == select) {
				return 0;
			}
			return -1;
		}
		//执行用户输入的命令
		 public void execInstruction() {
			int select = getMenu();
			initialFreeArea();
			while(0 != select) {
				if(11 == select||12 == select||13 == select)
					storeHomework(select);
				else if(2 == select) {
					System.out.println("请输入要回收的作业的名称");
					Scanner in = new Scanner(System.in);
					String temp = in.next();
					recycleJob(temp);
					printSituation();
				}
				else if(3 == select){
					printSituation();
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				select = getMenu();
			}
		}
		freeSpaceLine freeLineOne;
		JobLine jobLine;
		//初始化用户区
		public void initialFreeArea() {
			freeLineOne = new freeSpaceLine();
			freeSpace first = new freeSpace();
			freeLineOne.firstSpace = first;
			freeLineOne.isFull = false;
			first.begin = 0;
			first.lenth = SystemMemory;
			first.lastSpace = null;
			first.nextSpace = null;
		}
		public void printSituation() {
			System.out.println("********************************");
			System.out.println("     已分配分区表\n");
			System.out.println("始址\t长度\t标志位\t");
			System.out.println("--------------------------------");
			Job temp  = jobLine.firstJob;
			while(temp != null) {
				System.out.println(temp.begin + "\t" + temp.lenth + "\t" + temp.name + "\t");
				temp = temp.nextJob;
			}
			
			System.out.println("--------------------------------");
			System.out.println("     空闲分区表\n");
			System.out.println("始址\t长度\t标志位\t");
			System.out.println("--------------------------------");
			freeSpace temp2 = freeLineOne.firstSpace;
			while(temp2 != null) {
				System.out.println(temp2.begin + "\t" + temp2.lenth + "\t" + "未分配\t");
				temp2 = temp2.nextSpace;
			}
			System.out.println("--------------------------------");
			System.out.println("********************************");
			System.out.println("\n\n\n\n");
		}
		//输入作业
		 
//1、创建一个temp临时变量(job类)
//2、将信息输入经temp类
//3、在空闲用户区找合适的块
//4、如果找得到则将temp存进作业链表
//5、重置空闲区
//6、打印情况
		public Job inputJob() {
			Job tempJob = new Job();
			Scanner scan1 = new Scanner(System.in);
			System.out.println("请输入作业的名称");
			tempJob.name  = scan1.next();
			System.out.println("请输入作业的长度");
			tempJob.lenth = scan1.nextInt();
			tempJob.nextJob = null;
			tempJob.lastJob = null;
			return tempJob;
		}
		//判断内存是否足够存入作业
		public  boolean lookForEnoughMemory(Job tempJob) {
			if(freeLineOne != null) {
				if(freeLineOne.isFull != true) {
					freeSpace temp = freeLineOne.firstSpace;
					while(temp != null) {
						if(temp.lenth >= tempJob.lenth)
							return true;
						temp = temp.nextSpace;
					}
				}
			}
			return false;
		}
		//如果内存足够则存入作业
		public void storeHomework(int selectArithmetic) {
			Job tempJob = inputJob();
			if(lookForEnoughMemory(tempJob) == true) {
				if(11 == selectArithmetic) {
					freeSpace space = firstFit(tempJob);
					apllicationSpace(tempJob,space);
					insertJob(tempJob);
				}
				else if(12 == selectArithmetic) {
					freeSpace space = bestFit(tempJob);
					apllicationSpace(tempJob,space);
					insertJob(tempJob);
					
				}else if(13 == selectArithmetic) {
					freeSpace space = worstFit(tempJob);
					apllicationSpace(tempJob,space);
					insertJob(tempJob);
				}
				printSituation();
			}
			else 
				System.out.println("系统内存不足,请稍后再试");
		}
		public void apllicationSpace(Job tempJob,freeSpace space) {
			int jobLenth = tempJob.lenth;
			int spaceBegin = space.begin;
			int spaceLenth = space.lenth;
			tempJob.begin = spaceBegin;
			space.begin = spaceBegin + jobLenth;
			space.lenth = spaceLenth - jobLenth;
			if(0 == space.lenth) {
				if(space.lastSpace != null && space.nextSpace != null)
					space.lastSpace.nextSpace = space.nextSpace;
				if(space.nextSpace != null && space.lastSpace != null)
				space.nextSpace.lastSpace = space.lastSpace;
			}
		}
		public void insertJob(Job tempJob) {
			if(jobLine == null) {
				jobLine = new JobLine();
				jobLine.firstJob = tempJob;
				return;
			}
			else if(jobLine.firstJob == null) {
				jobLine.firstJob = tempJob;
				return;
			}
			Job temp = jobLine.firstJob;
			while(temp.nextJob != null) {
				temp = temp.nextJob;
			}
			temp.nextJob = tempJob;
			tempJob.lastJob = temp;
			
		}
		//最先适应分配算法
		public freeSpace firstFit(Job tempJob) {
			freeSpace temp = freeLineOne.firstSpace;
			while(temp != null) {
				if(temp.lenth >= tempJob.lenth){
					return temp;
				}
				temp = temp.nextSpace;
			}
			return null;
			
		}
		//最优适应分配算法
		public freeSpace bestFit(Job tempJob) {
			freeSpace temp = freeLineOne.firstSpace;
			freeSpace min = null;
			while(null != temp) {
				if(min == null && temp.lenth >= tempJob.lenth) {
					min = temp;
				}
				else if(temp.lenth >= tempJob.lenth && temp.lenth < min.lenth){
					min = temp;
				}
				temp = temp.nextSpace;
			}
			return min;
		}
		//最坏适应分配算法
		public freeSpace worstFit(Job tempJob) {
			freeSpace temp = freeLineOne.firstSpace;
			freeSpace max = freeLineOne.firstSpace;
			while(null != temp) {
				if(temp.lenth >= tempJob.lenth && temp.lenth > max.lenth){
					max = temp;
				}
				temp = temp.nextSpace;
			}
			return max;
		}
		public void recycleJob(String name) {
			//1、找到与输入相同的Job
			Job address = jobLine.firstJob;
			Job sameJob = null;
			while(address != null) {
				if(address.name.equalsIgnoreCase(name)) {
					sameJob = address;
					break;
				}
				address = address.nextJob;
			}
			//按顺序插入空闲区域队列
			if(sameJob != null) {
				//改变空闲区
				freeSpace tempFree1 = freeLineOne.firstSpace;
				freeSpace tempFree2  = new freeSpace();
				tempFree2.begin = sameJob.begin;
				tempFree2.lenth = sameJob.lenth;
				while(tempFree1 != null) {
					if( tempFree1.begin < sameJob.begin ) {
						if(tempFree1.nextSpace == null) {
							tempFree1.nextSpace = tempFree2;
							tempFree2.lastSpace = tempFree1;
							tempFree2.nextSpace = null;
							defragmentation();
							break;
						}	
						else if(tempFree1.nextSpace != null && tempFree1.nextSpace.begin > sameJob.begin) {
							freeSpace temp3 = tempFree1.nextSpace;
							tempFree1.nextSpace = tempFree2;
							tempFree2.nextSpace = temp3;
							tempFree2.lastSpace = tempFree1;
							temp3.lastSpace = tempFree2; 
							defragmentation();
							break;
						}	
					}
					else if(tempFree1.begin > sameJob.begin){
						tempFree2.lastSpace = null;
						tempFree2.nextSpace = tempFree1;
						tempFree1.lastSpace = tempFree2;
						freeLineOne.firstSpace = tempFree2;
						defragmentation();
						break;
					}
					tempFree1 = tempFree1.nextSpace;
				}
				//改变作业区
				if(sameJob.lastJob == null) {
					if(sameJob.nextJob != null) {
						jobLine.firstJob = sameJob.nextJob;
						sameJob.nextJob.lastJob = null;
					}
					else {
						jobLine.firstJob = null;
					}
						
				}
				else if(sameJob.nextJob == null) {
					sameJob.lastJob.nextJob = null;
				}
				else if(sameJob.nextJob != null) {
					Job temp1 = sameJob.lastJob;
					Job temp2 = sameJob.nextJob;
					temp1.nextJob = temp2;
					temp2.lastJob = temp1;
				}

			}
		}
		public void defragmentation() {
			//回收的作业在空闲区的前方
			freeSpace tempSpace1 = freeLineOne.firstSpace;
			freeSpace tempSpace2;
			boolean conbine = false;
			while(tempSpace1.nextSpace != null) {
			if(tempSpace1.begin + tempSpace1.lenth == tempSpace1.nextSpace.begin) {
				tempSpace1.lenth += tempSpace1.nextSpace.lenth;
				tempSpace2 = tempSpace1.nextSpace.nextSpace;
				tempSpace1.nextSpace = tempSpace2;
				if(tempSpace2 != null) {
					tempSpace2.lastSpace = tempSpace1;
				}
				conbine = true;
			}
			tempSpace1 = tempSpace1.nextSpace;
			if(conbine == true) {
				tempSpace1 = freeLineOne.firstSpace;
				conbine = false;
			}
		}	
		}
 }

以下是运行样例:

最先适应分配算法:

最优适应分配算法:

 最坏适应分配算法:

回收功能:

 

 

 

  • 9
    点赞
  • 95
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值