Dynamic Distributed Assignment--First Adaptive Assignment Algorithms

Keywords: Java

Variable partition scheduling algorithms include:
Firstly, adaptive allocation algorithm, optimal adaptive allocation algorithm and worst adaptive algorithm are adopted.

Users apply for memory space; the system analyses the use of memory space according to the request of the applicant and according to certain allocation strategy, finds out the free area that can satisfy the request and distributes it to the applicant; when the program is executed or voluntarily returns the memory resources, the system should reclaim the memory space it occupies or what it returns. Partial memory space.

Whenever a process is created, the memory allocator first looks for the free memory partition table (chain), finds an appropriate free block to partition, and modifies the free memory partition table (chain). When the process has finished releasing the memory, the system finds the corresponding insertion point from the free area list (chain) according to the first address of the recycled area. At this time, there are four situations as follows:
1) Recycling zone is adjacent to F1, which is the first free zone of insertion point. At this time, the reclaimed zone can be merged with F1 directly, and the size of F1 can be modified.
2) The recycling area is adjacent to F2, which is the last free area of insertion point. At this time, the recycling area can be merged with F2 directly, and the first address of the recycling area is the first address of the newest free area, the size of which is the sum of the two.
3) The recovery zone is adjacent to the two free zones before and after the insertion point, and the three zones need to be merged at this time.
4) Recycling area is not adjacent to any free area, so a new table item should be built at this time.

First, we build a partition table and its related operations. The code is as follows:

package Dynamic Distribution;

import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;

public class Partition implements Comparable<Partition>{
    private int PartitionSize;                  //Partition size
    private int StartLocation;                  //Partition Start Address
    private boolean IsBusy;                     //state

    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("Be busy");
        }else{
            System.out.println("free");
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        System.out.println("Initialize the free partition table[Partition size(KB)And partition start address(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;                                            //Zoning number
        System.out.println("Zoning number"+"  "+"Partition size(KB)"+"  "+"Partition Start Address(K)"+" "+"state");
        while ( it.hasNext()){
            Partition p = it.next();
            System.out.print(cnt+"  ");
            p.Print();
            cnt++;
        }
        in.close();
    }
}

Then we begin to design the first adaptive allocation algorithm. The code is as follows:

package Dynamic Distribution;

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) {                     //Construction method
        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){                   //Executing First Adaptation Algorithms
        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(){                                //Printing
        System.out.println("Zoning number"+"  "+"Partition size(KB)"+"  "+"Partition Start Address(K)"+" "+"state");
        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("Please enter the number of partitions:");
        int cnt = in.nextInt();
        TreeSet<Partition> p = new TreeSet<Partition>();
        System.out.println("Start initializing partition tables:");
        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("        Start executing the first adaptation algorithm");
        System.out.println("Please enter the memory space allocated by the process:");
        for ( int i = 0 ; i < 2 ; i++){
            process[i] = in.nextInt();
        }
        System.out.println("The free partition table before dynamic allocation is as follows:");
        firstfit.Print();
        firstfit.CarryOut_FirstFit(process);
        System.out.println("After dynamic allocation, the free partition table is as follows:");
        firstfit.Print();

        in.close();
    }

}

Posted by mark_kccs on Mon, 15 Jul 2019 15:18:02 -0700