可变分区调度算法有: 最先适应分配算法,最优适应分配算法,最坏适应算法。
用户提出内存空间的申请;系统根据申请者的要求,按照一定的分配策略分析内存空间的使用情况,找出能满足请求的空闲区,分给申请者;当程序执行完毕或主动归还内存资源时,系统要收回它所占用的内存空间或它归还的部分内存空间。
每当一个进程被创建时,内存分配程序首先要查找空闲内存分区表(链),从中寻找一个合适的空闲块进行划分,并修改空闲内存分区表(链)。当进程运行完毕释放内存时,系统根据回收区的首址,从空闲区表(链)中找到相应的插入点,此时出现如下四种情况: 1) 回收区与插入点的前一个空闲分区F1相邻接,此时可将回收区直接与F1合并,并修改F1的大小; 2) 回收区与插入点的后一个空闲分区F2相邻接,此时可将回收区直接与F2合并,并用回收区的首址最为新空闲区的首址,大小为二者之和; 3) 回收区同时与插入点的前、后两个空闲分区邻接,此时需将三者合并; 4) 回收区不与任何一个空闲区邻接,此时应建一新的表项。
首先我们的构建一个分区表,及其相关操作,代码如下:
package 动态分区分配;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;
public class Partition implements Comparable<Partition>{
private int PartitionSize; //分区大小
private int StartLocation; //分区起始地址
private boolean IsBusy; //状态
public Partition(int partitionSize, int startLocation, boolean isBusy) {
super();
PartitionSize = partitionSize;
StartLocation = startLocation;
IsBusy = isBusy;
}
public int getPartitionSize() {
return PartitionSize;
}
public void setPartitionSize(int partitionSize) {
PartitionSize = partitionSize;
}
public boolean isIsBusy() {
return IsBusy;
}
public void setIsBusy(boolean isBusy) {
IsBusy = isBusy;
}
public int getStartLocation() {
return StartLocation;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + StartLocation;
return result;
}
@Override
public int compareTo(Partition arg0) {
// TODO Auto-generated method stub
if ( this.StartLocation < arg0.StartLocation)
return -1;
else if ( this.StartLocation > arg0.StartLocation)
return 1;
return 0;
}
public void setStartLocation(int startLocation) {
StartLocation = startLocation;
}
public void Print(){
System.out.print(this.PartitionSize+" "+this.StartLocation+" ");
if (this.isIsBusy()){
System.out.println("忙碌");
}else{
System.out.println("空闲");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("请初始化空闲分区表[分区大小(KB)和分区起始地址(K)]:");
TreeSet<Partition> partition = new TreeSet<Partition>();
for ( int i = 0 ; i < 5 ; i++){
int partitionSize = in.nextInt();
int startLocation = in.nextInt();
partition.add(new Partition(partitionSize, startLocation, false));
}
Iterator<Partition> it = partition.iterator();
int cnt = 1; //分区号
System.out.println("分区号"+" "+"分区大小(KB)"+" "+"分区起始地址(K)"+" "+"状态");
while ( it.hasNext()){
Partition p = it.next();
System.out.print(cnt+" ");
p.Print();
cnt++;
}
in.close();
}
}
之后开始设计最先适应分配算法,代码如下:
package 动态分区分配;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;
public class FirstFit {
private ArrayList<Partition> partition = new ArrayList<Partition>();
public FirstFit(TreeSet<Partition> partition) { //构造方法
Iterator<Partition> it = partition.iterator();
while(it.hasNext()){
Partition p = it.next();
this.partition.add(p);
}
}
public ArrayList<Partition> getPartition() {
return partition;
}
public void CarryOut_FirstFit(int[] process){ //执行最先适应算法
int index = 0;
int cnt = 0;
while( cnt < process.length){
Partition p = this.getPartition().get(index);
if ( p.getPartitionSize() >= process[cnt]){
p.setIsBusy(true);
int restsize = p.getPartitionSize() - process[cnt];
p.setPartitionSize(process[cnt]);
if ( index == 0){
Partition after = this.getPartition().get(index+1);
if ( after.isIsBusy() == false ){
after.setPartitionSize(after.getPartitionSize()+restsize);
after.setStartLocation(p.getStartLocation()+p.getPartitionSize()+1);
cnt++;
index++;
}
}else if ( index == this.getPartition().size() - 1){
Partition before = this.getPartition().get(index-1);
if ( before.isIsBusy() == false){
before.setPartitionSize(before.getPartitionSize()+restsize);
before.setStartLocation(p.getStartLocation()+p.getPartitionSize()+1);
cnt++;
index++;
}
}else{
Partition after = this.getPartition().get(index+1);
Partition before1 = this.getPartition().get(index-1);
if ( before1.isIsBusy() == false){
before1.setPartitionSize(before1.getPartitionSize()+restsize);
before1.setStartLocation(p.getStartLocation()+p.getPartitionSize()+1);
cnt++;
index++;
continue;
}else if ( after.isIsBusy() == false ){
after.setPartitionSize(after.getPartitionSize()+restsize);
after.setStartLocation(p.getStartLocation()+p.getPartitionSize()+1);
cnt++;
index++;
continue;
}
}
}else{
continue;
}
}
}
public void Print(){ //打印
System.out.println("分区号"+" "+"分区大小(KB)"+" "+"分区起始地址(K)"+" "+"状态");
Iterator<Partition> it = this.getPartition().iterator();
int cnt = 1;
while ( it.hasNext()){
Partition tmp = it.next();
System.out.print(cnt+" ");
tmp.Print();
cnt++;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("请输入分区数:");
int cnt = in.nextInt();
TreeSet<Partition> p = new TreeSet<Partition>();
System.out.println("开始初始化分区表:");
for ( int i = 0 ; i < cnt ; i++){
int partitionSize = in.nextInt();
int startLocation = in.nextInt();
p.add(new Partition(partitionSize, startLocation, false));
}
FirstFit firstfit = new FirstFit(p);
int[] process = new int[2];
System.out.println(" 开始执行最先适应算法");
System.out.println("请输入进程需分配的内存空间:");
for ( int i = 0 ; i < 2 ; i++){
process[i] = in.nextInt();
}
System.out.println("进行动态分配前空闲分区表为:");
firstfit.Print();
firstfit.CarryOut_FirstFit(process);
System.out.println("进行动态分配后空闲分区表为:");
firstfit.Print();
in.close();
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有