3
1

Added first fit, next fit, best fit, and worst fir codes (a6).

This commit is contained in:
K 2024-11-06 15:24:34 +05:30
parent 003d76229a
commit 71d10df55e
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F
4 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,28 @@
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)

View File

@ -0,0 +1,22 @@
def first_fit(memory_blocks, process_sizes):
allocation = [-1] * len(process_sizes)
for i in range(len(process_sizes)):
for j in range(len(memory_blocks)):
if memory_blocks[j] >= process_sizes[i]:
allocation[i] = j
memory_blocks[j] -= process_sizes[i]
break
print("\nFirst 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]
first_fit(memory_blocks, process_sizes)

View File

@ -0,0 +1,25 @@
def next_fit(memory_blocks, process_sizes):
allocation = [-1] * len(process_sizes)
next_index = 0
for i in range(len(process_sizes)):
while next_index < len(memory_blocks):
if memory_blocks[next_index] >= process_sizes[i]:
allocation[i] = next_index
memory_blocks[next_index] -= process_sizes[i]
next_index = (next_index + 1) % len(memory_blocks) # Move to next block
break
next_index += 1
print("\nNext 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]
next_fit(memory_blocks, process_sizes)

View File

@ -0,0 +1,28 @@
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)