Question: description of code - Simulate CPU Scheduling: Implement priority scheduling algorithm with round robin. The simulation should continue until all the processes in the input

description of code
- Simulate CPU Scheduling: Implement priority scheduling algorithm with round robin. The simulation should continue until all the processes in the input file terminate.
- Deadlock Detection and Recovery: Implement an appropriate deadlock detection algorithm to monitor the system status and identify deadlock situations. If a deadlock is detected, implement a deadlock recovery strategy of your choice (e.g., process termination, resource preemption).
At the end of simulation, your program must show the Gantt chart representing the timeline of process execution, average waiting time, and average turnaround time for all processes. Also report the detected deadlock states and how recovery was handled
edit this code to take the data from file input and to be work correctly and test it
input format
001 CPU {R[1],50, F[1]}
151 CPU {20} IO{30} CPU {20, R[2],30, F[2],10}
import heapq from collections import deque, defaultdict class Process: def __init__(self, pid, arrival_time, priority, bursts): self.pid = pid self.arrival_time = arrival_time self.priority = priority self.bursts = deque(bursts) # Queue of CPU and IO bursts self.waiting_time =0 self.turnaround_time =0 self.current_burst_time =0 self.resource_requests =[] self.is_waiting = False class Scheduler: def __init__(self): self.time =0 self.ready_queue =[] # Priority queue for scheduling self.io_queue = deque() self.resource_allocation = defaultdict(lambda: None) # Resource allocation self.waiting_processes = deque() self.completed_processes =[] self.deadlock_states =[] def add_process(self, process): heapq.heappush(self.ready_queue, (process.priority, process.arrival_time, process)) def simulate(self): while self.ready_queue or self.io_queue or self.waiting_processes: self.execute_ready_queue() self.execute_io_queue() self.check_deadlock() def execute_ready_queue(self): if self.ready_queue: _,_, process = heapq.heappop(self.ready_queue) current_burst = process.bursts.popleft() if isinstance(current_burst, tuple): # CPU with resource requests/releases self.handle_resource_requests(process, current_burst) else: self.time += current_burst # Simulate CPU burst process.turnaround_time += self.time - process.arrival_time if not process.bursts: self.completed_processes.append(process) else: self.io_queue.append(process) def execute_io_queue(self): # Simulate IO bursts and move back to ready queue if self.io_queue: io_process = self.io_queue.popleft() io_burst = io_process.bursts.popleft() self.time += io_burst self.add_process(io_process) def handle_resource_requests(self, process, resource_burst): request_type, resource, burst_time = resource_burst if request_type =="R": if self.resource_allocation[resource] is None: self.resource_allocation[resource]= process.pid process.current_burst_time += burst_time else: process.is_waiting = True self.waiting_processes.append(process) elif request_type =="F": if self.resource_allocation[resource]== process.pid: self.resource_allocation[resource]= None process.current_burst_time += burst_time else: raise Exception("Invalid resource release") def check_deadlock(self): # Simple deadlock detection: cyclic dependency check if self.detect_deadlock(): self.deadlock_states.append((self.time, list(self.waiting_processes))) self.recover_from_deadlock() def detect_deadlock(self): # Use resource allocation graph or similar technique return False # Placeholder for actual deadlock detection logic def recover_from_deadlock(self): # Recover by terminating or preempting processes if self.waiting_processes: victim = self.waiting_processes.popleft() print(f"Recovering from deadlock by terminating process {victim.pid}") def generate_statistics(self): total_waiting_time = sum(p.waiting_time for p in self.completed_processes) total_turnaround_time = sum(p.turnaround_time for p in self.completed_processes) avg_waiting_time = total_waiting_time / len(self.completed_processes) avg_turnaround_time = total_turnaround_time / len(self.completed_processes) print(f"Average Waiting Time: {avg_waiting_time}") print(f"Average Turnaround Time: {avg_turnaround_time}") # Generate Gantt Chart print("Gantt Chart: (Time -> Process)") # Example usage processes =[ Process(0,0,1,[("R",1,50),("F",1,0)]), Process(1,5,1,[20,("IO", None, 30),20,("R",2,30),("F",2,10)]),] scheduler = Scheduler() for p in processes: scheduler.add_process(p) scheduler.simulate() scheduler.generate_statistics()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!