3
1
SystemsProgrammingAndOperat.../Codes/Python version/Assignment-A6 (Memory placement)/Worst Fit.py

29 lines
918 B
Python
Raw Permalink Normal View History

def worst_fit(memory_blocks, process_sizes):
allocation = [-1] * len(process_sizes)
for i in range(len(process_sizes)):
worst_idx = -1
worst_size = -1
for j in range(len(memory_blocks)):
if memory_blocks[j] >= process_sizes[i] and memory_blocks[j] > worst_size:
worst_size = memory_blocks[j]
worst_idx = j
if worst_idx != -1:
allocation[i] = worst_idx
memory_blocks[worst_idx] -= process_sizes[i]
print("\nWorst 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]
worst_fit(memory_blocks, process_sizes)