From 71d10df55ea0f4496106e44c15471c64b46a5073 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Wed, 6 Nov 2024 15:24:34 +0530 Subject: [PATCH] Added first fit, next fit, best fit, and worst fir codes (a6). --- .../Best Fit.py | 28 +++++++++++++++++++ .../First Fit.py | 22 +++++++++++++++ .../Next Fit.py | 25 +++++++++++++++++ .../Worst Fit.py | 28 +++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 Codes/Python version/Assignment-A6 (Memory placement)/Best Fit.py create mode 100644 Codes/Python version/Assignment-A6 (Memory placement)/First Fit.py create mode 100644 Codes/Python version/Assignment-A6 (Memory placement)/Next Fit.py create mode 100644 Codes/Python version/Assignment-A6 (Memory placement)/Worst Fit.py diff --git a/Codes/Python version/Assignment-A6 (Memory placement)/Best Fit.py b/Codes/Python version/Assignment-A6 (Memory placement)/Best Fit.py new file mode 100644 index 0000000..4d515ca --- /dev/null +++ b/Codes/Python version/Assignment-A6 (Memory placement)/Best Fit.py @@ -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) diff --git a/Codes/Python version/Assignment-A6 (Memory placement)/First Fit.py b/Codes/Python version/Assignment-A6 (Memory placement)/First Fit.py new file mode 100644 index 0000000..f25b046 --- /dev/null +++ b/Codes/Python version/Assignment-A6 (Memory placement)/First Fit.py @@ -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) diff --git a/Codes/Python version/Assignment-A6 (Memory placement)/Next Fit.py b/Codes/Python version/Assignment-A6 (Memory placement)/Next Fit.py new file mode 100644 index 0000000..271fb21 --- /dev/null +++ b/Codes/Python version/Assignment-A6 (Memory placement)/Next Fit.py @@ -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) diff --git a/Codes/Python version/Assignment-A6 (Memory placement)/Worst Fit.py b/Codes/Python version/Assignment-A6 (Memory placement)/Worst Fit.py new file mode 100644 index 0000000..a0dc6e1 --- /dev/null +++ b/Codes/Python version/Assignment-A6 (Memory placement)/Worst Fit.py @@ -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)