processes = [
{"pid": "P0", "burst_time": 5, "arrival_time": 0, "service_time": None, "waiting_time": None},
{"pid": "P1", "burst_time": 3, "arrival_time": 1, "service_time": None, "waiting_time": None},
{"pid": "P2", "burst_time": 8, "arrival_time": 2, "service_time": None, "waiting_time": None},
{"pid": "P3", "burst_time": 6, "arrival_time": 3, "service_time": None, "waiting_time": None}
]
# No waiting time for first process
processes[0]["waiting_time"] = 0
# Service_time of the first process is it's arrival time
processes[0]["service_time"] = processes[0]["arrival_time"]
for i in range(1, len(processes)):
# Case where process comes when processor is in idle state
if processes[i]["arrival_time"] > (processes[i - 1]["service_time"] + processes[i - 1]["burst_time"]):
processes[i]["service_time"] = processes[i]["arrival_time"]
else:
processes[i]["service_time"] = processes[i - 1]["service_time"] + processes[i - 1]["burst_time"]
processes[i]["waiting_time"] = processes[i]["service_time"] - processes[i]["arrival_time"]
total_waiting_time = 0
for process in processes:
total_waiting_time += process["waiting_time"]
avg_waiting_time = total_waiting_time / len(processes)
print("Average Waiting Time:", avg_waiting_time)