29 lines
955 B
Python
29 lines
955 B
Python
|
def best_fit(memory_blocks, process_sizes):
|
||
|
allocation = [-1] * len(process_sizes)
|
||
|
|
||
|
for i in range(len(process_sizes)):
|
||
|
best_idx = -1
|
||
|
best_size = float('inf')
|
||
|
|
||
|
for j in range(len(memory_blocks)):
|
||
|
if memory_blocks[j] >= process_sizes[i] and memory_blocks[j] - process_sizes[i] < best_size:
|
||
|
best_size = memory_blocks[j] - process_sizes[i]
|
||
|
best_idx = j
|
||
|
|
||
|
if best_idx != -1:
|
||
|
allocation[i] = best_idx
|
||
|
memory_blocks[best_idx] -= process_sizes[i]
|
||
|
|
||
|
print("\nBest Fit Allocation:")
|
||
|
for i in range(len(process_sizes)):
|
||
|
if allocation[i] != -1:
|
||
|
print(f"Process {i+1} allocated to Block {allocation[i]+1}")
|
||
|
else:
|
||
|
print(f"Process {i+1} not allocated")
|
||
|
|
||
|
# Example Memory Blocks and Process Sizes
|
||
|
memory_blocks = [100, 500, 200, 300, 600]
|
||
|
process_sizes = [212, 417, 112, 426]
|
||
|
|
||
|
best_fit(memory_blocks, process_sizes)
|