Below is the modified program where you only need to input the job sizes (job numbers are internally assigned). The program still generates 4 random partitions and lets you choose the allocation algorithm.
import random
# Fixed Memory Allocation Program
print("Select a memory allocation algorithm:")
print("1. First Fit (.❛ ᴗ ❛.)")
print("2. Best Fit⸜( ´ ꒳ ` )⸝")
print("3. Worst Fit(¬‿¬ )")
algo_choice = int(input("Enter 1, 2, or 3: "))
# Function to generate random partitions
def generate_random_partitions(total_size=200, num_partitions=4):
partitions = []
remaining_size = total_size
for i in range(num_partitions - 1):
# Ensure minimum size for remaining partitions is 1 each.
size = random.randint(1, remaining_size - (num_partitions - i - 1))
partitions.append(size)
remaining_size -= size
partitions.append(remaining_size)
return partitions
# Generate random partitions
random_partitions = generate_random_partitions()
print(f"\nGenerated Partitions: {random_partitions}")
# Initialize partition list with partition properties
partitions = [{"size": size, "status": "free", "job": None} for size in random_partitions]
# --- Input Job Sizes Manually ---
jobs = []
print("\nEnter sizes for 4 jobs:")
for i in range(4):
job_size = int(input(f"Enter size for Job {i+1}: "))
jobs.append({"job_no": i+1, "size": job_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_size = max(partitions, key=lambda x: x["size"])["size"]
if job_size > max_partition_size:
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(len(partitions)):
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(len(partitions)):
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(len(partitions)):
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
# Save partition number (1-indexed) for reference
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"Job {job['job_no']} (Size {job['size']}): Allocated to Partition {job['partition']}")
else:
print(f"Job {job['job_no']} (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:
if job["allocated"]:
partition_index = job["partition"] - 1
partition_size = partitions[partition_index]["size"]
status = "Allocated"
fragmentation = partition_size - job["size"]
partition_no = job["partition"]
else:
partition_no = "-"
partition_size = "-"
status = "Waiting"
fragmentation = "-"
print(f"{job['job_no']}\t{job['size']}\t\t{partition_no}\t\t{partition_size}\t\t{status}\t{fragmentation}")
How the Program Works:
-
Algorithm Selection:
You choose one of the three allocation algorithms (First Fit, Best Fit, or Worst Fit).
-
Random Partitions Generation:
The program automatically generates 4 random partitions whose total sum is 200.
-
Job Input:
You only need to enter the sizes for 4 jobs (job numbers are automatically assigned from 1 to 4).
-
Job Allocation:
Jobs are allocated to the partitions based on the chosen algorithm.
(Each partition can accept only one job.)
-
Results Display:
The program prints a summary of memory partition allocations and a detailed results table including internal fragmentation.
Feel free to ask for further modifications or clarifications!