i want to input jobs
- User
i want to input jobs
- Assistant
Great! If you'd prefer to manually input the jobs instead of using fixed ones, we can simply remove the hardcoded job list and prompt the user to enter each job's number and size.
Here’s the updated version of the program with:
✅ Randomly generated partitions (sum ≤ 200)
✅ Manual input for jobs
✅ Selection of memory allocation algorithm (First-Fit, Best-Fit, Worst-Fit)
✅ Full allocation logic and results table
import random # Fixed Memory Allocation Program 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: ")) # 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): 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 partitions = generate_random_partitions() print(f"\nGenerated partitions: {partitions}") # Initialize partition list partitions = [{"size": size, "status": "free", "job": None} for size in partitions] # --- Input Jobs Manually --- jobs = [] print("\nEnter details for 4 jobs (Job No. and Size):") for i in range(4): job_no = int(input(f"Job {i+1} Number: ")) job_size = int(input(f"Job {i+1} Size: ")) jobs.append({"job_no": job_no, "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 = 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(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 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}")
✅ Example Input/Output Flow:
Step 1: Algorithm selection
Select a memory allocation algorithm: 1. First Fit (.❛ ᴗ ❛.) 2. Best Fit⸜( ´ ꒳ ` )⸝ 3. Worst Fit(¬‿¬ ) Enter 1, 2, or 3: 2Step 2: Generated partitions (e.g.)
Generated partitions: [70, 60, 50, 20]Step 3: Manual job input
Enter details for 4 jobs (Job No. and Size): Job 1 Number: 1 Job 1 Size: 30 Job 2 Number: 2 Job 2 Size: 50 Job 3 Number: 3 Job 3 Size: 30 Job 4 Number: 4 Job 4 Size: 25Step 4: Output
Memory Partitions Summary: JobNo.1: Job_size 30: Allocated, Partition 1 JobNo.2: Job_size 50: Allocated, Partition 2 JobNo.3: Job_size 30: Allocated, Partition 3 JobNo.4: Job_size 25: Allocated, Partition 3(Note: If multiple jobs try to allocate to same partition, logic may need adjustment — current code doesn't prevent overlapping. Let me know if you want to fix that.)
🔧 Want to improve this?
If you want to:- Prevent more than one job per partition
- Sort jobs by size before allocation
- Show remaining free memory
Just let me know!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.