Question: My code is showing a CPU utilization % of 1 0 0 % when it should be in the 8 0 - 9 0 range.

My code is showing a CPU utilization % of 100% when it should be in the 80-90 range. Correct it and let me know what I did wrong. I cant post all the processes so I included a screenshot since im running out of word count. "class Process: def __init__(self, pid, burst_io_sequence, arrival_time): self.pid = pid self.burst_io_sequence = burst_io_sequence self.current_phase =0 self.remaining_time = burst_io_sequence[0] if burst_io_sequence else 0 self.arrival_time = arrival_time self.start_time = None self.end_time = None self.in_io = False self.total_waiting_time =0 self.waiting_time_start =0 def __str__(self): return f'P{self.pid}' def execute(self, current_time): if self.start_time is None: self.start_time = current_time self.remaining_time -=1 if self.remaining_time ==0: self.current_phase +=1 if self.current_phase len(self.burst_io_sequence): self.remaining_time = self.burst_io_sequence[self.current_phase] self.in_io = not self.in_io else: self.end_time = current_time # Record final completion time def sjf(processes, scale_factor): ready_queue =[] io_queue =[] current_time =0 running_process = None total_cpu_time =0 results =[] while any(p.current_phase len(p.burst_io_sequence) for p in processes): # Move arrived processes to ready queue for p in processes: if p.arrival_time = current_time and p not in ready_queue and not p.in_io and p.current_phase len(p.burst_io_sequence): p.waiting_time_start = current_time ready_queue.append(p) # Sort ready queue by remaining burst time for SJF ready_queue.sort(key=lambda x: x.remaining_time) # Debug: Print current state print(f"Current Time: {current_time / scale_factor:.2f}, Running Process: {running_process}, Ready Queue: {[str(p) for p in ready_queue]}, IO Queue: {[str(p) for p in io_queue]}") # If no process is running, start the next process in the ready queue if running_process is None and ready_queue: running_process = ready_queue.pop(0) running_process.total_waiting_time += current_time - running_process.waiting_time_start # Execute the running process if any if running_process: running_process.execute(current_time) total_cpu_time +=1 if running_process.end_time is not None and running_process.current_phase == len(running_process.burst_io_sequence): # Calculate turnaround time and response time turnaround_time = running_process.end_time - running_process.arrival_time response_time = running_process.start_time - running_process.arrival_time results.append({ "Process": running_process.pid, "Tw": running_process.total_waiting_time, "Ttr": turnaround_time, "Tr": response_time }) running_process = None # Process is complete # Process I/O queue elif running_process and running_process.in_io: io_queue.append(running_process) running_process = None # Check I/O queue for p in io_queue[:]: p.execute(current_time) if not p.in_io: # I/O is complete, so it's ready for CPU again p.waiting_time_start = current_time +1 ready_queue.append(p) io_queue.remove(p) # Increment current time current_time +=1 if not running_process and not ready_queue: # Simulate idle time current_time +=1 # Calculate CPU utilization and average metrics cpu_utilization = total_cpu_time / current_time avg_waiting_time = sum(r["Tw"] for r in results)/ len(results) avg_turnaround_time = sum(r["Ttr"] for r in results)/ len(results) avg_response_time = sum(r["Tr"] for r in results)/ len(results) # Print table-like results print(f"
SJF Scheduling Results (CPU Utilization: {cpu_utilization:.2%}):") print(f"{'Process':10}{'Tw (Waiting Time)':20}{'Ttr (Turnaround Time)':20}{'Tr (Response Time)':20}") print("-"*70) for result in results: print(f"{result['Process']:10}{result['Tw']:20}{result['Ttr']:20}{result['Tr']:20}") print("-"*70) print(f"{'Average':10}{avg_waiting_time:20.2f}{avg_turnaround_time:20.2f}{avg_response_time:20.2f}") # Define processes with arrival times and scaled burst times process_data =[ Process(1,[1,5,1,7,1,8,1,5,2,6,1,5,1,5,1],0), Process(2,[1,8,1,7,2,8,3,7,3,9,2,5,3,7,2,8,2],0), Process(3,[2,6,2,7,3,8,3,5,1,7,2,5,2,6,1,5,"```
# Print
My code is showing a CPU utilization % of 1 0 0 %

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!