I'll simplify the program to have exactly 4 jobs and 4 partitions:
def first_fit(memory_partitions, job_size, job_number):
for i, partition in enumerate(memory_partitions):
if partition['status'] == 'free' and partition['size'] >= job_size:
partition['status'] = 'busy'
partition['job_size'] = job_size
partition['job_number'] = job_number
return i
return -1
def best_fit(memory_partitions, job_size, job_number):
best_fit_index = -1
best_fit_size = float('inf')
for i, partition in enumerate(memory_partitions):
if partition['status'] == 'free' and partition['size'] >= job_size:
if partition['size'] - job_size < best_fit_size:
best_fit_size = partition['size'] - job_size
best_fit_index = i
if best_fit_index != -1:
memory_partitions[best_fit_index]['status'] = 'busy'
memory_partitions[best_fit_index]['job_size'] = job_size
memory_partitions[best_fit_index]['job_number'] = job_number
return best_fit_index
def worst_fit(memory_partitions, job_size, job_number):
worst_fit_index = -1
worst_fit_size = -1
for i, partition in enumerate(memory_partitions):
if partition['status'] == 'free' and partition['size'] >= job_size:
if partition['size'] - job_size > worst_fit_size:
worst_fit_size = partition['size'] - job_size
worst_fit_index = i
if worst_fit_index != -1:
memory_partitions[worst_fit_index]['status'] = 'busy'
memory_partitions[worst_fit_index]['job_size'] = job_size
memory_partitions[worst_fit_index]['job_number'] = job_number
return worst_fit_index
def allocate_memory(algorithm, memory_partitions, job_size, job_number):
if algorithm == 1:
return first_fit(memory_partitions, job_size, job_number)
elif algorithm == 2:
return best_fit(memory_partitions, job_size, job_number)
elif algorithm == 3:
return worst_fit(memory_partitions, job_size, job_number)
else:
print("Invalid algorithm choice.")
return -1
def main():
algorithm = int(input("Choose an allocation algorithm (Enter 1 for First Fit, 2 for Best Fit, 3 for Worst Fit): "))
# Fixed 4 partitions
memory_partitions = []
print("Enter the sizes of 4 memory partitions:")
for i in range(4):
size = int(input(f"Enter the size of memory partition {i+1}: "))
memory_partitions.append({'size': size, 'status': 'free', 'job_size': 0, 'job_number': 0})
# Fixed 4 jobs
jobs = []
print("Enter the sizes of 4 jobs:")
for i in range(4):
size = int(input(f"Enter the size of job {i+1}: "))
jobs.append(size)
# Process allocation
job_allocation = [None] * 4 # To track which partition each job is allocated to
for i, job_size in enumerate(jobs):
job_number = i + 1
partition_index = allocate_memory(algorithm, memory_partitions, job_size, job_number)
if partition_index != -1:
job_allocation[i] = partition_index
# Display results
print("\nMemory Partitions Summary:")
for i, partition in enumerate(memory_partitions):
if partition['status'] == 'busy':
print(f"JobNo.{partition['job_number']}: Job_size {partition['job_size']}: Allocated, Partition {i+1}")
print("\nTable for the fixed allocation results")
print("Job\tJob size\tPartition\tPartition size\tStatus\tInternal Fragmentation")
for i, job_size in enumerate(jobs):
job_number = i + 1
if job_allocation[i] is not None:
partition_index = job_allocation[i]
partition = memory_partitions[partition_index]
internal_frag = partition['size'] - job_size
print(f"{job_number}\t{job_size}\t\t{partition_index+1}\t\t{partition['size']}\t\tAllocated\t{internal_frag}")
else:
print(f"{job_number}\t{job_size}\t\tNot Allocated\t-\t\tWaiting\t-")
if __name__ == "__main__":
main()
This simplified version has exactly 4 jobs and 4 partitions. The program prompts the user to enter the sizes for each, then processes the allocation according to the selected algorithm. Finally, it displays the results in the requested format.