akbarbasya26

Shortest remaining time.py

Jan 6th, 2021
977
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def findWaitingTime(processes, n, wt):
  2.     rt = [0] * n
  3.  
  4.     # Copy the burst time into rt[]
  5.     for i in range(n):
  6.         rt[i] = processes[i][1]
  7.     complete = 0
  8.     t = 0
  9.     minm = 999999999
  10.     short = 0
  11.     check = False
  12.  
  13.     # Process until all processes gets
  14.     # completed
  15.  
  16.     while (complete != n):
  17.  
  18.         # Find process with minimum remaining
  19.         # time among the processes that
  20.         # arrives till the current time`
  21.  
  22.         for j in range(n):
  23.             if ((processes[j][2] <= t) and
  24.                 (rt[j] < minm) and rt[j] > 0):
  25.                 minm = rt[j]
  26.                 short = j
  27.                 check = True
  28.  
  29.         if (check == False):
  30.             t += 1
  31.             continue
  32.  
  33.         # Reduce remaining time by one
  34.  
  35.         rt[short] -= 1
  36.  
  37.         # Update minimum
  38.         minm = rt[short]
  39.         if (minm == 0):
  40.             minm = 999999999
  41.  
  42.  
  43.  
  44.         # If a process gets completely
  45.         # executed
  46.  
  47.         if (rt[short] == 0):
  48.  
  49.             # Increment complete
  50.  
  51.             complete += 1
  52.             check = False
  53.  
  54.             # Find finish time of current
  55.             # process
  56.  
  57.             fint = t + 1
  58.  
  59.             # Calculate waiting time
  60.  
  61.             wt[short] = (fint - proc[short][1] -   
  62.                                 proc[short][2])
  63.  
  64.             if (wt[short] < 0):
  65.                 wt[short] = 0
  66.  
  67.         # Increment time
  68.         t += 1
  69.  
  70. # Function to calculate turn around time
  71.  
  72. def findTurnAroundTime(processes, n, wt, tat):
  73.  
  74.     # Calculating turnaround time
  75.     for i in range(n):
  76.         tat[i] = processes[i][1] + wt[i]
  77.  
  78.  
  79.  
  80. # Function to calculate average waiting
  81. # and turn-around times.
  82. def findavgTime(processes, n):
  83.     wt = [0] * n
  84.     tat = [0] * n
  85.  
  86.     # Function to find waiting time
  87.     # of all processes
  88.     findWaitingTime(processes, n, wt)
  89.  
  90.     # Function to find turn around time
  91.     # for all processes
  92.     findTurnAroundTime(processes, n, wt, tat)
  93.  
  94.     # Display processes along with all details
  95.     print("Processes Burst Time  Waiting", "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(" ", processes[i][0], "\t\t",
  103.                 processes[i][1], "\t\t",
  104.                 wt[i], "\t\t", tat[i])
  105.  
  106.     print("\nAverage waiting time = %.5f "%(total_wt /n) )
  107.     print("Average turn around time = ", total_tat / n)
  108.  
  109. # Driver code
  110. if __name__ =="__main__":
  111.     # Process id's
  112.     print ("Algoritma Shortest Remaining Time First ")
  113.     print("             ")
  114.     proc = [[1, 46, 3], [2, 35, 3],
  115.             [3, 46*46, 3], [4, 15, 3],
  116.             [5, 20, 3], [6, 46+46, 3],
  117.             [7, 5, 3],[8, 46+14, 3],
  118.             [9, (46+(2*46))-3, 3],[10, 12, 3]]
  119.     n = 10
  120.     findavgTime(proc, n)
  121.     print("Interval = 3")
RAW Paste Data