Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views1 page

CPU Scheduling Average Time Calculator

This Python code calculates waiting times and turnaround times for processes using scheduling algorithms. It defines functions to calculate waiting times from arrival times and service times, turnaround times from waiting times and burst times, and average waiting and turnaround times. The main function gets input for the number of processes, their burst times and arrival times, then calls the other functions to output the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

CPU Scheduling Average Time Calculator

This Python code calculates waiting times and turnaround times for processes using scheduling algorithms. It defines functions to calculate waiting times from arrival times and service times, turnaround times from waiting times and burst times, and average waiting and turnaround times. The main function gets input for the number of processes, their burst times and arrival times, then calls the other functions to output the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

def calculate_waiting_time(processes, n, burst_time, waiting_time, arrival_time):

service_time = [0] * n
service_time[0] = arrival_time[0]
waiting_time[0] = 0
for i in range(1, n):
service_time[i] = (service_time[i - 1] + burst_time[i - 1])
waiting_time[i] = service_time[i] - arrival_time[i]
if (waiting_time[i] < 0):
waiting_time[i] = 0

def calculate_turn_around_time(processes, n, burst_time, waiting_time,


turn_around_time):
for i in range(n):
turn_around_time[i] = burst_time[i] + waiting_time[i]

def calculate_average_times(processes, n, burst_time, arrival_time):


waiting_time = [0] * n
turn_around_time = [0] * n
calculate_waiting_time(processes, n, burst_time, waiting_time, arrival_time)
calculate_turn_around_time(processes, n, burst_time, waiting_time,
turn_around_time)
total_waiting_time = sum(waiting_time)
total_turn_around_time = sum(turn_around_time)

print("Processes".center(15), "Burst Time".center(15), "Arrival


Time".center(15), "Waiting Time".center(15),
"Turn-Around Time".center(15))
for i in range(n):
print(str(processes[i]).center(15), str(burst_time[i]).center(15),
str(arrival_time[i]).center(15),
str(waiting_time[i]).center(15), str(turn_around_time[i]).center(15))

average_waiting_time = total_waiting_time / n
average_turn_around_time = total_turn_around_time / n
print(f"\nAverage Waiting Time = {average_waiting_time:.2f}")
print(f"Average Turn-Around Time = {average_turn_around_time:.2f}")

def main():
n = int(input("Enter the number of processes: "))
processes = [0] * n
burst_time = [0] * n
arrival_time = [0] * n
for i in range(n):
processes[i] = i + 1
burst_time[i] = int(input(f'P{processes[i]}: Enter the burst time: '))
for i in range(n):
arrival_time[i] = int(input(f'P{processes[i]}: Enter the arrival time: '))
calculate_average_times(processes, n, burst_time, arrival_time)

if __name__ == "__main__":
main()

You might also like