动态分区分配--最先适应分配算法

可变分区调度算法有:
最先适应分配算法,最优适应分配算法,最坏适应算法。

用户提出内存空间的申请;系统根据申请者的要求,按照一定的分配策略分析内存空间的使用情况,找出能满足请求的空闲区,分给申请者;当程序执行完毕或主动归还内存资源时,系统要收回它所占用的内存空间或它归还的部分内存空间。

每当一个进程被创建时,内存分配程序首先要查找空闲内存分区表(链),从中寻找一个合适的空闲块进行划分,并修改空闲内存分区表(链)。当进程运行完毕释放内存时,系统根据回收区的首址,从空闲区表(链)中找到相应的插入点,此时出现如下四种情况:
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();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

daipuweiai

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

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

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

打赏作者

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

抵扣说明:

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

余额充值