ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Develop] OS 메모리 할당 알고리즘
    Mhwan's Develope/JAVA 2020. 1. 3. 00:34

    ## 기능

     - 운영체제에는 동적인 메모리 할당 알고리즘으로 First fit, Next fit, Best fit이 있다.

     - 이것을 모두 구현함

     

    ## 코드

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.LinkedList;
     
    public class MemoryAllocator {
        public void bestFit(LinkedList<MemoryBlock> bList, ArrayList<Integer> pList, int m, int n) {
            int allocation[] = new int[n];
            Arrays.fill(allocation, -1);
            int count = 0;
            for (int i = 0; i < n; i++) {
                int bestIdx = -1;
                for (int j = 0; j < m; j++) {
                    count++;
                    if (bList.get(j).getMemory() >= pList.get(i)) {
                        if (bestIdx == -1)
                            bestIdx = j;
                        else if (bList.get(bestIdx).getMemory()> bList.get(j).getMemory())
                            bestIdx = j;
                    }
                }
                if (bestIdx != -1) {
                    allocation[i] = bestIdx;
                    bList.get(bestIdx).allocateProcess(pList.get(i));
                }
            }
            System.out.println("\n-- BestFit");
            printAllocation(pList, bList, allocation, count);
        }
     
        public void nextFit(LinkedList<MemoryBlock> bList, ArrayList<Integer> pList, int m, int n) {
            int allocation[] = new int[n], j = 0;
            Arrays.fill(allocation, -1);
            int count = 0;
            for (int i = 0; i < n; i++) {
                for (; j < m; j=(j+1)%m){
                    count++;
                    if (!(bList.get(j).getMemory() < pList.get(i))) {
                        allocation[i] = j;
                        bList.get(j).allocateProcess(pList.get(i));
     
                        break;
                    }
                }
            }
            System.out.println("\n-- NextFit");
            printAllocation(pList, bList, allocation, count);
        }
     
        public void firstFit(LinkedList<MemoryBlock> bList, ArrayList<Integer> pList, int m, int n) {
            int[] allocation = new int[n];
            Arrays.fill(allocation, -1);
     
            int count = 0;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    count++;
                    if (!(bList.get(j).getMemory() < pList.get(i))) {
                        allocation[i] = j;
                        bList.get(j).allocateProcess(pList.get(i));
     
                        break;
                    }
                }
            }
            System.out.println("-- FirstFit");
            printAllocation(pList, bList, allocation, count);
        }
     
        private void printAllocation(ArrayList<Integer> processList, LinkedList<MemoryBlock> blockList, int[] allocation, int count) {
            System.out.println("Process No.\tProcess Size\tBlock no.");
            for (int i = 0; i < processList.size(); i++) {
                System.out.print(" " + (i));
                System.out.print("\t\t "+processList.get(i));
                if (allocation[i] != -1)
                    System.out.print("\t\t "+allocation[i]);
                else
                    System.out.printf("\t\t Not Allocated");
                System.out.println();
            }
            System.out.println("Usable Block List");
            for (MemoryBlock block : blockList) {
                System.out.print(block.getMemory() + " ");
                block.releaseAllProcess();
            }
            System.out.println("\nTotal count of Comparison\n"+count);
     
            System.out.printf("Average count of Comparison\n%.1f\n", ((double)count/processList.size()));
        }
    }
    cs

     

    ## 링크

    https://github.com/mhwan/MemoryAllocation

     

    mhwan/MemoryAllocation

    This is Algorithm for how allocated to memory in the operating system - mhwan/MemoryAllocation

    github.com

     

    댓글

Designed by Mhwan.