Advertisement
0xCor3

Round Robin

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