6. Write a python program to demonstrate First Fit- Memory Management Technique
class MemoryManager:
def __init__(self, memory_size):
self.memory_size = memory_size
self.memory = [0] * memory_size
self.processes = {}
def allocate_first_fit(self, process_id, size):
for i in range(self.memory_size - size + 1):
if all(self.memory[i + j] == 0 for j in range(size)):
for j in range(size):
self.memory[i + j] = process_id
self.processes[process_id] = (i, size)
return True
return False
def print_memory(self):
print("Memory:")
for i, val in enumerate(self.memory):
print(val, end=" ")
if (i + 1) % 20 == 0:
print()
print()
if __name__ == "__main__":
memory_manager = MemoryManager(100)
# First fit allocation
print("First Fit Allocation:")
memory_manager.allocate_first_fit("P1", 20)
memory_manager.print_memory()
Output:
First Fit Allocation:
Memory:
P1 P1 P1 P1 P1 P1 P1 P1 P1 P1 P1 P1 P1 P1 P1 P1 P1 P1 P1 P1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0