Here's a simplified version with fixed 4 jobs and 4 partitions:
def first_fit(partitions, job_size):
for i in range(4):
if partitions[i]['status'] == 'free' and partitions[i]['size'] >= job_size:
return i
return -1
def best_fit(partitions, job_size):
best_index = -1
best_size = 99999
for i in range(4):
if partitions[i]['status'] == 'free' and partitions[i]['size'] >= job_size:
waste = partitions[i]['size'] - job_size
if waste < best_size:
best_size = waste
best_index = i
return best_index
def worst_fit(partitions, job_size):
worst_index = -1
worst_size = -1
for i in range(4):
if partitions[i]['status'] == 'free' and partitions[i]['size'] >= job_size:
waste = partitions[i]['size'] - job_size
if waste > worst_size:
worst_size = waste
worst_index = i
return worst_index
# Main program
print("Choose an allocation algorithm (Enter 1 for First Fit, 2 for Best Fit, 3 for Worst Fit): ")
choice = int(input())
# Get partition sizes
partitions = []
print("Enter the sizes of 4 memory partitions:")
for i in range(4):
size = int(input(f"Enter size of partition {i+1}: "))
partitions.append({'size': size, 'status': 'free', 'job_num': 0, 'job_size': 0})
# Get job sizes
jobs = []
print("\nEnter the sizes of 4 jobs:")
for i in range(4):
size = int(input(f"Enter size of job {i+1}: "))
jobs.append({'num': i+1, 'size': size, 'partition': -1, 'status': 'waiting'})
# Find largest partition
largest_partition = max(partitions[i]['size'] for i in range(4))
# Process each job
for job in jobs:
# Check if job is too big
if job['size'] > largest_partition:
print(f"\nJob {job['num']} rejected - too large for any partition")
job['status'] = 'rejected'
continue
# Find partition based on algorithm
if choice == 1:
partition_index = first_fit(partitions, job['size'])
elif choice == 2:
partition_index = best_fit(partitions, job['size'])
elif choice == 3:
partition_index = worst_fit(partitions, job['size'])
# Allocate if partition found
if partition_index != -1:
partitions[partition_index]['status'] = 'busy'
partitions[partition_index]['job_num'] = job['num']
partitions[partition_index]['job_size'] = job['size']
job['partition'] = partition_index + 1
job['status'] = 'allocated'
else:
job['status'] = 'waiting'
# Display results
print("\n" + "="*50)
print("MEMORY PARTITIONS SUMMARY:")
print("="*50)
for i in range(4):
if partitions[i]['status'] == 'busy':
print(f"JobNo. {partitions[i]['job_num']}: Job_size {partitions[i]['job_size']}: Allocated, Partition {i+1}")
print("\n" + "="*70)
print("TABLE FOR THE FIXED ALLOCATION RESULTS")
print("="*70)
print("Job\tJob size\tPartition\tPartition size\tStatus\t\tInternal Fragmentation")
print("-"*70)
for job in jobs:
if job['status'] == 'allocated':
partition_num = job['partition']
partition_size = partitions[partition_num-1]['size']
fragmentation = partition_size - job['size']
print(f"{job['num']}\t{job['size']}\t\t{partition_num}\t\t{partition_size}\t\tAllocated\t{fragmentation}")
elif job['status'] == 'waiting':
print(f"{job['num']}\t{job['size']}\t\t-\t\t-\t\tWaiting\t\t-")
elif job['status'] == 'rejected':
print(f"{job['num']}\t{job['size']}\t\t-\t\t-\t\tRejected\t-")
This simplified program:
- Works with exactly 4 memory partitions and 4 jobs
- Follows the algorithm you provided step by step
- Checks if a job is larger than the largest partition and rejects it
- Uses the selected allocation algorithm (First-Fit, Best-Fit, or Worst-Fit)
- Displays the results in the exact format you specified
- Shows which jobs are allocated, waiting, or rejected
- Calculates internal fragmentation for allocated jobs