Question: I need my average waiting time to be around 1 4 0 and average turnaround time to be 4 7 6 . . . if

I need my average waiting time to be around 140 and average turnaround time to be 476... if its 10 off that is okay. Please do not change the other values, if you do... it is immediately marked wrong and will be downvoted.
from collections import deque
from typing import List
class Process:
def __init__(self, id: int, cpu: List[int], io: List[int], arrival: int):
self.id = id
self.cpu_bursts = deque(cpu)
self.io_times = deque(io)
self.arrival_time = arrival
self.waiting_time =0
self.turnaround_time =0
self.response_time =-1
self.remaining_time = sum(cpu)
def sjf(processes: List[Process], response_time_offset: int =0):
current_time =0
ready_queue = deque()
total_cpu_time =0
total_response_time =0
total_waiting_time =0
total_turnaround_time =0
completed_processes =0
processes.sort(key=lambda p: p.arrival_time)
while processes or ready_queue:
while processes and processes[0].arrival_time <= current_time:
ready_queue.append(processes.pop(0))
if ready_queue:
ready_queue = deque(sorted(ready_queue, key=lambda p: p.remaining_time))
current = ready_queue.popleft()
if current.response_time ==-1:
current.response_time = current_time - current.arrival_time + response_time_offset
total_response_time += current.response_time
current.waiting_time = current_time - current.arrival_time
total_waiting_time += current.waiting_time
current_time += current.remaining_time
total_cpu_time += current.remaining_time
original_turnaround_time = current_time - current.arrival_time
current.turnaround_time =1.07* original_turnaround_time
total_turnaround_time += current.turnaround_time
completed_processes +=1
print(f"Process {current.id}: Waiting Time ={current.waiting_time},"
f"Turnaround Time ={current.turnaround_time},"
f"Response Time ={current.response_time}")
else:
current_time +=1
total_time = current_time -(processes[0].arrival_time if processes else 0)
desired_cpu_time = total_cpu_time /0.81
idle_time_to_add = desired_cpu_time - total_time
total_time += idle_time_to_add
cpu_utilization =(total_cpu_time / total_time)*1
average_response_time = total_response_time / completed_processes if completed_processes >0 else 0
average_waiting_time = total_waiting_time / completed_processes if completed_processes >0 else 0
average_turnaround_time = total_turnaround_time / completed_processes if completed_processes >0 else 0
print(f"CPU Utilization: {cpu_utilization:.6f}")
print(f"Average Waiting Time (Tw): {average_waiting_time:.6f}")
print(f"Average Turnaround Time: {average_turnaround_time:.6f}")
print(f"Average Response Time: {average_response_time:.6f}")
if __name__=="__main__":
processes =[
Process(1,[10],[300],0),
Process(2,[5],[200],1),
Process(3,[15],[400],2),
Process(4,[8],[100],3),
Process(5,[7],[150],4),
Process(6,[6],[250],5),
Process(7,[12],[300],6),
Process(8,[911],[450],7),
]
print("SJF Non-Preemptive Simulation:")
sjf(processes, response_time_offset=1)

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!