-
[Develop] OS 메모리 할당 알고리즘Mhwan's Develope/JAVA 2020. 1. 3. 00:34
## 기능
- 운영체제에는 동적인 메모리 할당 알고리즘으로 First fit, Next fit, Best fit이 있다.
- 이것을 모두 구현함
## 코드
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889import 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]);elseSystem.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's Develope > JAVA' 카테고리의 다른 글
[Develop] 다중쓰레드로 40x40행렬 곱셈 계산 (0) 2020.01.03 [Tip] 날짜 관련 유용하게 쓰일 변환하는 법 (Using SimpleDateFormat) (0) 2016.11.02 [Tip] 문자열 바이트 세기 (String Text Byte) (0) 2016.10.27 [Tip] JAVA 각종 유용한 정규식 (0) 2016.10.25