Facebook
From MAISHA FAHMIDA, 2 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 96
  1. def findWaitingTime(processes, n, bt,  wt, quantum):  
  2.  
  3.     rem_bt = [0] * n
  4.  
  5.   # Copy the burst time into rt[]  
  6.  
  7.     for i in range(n):  
  8.  
  9.         rem_bt[i] = bt[i]
  10.  
  11.     t = 0 # Current time  
  12.  
  13.  
  14.  
  15.     # Keep traversing processes in round  
  16.  
  17.     # robin manner until all of them are
  18.  
  19.     # not done.  
  20.  
  21.     while(1):
  22.  
  23.         done = True
  24.  
  25.  
  26.  
  27.         # Traverse all processes one by
  28.  
  29.         # one repeatedly  
  30.  
  31.         for i in range(n):
  32.  
  33.              
  34.  
  35.             # If burst time of a process is greater  
  36.  
  37.             # than 0 then only need to process further  
  38.  
  39.             if (rem_bt[i] > 0) :
  40.  
  41.                 done = False # There is a pending process
  42.  
  43.                  
  44.  
  45.                 if (rem_bt[i] > quantum) :
  46.  
  47.                  
  48.  
  49.                     # Increase the value of t i.e. shows  
  50.  
  51.                     # how much time a process has been processed  
  52.  
  53.                     t += quantum  
  54.  
  55.  
  56.  
  57.                     # Decrease the burst_time of current  
  58.  
  59.                     # process by quantum  
  60.  
  61.                     rem_bt[i] -= quantum  
  62.  
  63.                  
  64.  
  65.                 # If burst time is smaller than or equal  
  66.  
  67.                 # to quantum. Last cycle for this process  
  68.  
  69.                 else:
  70.  
  71.                  
  72.  
  73.                     # Increase the value of t i.e. shows  
  74.  
  75.                     # how much time a process has been processed  
  76.  
  77.                     t = t + rem_bt[i]  
  78.  
  79.  
  80.  
  81.                     # Waiting time is current time minus  
  82.  
  83.                     # time used by this process  
  84.  
  85.                     wt[i] = t - bt[i]  
  86.  
  87.  
  88.  
  89.                     # As the process gets fully executed  
  90.  
  91.                     # make its remaining burst time = 0  
  92.  
  93.                     rem_bt[i] = 0
  94.  
  95.                  
  96.  
  97.         # If all processes are done  
  98.  
  99.         if (done == True):
  100.  
  101.             break
  102.  
  103.              
  104. # Function to calculate turn around time  
  105.  
  106. def findTurnAroundTime(processes, n, bt, wt, tat):
  107.  
  108.      
  109.  
  110.     # Calculating turnaround time  
  111.  
  112.     for i in range(n):
  113.  
  114.         tat[i] = bt[i] + wt[i]  
  115.  
  116.  
  117.  
  118.  
  119. # Function to calculate average waiting  
  120. # and turn-around times.  
  121.  
  122. def findavgTime(processes, n, bt, quantum):  
  123.  
  124.     wt = [0] * n
  125.  
  126.     tat = [0] * n  
  127.  
  128.  
  129.  
  130.     # Function to find waiting time
  131.  
  132.     # of all processes  
  133.  
  134.     findWaitingTime(processes, n, bt,  
  135.  
  136.                          wt, quantum)  
  137.  
  138.  
  139.  
  140.     # Function to find turn around time
  141.  
  142.     # for all processes  
  143.  
  144.     findTurnAroundTime(processes, n, bt,
  145.  
  146.                                 wt, tat)  
  147.  
  148.  
  149.  
  150.     # Display processes along with all details  
  151.  
  152.     print("Processes    Burst Time     Waiting",  
  153.  
  154.                      "Time    Turn-Around Time")
  155.  
  156.     total_wt = 0
  157.  
  158.     total_tat = 0
  159.  
  160.     for i in range(n):
  161.  
  162.  
  163.  
  164.         total_wt = total_wt + wt[i]  
  165.  
  166.         total_tat = total_tat + tat[i]  
  167.  
  168.         print(" ", proc[i], "\t\t", bt[i],  
  169.  
  170.               "\t\t", wt[i], "\t\t", tat[i])
  171.  
  172.  
  173.  
  174.     print("\nAverage waiting time = %.5f "%(total_wt /n) )
  175.  
  176.     print("Average turn around time = %.5f "% (total_tat / n))  
  177.  
  178.      
  179. # Driver code  
  180.  
  181. if __name__ =="__main__":
  182.  
  183.      
  184.  
  185.     # Process id's  
  186.  
  187.     proc = ["p1","p2", "p3"]
  188.  
  189.     n = 3
  190.  
  191.  
  192.  
  193.     # Burst time of all processes  
  194.  
  195.     burst_time = [10, 5, 8]  
  196.  
  197.  
  198.  
  199.     # Time quantum  
  200.  
  201.     quantum = 2;  
  202.  
  203.     findavgTime(proc, n, burst_time, quantum)
  204.  
  205.  
  206. # This code is contributed by
  207. # Shubham Singh(SHUBHAMSINGH10