OS WEEK5 (1)
OS WEEK5 (1)
WEEK 5 CODES
OUTPUT :
Starting worst-fit memory allocation...
5.2 . CentralAI
PROGRAM :
class MemoryBlock:
def __init__(self, size):
self.size = size
self.is_free = True
class CentralAI:
def __init__(self, total_memory):
# Initially, CentralAI has one big free block of the entire memory.
self.memory_blocks = [MemoryBlock(total_memory)]
self.total_memory = total_memory
self.allocated_memory = 0
def best_fit_allocate(self, subsystem_name, requested_memory):
# Find the best-fit block (smallest block that is large enough)
best_fit_index = None
best_fit_size = float('inf')
for i, block in enumerate(self.memory_blocks):
if block.is_free and block.size >= requested_memory and block.size < best_fit_size:
best_fit_index = i
best_fit_size = block.size
if best_fit_index is not None:
block = self.memory_blocks[best_fit_index]
block.is_free = False
self.allocated_memory += requested_memory
print(f"{subsystem_name} allocated {requested_memory} units in block of {block.size}
units.")
OUTPUT :
SubsysA allocated 1500 units in block of 8000 units.
SubsysB allocated 1000 units in block of 6500 units.
SubsysC allocated 700 units in block of 5500 units.
SubsysD allocated 2200 units in block of 4800 units.
SubsysE allocated 500 units in block of 2600 units.
SubsysF allocated 1200 units in block of 2100 units.
Memory Utilization:
Total memory used: 7100 units
Fragmentation Analysis:
Free memory blocks: [900]
Total free memory: 900 units
5.3 . MetroCentral
PROGRAM :
class MemoryBlock:
def __init__(self, size):
self.size = size
self.is_free = True
class MetroCentral:
def __init__(self, total_memory):
# Initialize with a single large block of memory
self.memory_blocks = [MemoryBlock(total_memory)]
self.total_memory = total_memory
self.allocated_memory = 0
def first_fit_allocate(self, subsystem_name, requested_memory):
# Iterate through memory blocks to find the first free block that fits
for i, block in enumerate(self.memory_blocks):
if block.is_free and block.size >= requested_memory:
# Allocate memory in this block
block.is_free = False
self.allocated_memory += requested_memory
print(f"{subsystem_name} allocated {requested_memory} units in block of {block.size}
units.")
# Split the block if there's leftover memory
if block.size > requested_memory:
remaining_size = block.size - requested_memory
block.size = requested_memory
# Insert a new block with the remaining size right after the allocated block
self.memory_blocks.insert(i + 1, MemoryBlock(remaining_size))
return
# If no suitable block is found
print(f"Not enough memory to allocate {requested_memory} units to {subsystem_name}.")
def memory_utilization(self):
return self.allocated_memory
def fragmentation(self):
# Collect information about free blocks and total free memory
free_blocks = [block.size for block in self.memory_blocks if block.is_free]
total_free_memory = sum(free_blocks)
return free_blocks, total_free_memory
# Initialize MetroCentral with 5000 units of memory
metro_central = MetroCentral(5000)
# Subsystem memory requests
subsystems = {
"TrafficControl": 1200,
"RoutePlanning": 800,
"VehicleMonitoring": 1500,
"PassengerInformation": 600,
"MaintenanceLogistics": 700,
}
# Allocate memory using the first-fit technique for each subsystem
for subsystem, memory in subsystems.items():
metro_central.first_fit_allocate(subsystem, memory)
# Report total memory utilization
print("\nMemory Utilization:")
print(f"Total memory used: {metro_central.memory_utilization()} units")
# Fragmentation analysis
free_blocks, total_free_memory = metro_central.fragmentation()
print("\nFragmentation Analysis:")
print(f"Free memory blocks: {free_blocks}")
print(f"Total free memory: {total_free_memory} units")
OUTPUT :
TrafficControl allocated 1200 units in block of 5000 units.
RoutePlanning allocated 800 units in block of 3800 units.
VehicleMonitoring allocated 1500 units in block of 3000 units.
PassengerInformation allocated 600 units in block of 1500 units.
MaintenanceLogistics allocated 700 units in block of 900 units.
Memory Utilization:
Total memory used: 4800 units
Fragmentation Analysis:
Free memory blocks: [200]
Total free memory: 200 units
5.4 . AI Lab
PROGRAM :
class MemoryBlock:
def __init__(self, size):
self.size = size
self.is_free = True
class AILab:
def __init__(self, total_memory):
# Initially, there is one large memory block
self.memory_blocks = [MemoryBlock(total_memory)]
self.total_memory = total_memory
self.allocated_memory = 0
def first_fit_allocate(self, project_name, requested_memory):
# Iterate over memory blocks to find the first free block that fits the requested memory
for i, block in enumerate(self.memory_blocks):
if block.is_free and block.size >= requested_memory:
# Allocate memory in this block
block.is_free = False
self.allocated_memory += requested_memory
print(f"{project_name} allocated {requested_memory} units in block of {block.size}
units.")
# If block is larger than needed, split the block
if block.size > requested_memory:
remaining_size = block.size - requested_memory
block.size = requested_memory
self.memory_blocks.insert(i + 1, MemoryBlock(remaining_size))
return
# If no suitable block is found
print(f"Not enough memory to allocate {requested_memory} units to {project_name}.")
def memory_utilization(self):
return self.allocated_memory
def fragmentation(self):
# Find all free blocks and sum them up to get fragmentation info
free_blocks = [block.size for block in self.memory_blocks if block.is_free]
total_free_memory = sum(free_blocks)
return free_blocks, total_free_memory
# Initialize the AI Lab with 6000 units of memory
ai_lab = AILab(6000)
# Project memory requests
projects = {
"Project A": 1500,
"Project B": 1000,
"Project C": 700,
"Project D": 2200,
"Project E": 500,
"Project F": 1200,
}
# Allocate memory using the first-fit technique for each project
for project, memory in projects.items():
ai_lab.first_fit_allocate(project, memory)
# Report total memory utilization
print("\nMemory Utilization:")
print(f"Total memory used: {ai_lab.memory_utilization()} units")
# Fragmentation analysis
free_blocks, total_free_memory = ai_lab.fragmentation()
print("\nFragmentation Analysis:")
print(f"Free memory blocks: {free_blocks}")
print(f"Total free memory: {total_free_memory} units")
OUTPUT :
Memory Utilization:
Total memory used: 5900 units
Fragmentation Analysis:
Free memory blocks: [100]
Total free memory: 100 units
5.5 . MedTech Hospital
PROGRAM :
class MemoryBlock:
def __init__(self, size):
self.size = size
self.is_free = True
class MedTechHospital:
def __init__(self, total_memory):
# Initially, the memory is one big free block
self.memory_blocks = [MemoryBlock(total_memory)]
self.total_memory = total_memory
self.allocated_memory = 0
def best_fit_allocate(self, department_name, requested_memory):
# Find the best-fit block (smallest block that fits)
best_fit_index = None
best_fit_size = float('inf')
for i, block in enumerate(self.memory_blocks):
if block.is_free and block.size >= requested_memory and block.size < best_fit_size:
best_fit_index = i
best_fit_size = block.size
if best_fit_index is not None:
block = self.memory_blocks[best_fit_index]
block.is_free = False
self.allocated_memory += requested_memory
print(f"{department_name} allocated {requested_memory} units in block of {block.size}
units.")
# Split the block if it's larger than the requested memory
if block.size > requested_memory:
remaining_size = block.size - requested_memory
block.size = requested_memory
self.memory_blocks.insert(best_fit_index + 1, MemoryBlock(remaining_size))
else:
print(f"Not enough memory to allocate {requested_memory} units to
{department_name}.")
def memory_utilization(self):
return self.allocated_memory
def fragmentation(self):
free_blocks = [block.size for block in self.memory_blocks if block.is_free]
total_free_memory = sum(free_blocks)
return free_blocks, total_free_memory
# Initialize MedTech Hospital with 8000 units of memory
hospital = MedTechHospital(8000)
# Department memory requests
departments = {
"Emergency Department": 2000,
"Cardiology Department": 1500,
"Laboratory Information System": 1200,
"Radiology Department": 1800,
"Patient Management System": 1000,
"Pharmacy System": 600,
"Surgical Services": 2200,
}
# Allocate memory using the best-fit technique for each department/system
for department, memory in departments.items():
hospital.best_fit_allocate(department, memory)
# Report total memory utilization
print("\nMemory Utilization:")
print(f"Total memory used: {hospital.memory_utilization()} units")
# Fragmentation analysis
free_blocks, total_free_memory = hospital.fragmentation()
print("\nFragmentation Analysis:")
print(f"Free memory blocks: {free_blocks}")
print(f"Total free memory: {total_free_memory} units")
OUTPUT :
Emergency Department allocated 2000 units in block of 8000 units.
Cardiology Department allocated 1500 units in block of 6000 units.
Laboratory Information System allocated 1200 units in block of 4500 units.
Radiology Department allocated 1800 units in block of 3300 units.
Patient Management System allocated 1000 units in block of 1500 units.
Memory Utilization:
Total memory used: 7500 units
Fragmentation Analysis:
Free memory blocks: [500]
Total free memory: 500 units