fork download
  1. def findWaitingTime(processes, n, bt,
  2. wt, quantum):
  3. rem_bt = [0] * n
  4.  
  5. # Copy the burst time into rt[]
  6. for i in range(n):
  7. rem_bt[i] = bt[i]
  8. t = 0 # Current time
  9.  
  10. # Keep traversing processes in round
  11. # robin manner until all of them are
  12. # not done.
  13. while(1):
  14. done = True
  15.  
  16. # Traverse all processes one by
  17. # one repeatedly
  18. for i in range(n):
  19.  
  20. # If burst time of a process is greater
  21. # than 0 then only need to process further
  22. if (rem_bt[i] > 0) :
  23. done = False # There is a pending process
  24.  
  25. if (rem_bt[i] > quantum) :
  26.  
  27. # Increase the value of t i.e. shows
  28. # how much time a process has been processed
  29. t += quantum
  30.  
  31. # Decrease the burst_time of current
  32. # process by quantum
  33. rem_bt[i] -= quantum
  34.  
  35. # If burst time is smaller than or equal
  36. # to quantum. Last cycle for this process
  37. else:
  38.  
  39. # Increase the value of t i.e. shows
  40. # how much time a process has been processed
  41. t = t + rem_bt[i]
  42.  
  43. # Waiting time is current time minus
  44. # time used by this process
  45. wt[i] = t - bt[i]
  46.  
  47. # As the process gets fully executed
  48. # make its remaining burst time = 0
  49. rem_bt[i] = 0
  50.  
  51. # If all processes are done
  52. if (done == True):
  53. break
  54.  
  55. # Function to calculate turn around time
  56. def findTurnAroundTime(processes, n, bt, wt, tat):
  57.  
  58. # Calculating turnaround time
  59. for i in range(n):
  60. tat[i] = bt[i] + wt[i]
  61.  
  62.  
  63. # Function to calculate average waiting
  64. # and turn-around times.
  65. def findavgTime(processes, n, bt, quantum):
  66. wt = [0] * n
  67. tat = [0] * n
  68.  
  69. # Function to find waiting time
  70. # of all processes
  71. findWaitingTime(processes, n, bt,
  72. wt, quantum)
  73.  
  74. # Function to find turn around time
  75. # for all processes
  76. findTurnAroundTime(processes, n, bt,
  77. wt, tat)
  78.  
  79. # Display processes along with all details
  80. print("Processes Burst Time Waiting",
  81. "Time Turn-Around Time")
  82. total_wt = 0
  83. total_tat = 0
  84. for i in range(n):
  85.  
  86. total_wt = total_wt + wt[i]
  87. total_tat = total_tat + tat[i]
  88. print(" ", i + 1, "\t\t", bt[i],
  89. "\t\t", wt[i], "\t\t", tat[i])
  90.  
  91. print("\nAverage waiting time = %.5f "%(total_wt /n) )
  92. print("Average turn around time = %.5f "% (total_tat / n))
  93.  
  94. # Driver code
  95. if __name__ =="__main__":
  96.  
  97. # Process id's
  98. proc = [1, 2, 3]
  99. n = 3
  100.  
  101. # Burst time of all processes
  102. burst_time = [2, 7, 4]
  103.  
  104. # Time quantum
  105. quantum = 3;
  106. findavgTime(proc, n, burst_time, quantum)
Success #stdin #stdout 0.02s 9216KB
stdin
Standard input is empty
stdout
Processes    Burst Time     Waiting Time    Turn-Around Time
  1 		 2 		 0 		 2
  2 		 7 		 6 		 13
  3 		 4 		 8 		 12

Average waiting time = 4.66667 
Average turn around time = 9.00000