0% found this document useful (0 votes)
19 views

OS WEEK5 (1)

Uploaded by

tsahithi726
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

OS WEEK5 (1)

Uploaded by

tsahithi726
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

OPERATING SYSTEMS

WEEK 5 CODES

5.1 . Futuristic Space Station


PROGRAM :
memory_blocks = [
{"block": 1, "size": 400},
{"block": 2, "size": 250},
{"block": 3, "size": 350},
{"block": 4, "size": 200},
{"block": 5, "size": 150}
]
# Program memory requirements
programs = {
"Program A": 150,
"Program B": 300,
"Program C": 200
}
# Function to simulate worst-fit memory allocation
def worst_fit(memory_blocks, programs):
allocations = {} # Stores program and the block it gets allocated to
for program, mem_needed in programs.items():
# Find the largest block that can fit the memory required by the program
largest_block = None
for block in memory_blocks:
if block["size"] >= mem_needed:
if largest_block is None or block["size"] > largest_block["size"]:
largest_block = block
# Allocate memory to the program if a suitable block is found
if largest_block:
print(f"{program} is allocated to Block {largest_block['block']} (Size:{largest_block['size']}
units)")
allocations[program] = largest_block["block"]
# Reduce the size of the allocated block
largest_block["size"] -= mem_needed
print(f"After allocation, Block {largest_block['block']} has {largest_block['size']} units
remaining.")
else:
print(f"{program} cannot be allocated to any block.")
return allocations
# Function to calculate remaining memory after allocation
def remaining_memory(memory_blocks):
total_remaining = sum(block["size"] for block in memory_blocks)
return total_remaining
# Run the worst-fit memory allocation
print("Starting worst-fit memory allocation...\n")
allocations = worst_fit(memory_blocks, programs)
# Print remaining memory in all blocks
print("\nRemaining memory in each block:")
for block in memory_blocks:
print(f"Block {block['block']}: {block['size']} units")
# Calculate total remaining memory
total_remaining_memory = remaining_memory(memory_blocks)
print(f"\nTotal remaining memory: {total_remaining_memory} units")

OUTPUT :
Starting worst-fit memory allocation...

Program A is allocated to Block 1 (Size:400 units)


After allocation, Block 1 has 250 units remaining.
Program B is allocated to Block 3 (Size:350 units)
After allocation, Block 3 has 50 units remaining.
Program C is allocated to Block 1 (Size:250 units)
After allocation, Block 1 has 50 units remaining.

Remaining memory in each block:


Block 1: 50 units
Block 2: 250 units
Block 3: 50 units
Block 4: 200 units
Block 5: 150 units

Total remaining memory: 700 units

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.")

# 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
# Insert a new block with the remaining size
self.memory_blocks.insert(best_fit_index + 1, MemoryBlock(remaining_size))
else:
print(f"Not enough memory to allocate {requested_memory} units to
{subsystem_name}.")
def memory_utilization(self):
return self.allocated_memory
def fragmentation(self):
# Count free blocks and total free memory
free_blocks = [block.size for block in self.memory_blocks if block.is_free]
return free_blocks, sum(free_blocks)
# Initialize CentralAI with 8000 units of memory
central_ai = CentralAI(8000)
# Subsystem memory requests
subsystems = {
"SubsysA": 1500,
"SubsysB": 1000,
"SubsysC": 700,
"SubsysD": 2200,
"SubsysE": 500,
"SubsysF": 1200,
}
# Allocate memory using the best-fit technique for each subsystem
for subsystem, memory in subsystems.items():
central_ai.best_fit_allocate(subsystem, memory)
# Report total memory utilization
print("\nMemory Utilization:")
print(f"Total memory used: {central_ai.memory_utilization()} units")
# Fragmentation analysis
free_blocks, total_free_memory = central_ai.fragmentation()
print("\nFragmentation Analysis:")
print(f"Free memory blocks: {free_blocks}")
print(f"Total free memory: {total_free_memory} 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 :

Project A allocated 1500 units in block of 6000 units.


Project B allocated 1000 units in block of 4500 units.
Project C allocated 700 units in block of 3500 units.
Project D allocated 2200 units in block of 2800 units.
Project E allocated 500 units in block of 600 units.
Not enough memory to allocate 1200 units to Project F.

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

You might also like