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 eg 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
CPU R F
CPU IO CPU R F
import heapq from collections import deque, defaultdict class Process: def initself pid, arrivaltime, priority, bursts: self.pid pid self.arrivaltime arrivaltime self.priority priority self.bursts dequebursts # Queue of CPU and IO bursts self.waitingtime self.turnaroundtime self.currentbursttime self.resourcerequests self.iswaiting False class Scheduler: def initself: self.time self.readyqueue # Priority queue for scheduling self.ioqueue deque self.resourceallocation defaultdictlambda: None # Resource allocation self.waitingprocesses deque self.completedprocesses self.deadlockstates def addprocessself process: heapq.heappushselfreadyqueue, processpriority, process.arrivaltime, process def simulateself: while self.readyqueue or self.ioqueue or self.waitingprocesses: self.executereadyqueue self.executeioqueue self.checkdeadlock def executereadyqueueself: if self.readyqueue: process heapq.heappopselfreadyqueue currentburst process.bursts.popleft if isinstancecurrentburst, tuple: # CPU with resource requestsreleases self.handleresourcerequestsprocess currentburst else: self.time currentburst # Simulate CPU burst process.turnaroundtime self.time process.arrivaltime if not process.bursts: self.completedprocesses.appendprocess else: self.ioqueue.appendprocess def executeioqueueself: # Simulate IO bursts and move back to ready queue if self.ioqueue: ioprocess self.ioqueue.popleft ioburst ioprocess.bursts.popleft self.time ioburst self.addprocessioprocess def handleresourcerequestsself process, resourceburst: requesttype, resource, bursttime resourceburst if requesttype R: if self.resourceallocationresource is None: self.resourceallocationresource process.pid process.currentbursttime bursttime else: process.iswaiting True self.waitingprocesses.appendprocess elif requesttype F: if self.resourceallocationresource process.pid: self.resourceallocationresource None process.currentbursttime bursttime else: raise ExceptionInvalid resource release" def checkdeadlockself: # Simple deadlock detection: cyclic dependency check if self.detectdeadlock: self.deadlockstates.appendselftime, listselfwaitingprocesses self.recoverfromdeadlock def detectdeadlockself: # Use resource allocation graph or similar technique return False # Placeholder for actual deadlock detection logic def recoverfromdeadlockself: # Recover by terminating or preempting processes if self.waitingprocesses: victim self.waitingprocesses.popleft printfRecovering from deadlock by terminating process victimpid def generatestatisticsself: totalwaitingtime sumpwaitingtime for p in self.completedprocesses totalturnaroundtime sumpturnaroundtime for p in self.completedprocesses avgwaitingtime totalwaitingtime lenselfcompletedprocesses avgturnaroundtime totalturnaroundtime lenselfcompletedprocesses printfAverage Waiting Time: avgwaitingtime printfAverage Turnaround Time: avgturnaroundtime # Generate Gantt Chart printGantt Chart: Time Process # Example usage processes ProcessRF ProcessIO None, RF scheduler Scheduler for p in processes: scheduler.addprocessp scheduler.simulate scheduler.generatestatistics
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
