先决条件:分区分配方法
最佳拟合将进程分配到一个分区,该分区是可用分区中最小的足够分区。
例子:
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
Implementation:
1- Input memory blocks and processes with sizes.
2- Initialize all memory blocks as free.
3- Start by picking each process and find the
minimum block size that can be assigned to
current process i.e., find min(bockSize[1],
blockSize[2],.....blockSize[n]) >
processSize[current], if found then assign
it to the current process.
5- If not then leave that process and keep checking
the further processes.
下面是实现。
C/C++
// C++ implementation of Best - Fit algorithm
#include
using namespace std;
// Function to allocate memory to blocks as per Best fit
// algorithm
void bestFit(int blockSize[], int m, int processSize[], int n)
{
// Stores block id of the block allocated to a
// process
int allocation[n];
// Initially no block is assigned to any process
memset(allocation, -1, sizeof(allocation));
// pick each process and find suitable blocks
// according to its size ad assign to it
for (int i=0; i= 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];
}
}
cout << "\nProcess No.\tProcess Size\tBlock no.\n";
for (int i = 0; i < n; i++)
{
cout << " " << i+1 << "\t\t" << processSize[i] << "\t\t";
if (allocation[i] != -1)
cout << allocation[i] + 1;
else
cout << "Not Allocated";
cout << endl;
}
}
// Driver code
int main()
{
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize)/sizeof(blockSize[0]);
int n = sizeof(processSize)/sizeof(processSize[0]);
bestFit(blockSize, m, processSize, n);
return 0 ;
}
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= 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
elif 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.
输出:
Process No. Process Size Block no.
1 212 4
2 417 2
3 112 3
4 426 5
最佳拟合真的是最好的吗?
虽然,最佳拟合最大限度地减少了浪费空间,但它会消耗大量处理器时间来搜索接近所需大小的块。此外,在某些情况下,Best-fit 的性能可能比其他算法差。例如,请参见下面的练习。
示例:考虑来自进程的请求,顺序为 300K、25K、125K 和 50K。假设有两个大小为 150K 的可用内存块,然后是一个大小为 350K 的块。
最合适:
300K 从大小为 350K 的块中分配。 50 留在块中。
从剩余的 50K 块中分配 25K。块中剩余 25K。
125K 是从 150 K 块中分配的。该块中还剩下 25K。
即使有 25K + 25K 可用空间,也无法分配 50K。
第一次拟合:
300K 请求从 350K 块中分配,50K 被省略。
25K 是从 150K 块中分配的,125K 被排除在外。
然后将 125K 和 50K 分配给剩余的遗漏分区。
所以,first fit 可以处理请求。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。