Create a fixed memory allocation program where:
Total memory is fixed at 200.
The program should first prompt the user to select a memory allocation algorithm from the following options:
First-Fit
Best-Fit
Worst-Fit
After choosing the algorithm, the user enters 4 jobs(each with a memory size).
J1 - 30k
J2 - 50k
J3 - 30k
J4 - 25k
The system automatically creates memory partitions and attempts to allocate jobs.
After processing the allocation, your program should display the table for fixed allocation results
here is my sample code i like you too imitate
Prompt user for algorithm choice
print("Select a memory allocation algorithm:")
print("1. First Fit (.❛ ᴗ ❛.)\n")
print("2. Best Fit⸜( ´ ꒳ ` )⸝ \n")
print("3. Worst Fit(¬‿¬ ) \n")
algo_choice = int(input("Enter 1, 2, or 3: "))
Initialize partition list and total size variable
partitions = []
total_size = 0
Enter sizes for 4 partitions
print("Enter sizes for 4 partitions (total must not exceed 200):")
for i in range(4):
size = int(input(f"Enter size for Memory {i+1}: "))
total_size += size
if total_size > 200:
print("Error: Total partition sizes exceed 200!")
exit()
partitions.append({"size": size, "status": "free", "job": None})
Initialize jobs list
jobs = []
print("Enter sizes for 4 jobs:")
for i in range(4):
size = int(input(f"Enter size for job {i+1}: "))
jobs.append({"job_no": i+1, "size": size, "allocated": False, "partition": None})
Function to allocate jobs based on chosen algorithm
def allocate_jobs():
for job in jobs:
job_size = job["size"]
allocated = False
selected_partition = -1
# Check if job can fit in any partition
max_partition = max(partitions, key=lambda x: x["size"])["size"]
if job_size > max_partition:
print(f"Job {job['job_no']} (size {job_size}) is too big for any partition")
continue
# Allocation based on First Fit
if algo_choice == 1:
for i in range(4):
if partitions[i]["status"] == "free" and job_size <= partitions[i]["size"]:
selected_partition = i
allocated = True
break
# Allocation based on Best Fit
elif algo_choice == 2:
min_diff = float('inf')
for i in range(4):
if partitions[i]["status"] == "free" and job_size <= partitions[i]["size"]:
diff = partitions[i]["size"] - job_size
if diff < min_diff:
min_diff = diff
selected_partition = i
allocated = True
# Allocation based on Worst Fit
elif algo_choice == 3:
max_size = -1
for i in range(4):
if partitions[i]["status"] == "free" and job_size <= partitions[i]["size"]:
if partitions[i]["size"] > max_size:
max_size = partitions[i]["size"]
selected_partition = i
allocated = True
# Allocate job if possible
if allocated:
partitions[selected_partition]["status"] = "busy"
partitions[selected_partition]["job"] = job["job_no"]
job["allocated"] = True
job["partition"] = selected_partition + 1
Call the allocation function
allocate_jobs()
Display memory partitions summary
print("\nMemory Partitions Summary:")
for job in jobs:
if job["allocated"]:
print(f"JobNo.{job['job_no']}: Job_size {job['size']}: Allocated, Partition {job['partition']}")
else:
print(f"JobNo.{job['job_no']}: Job_size {job['size']}: Not Allocated")
Display allocation results table
print("\nAllocation Results Table:")
print("Job\tJob Size\tPartition\tPartition Size\tStatus\tInternal Fragmentation")
for job in jobs:
job_no = job["job_no"]
job_size = job["size"]
if job["allocated"]:
partition = job["partition"]
partition_size = partitions[partition-1]["size"]
status = "Allocated"
fragmentation = partition_size - job_size
else:
partition = "-"
partition_size = "-"
status = "Waiting"
fragmentation = "-"
print(f"{job_no}\t{job_size}\t\t{partition}\t\t{partition_size}\t\t{status}\t{fragmentation}")