akbarbasya26

ROUND ROBIN.py

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