Requisito previo: Métodos de asignación de particiones
El mejor ajuste asigna el proceso a una partición que es la partición más pequeña suficiente entre las particiones libres disponibles.
Ejemplo:
Input : blockSize[] = {100, 500, 200, 300, 600}; processSize[] = {212, 417, 112, 426}; Output: Process No. Process Size Block no. 1 212 4 2 417 2 3 112 3 4 426 5
Java
// Java implementation of Best - Fit algorithm public class GFG { // Method to allocate memory to blocks as per Best fit // algorithm static void bestFit(int blockSize[], int m, int processSize[], int n) { // Stores block id of the block allocated to a // process int allocation[] = new int[n]; // Initially no block is assigned to any process for (int i = 0; i < allocation.length; i++) allocation[i] = -1; // pick each process and find suitable blocks // according to its size ad assign to it for (int i=0; i<n; i++) { // Find the best fit block for current process int bestIdx = -1; for (int j=0; j<m; j++) { if (blockSize[j] >= processSize[i]) { if (bestIdx == -1) bestIdx = j; else if (blockSize[bestIdx] > blockSize[j]) bestIdx = j; } } // If we could find a block for current process if (bestIdx != -1) { // allocate block j to p[i] process allocation[i] = bestIdx; // Reduce available memory in this block. blockSize[bestIdx] -= processSize[i]; } } System.out.println("\nProcess No.\tProcess Size\tBlock no."); for (int i = 0; i < n; i++) { System.out.print(" " + (i+1) + "\t\t" + processSize[i] + "\t\t"); if (allocation[i] != -1) System.out.print(allocation[i] + 1); else System.out.print("Not Allocated"); System.out.println(); } } // Driver Method public static void main(String[] args) { int blockSize[] = {100, 500, 200, 300, 600}; int processSize[] = {212, 417, 112, 426}; int m = blockSize.length; int n = processSize.length; bestFit(blockSize, m, processSize, n); } }
Python3
# Python3 implementation of Best - Fit algorithm # Function to allocate memory to blocks # as per Best fit algorithm def bestFit(blockSize, m, processSize, n): # Stores block id of the block # allocated to a process allocation = [-1] * n # pick each process and find suitable # blocks according to its size ad # assign to it for i in range(n): # Find the best fit block for # current process bestIdx = -1 for j in range(m): if blockSize[j] >= processSize[i]: if bestIdx == -1: bestIdx = j else if blockSize[bestIdx] > blockSize[j]: bestIdx = j # If we could find a block for # current process if bestIdx != -1: # allocate block j to p[i] process allocation[i] = bestIdx # Reduce available memory in this block. blockSize[bestIdx] -= processSize[i] print("Process No. Process Size Block no.") for i in range(n): print(i + 1, " ", processSize[i], end = " ") if allocation[i] != -1: print(allocation[i] + 1) else: print("Not Allocated") # Driver code if __name__ == '__main__': blockSize = [100, 500, 200, 300, 600] processSize = [212, 417, 112, 426] m = len(blockSize) n = len(processSize) bestFit(blockSize, m, processSize, n) # This code is contributed by PranchalK
C#
// C# implementation of Best - Fit algorithm using System; public class GFG { // Method to allocate memory to blocks // as per Best fit // algorithm static void bestFit(int []blockSize, int m, int []processSize, int n) { // Stores block id of the block // allocated to a process int []allocation = new int[n]; // Initially no block is assigned to // any process for (int i = 0; i < allocation.Length; i++) allocation[i] = -1; // pick each process and find suitable // blocks according to its size ad // assign to it for (int i = 0; i < n; i++) { // Find the best fit block for // current process int bestIdx = -1; for (int j = 0; j < m; j++) { if (blockSize[j] >= processSize[i]) { if (bestIdx == -1) bestIdx = j; else if (blockSize[bestIdx] > blockSize[j]) bestIdx = j; } } // If we could find a block for // current process if (bestIdx != -1) { // allocate block j to p[i] // process allocation[i] = bestIdx; // Reduce available memory in // this block. blockSize[bestIdx] -= processSize[i]; } } Console.WriteLine("\nProcess No.\tProcess" + " Size\tBlock no."); for (int i = 0; i < n; i++) { Console.Write(" " + (i+1) + "\t\t" + processSize[i] + "\t\t"); if (allocation[i] != -1) Console.Write(allocation[i] + 1); else Console.Write("Not Allocated"); Console.WriteLine(); } } // Driver Method public static void Main() { int []blockSize = {100, 500, 200, 300, 600}; int []processSize = {212, 417, 112, 426}; int m = blockSize.Length; int n = processSize.Length; bestFit(blockSize, m, processSize, n); } } // This code is contributed by nitin mittal.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA