fork download
  1. # FCFS(WithAT)
  2. '''
  3. def fcfs(arrival, burst):
  4. n = len(arrival)
  5. completion = [0] * n
  6. waiting = [0] * n
  7. turnaround = [0] * n
  8.  
  9. # Pair process IDs with arrival and burst times, then sort by arrival time
  10. process = list(zip(range(1, n + 1), arrival, burst))
  11. process.sort(key=lambda x: x[1])
  12. process_id, arrival, burst = zip(*process)
  13.  
  14. # First process
  15. completion[0] = arrival[0] + burst[0]
  16.  
  17. # Completion time for the rest
  18. for i in range(1, n):
  19. if arrival[i] > completion[i - 1]:
  20. completion[i] = arrival[i] + burst[i]
  21. else:
  22. completion[i] = completion[i - 1] + burst[i]
  23.  
  24. # Turnaround time and waiting time
  25. for i in range(n):
  26. turnaround[i] = completion[i] - arrival[i]
  27. waiting[i] = turnaround[i] - burst[i]
  28.  
  29. # Output
  30. print("Process\tArrival\tBurst\tCompletion\tTurnaround\tWaiting")
  31. for i in range(n):
  32. print(f"P{process_id[i]}\t{arrival[i]}\t{burst[i]}\t{completion[i]}\t\t{turnaround[i]}\t\t{waiting[i]}")
  33.  
  34. avgwt = sum(waiting) / n
  35. avgtat = sum(turnaround) / n
  36. print(f"\nAverage Turnaround Time = {avgtat:.2f}")
  37. print(f"Average Waiting Time = {avgwt:.2f}")
  38.  
  39.  
  40. # Test data
  41. arrival_time = [3, 4, 0, 3, 5]
  42. burst_time = [1, 5, 2, 7, 5]
  43.  
  44. fcfs(arrival_time, burst_time)
  45.  
  46. '''
  47. #FCFS(WithoutAT)
  48. '''
  49. def fcfs_no_arrival(burst_times):
  50. n = len(burst_times)
  51. process_id = list(range(1, n + 1))
  52.  
  53. waiting = [0] * n
  54. turnaround = [0] * n
  55. completion = [0] * n
  56.  
  57. # First process starts at time 0
  58. completion[0] = burst_times[0]
  59. turnaround[0] = burst_times[0]
  60. waiting[0] = 0
  61.  
  62. for i in range(1, n):
  63. completion[i] = completion[i - 1] + burst_times[i]
  64. turnaround[i] = completion[i]
  65. waiting[i] = turnaround[i] - burst_times[i]
  66.  
  67. # Output
  68. print("Process\tBurst\tCompletion\tTurnaround\tWaiting")
  69. for i in range(n):
  70. print(f"P{process_id[i]}\t{burst_times[i]}\t{completion[i]}\t\t{turnaround[i]}\t\t{waiting[i]}")
  71.  
  72. avg_wt = sum(waiting) / n
  73. avg_tat = sum(turnaround) / n
  74. print(f"\nAverage Turnaround Time = {avg_tat:.2f}")
  75. print(f"Average Waiting Time = {avg_wt:.2f}")
  76.  
  77.  
  78. # Example usage
  79. burst_time = [5, 3, 8, 6]
  80. fcfs_no_arrival(burst_time)
  81.  
  82. '''
  83. #SJF
  84. '''
  85. def sjf(arrival, burst):
  86. n = len(arrival)
  87. process_id = range(1, n + 1)
  88. completion = [0] * n
  89. waiting = [0] * n
  90. turnaround = [0] * n
  91. remaining_time = list(burst)
  92. arrival = list(arrival)
  93.  
  94. current_time = 0
  95. completed = 0
  96.  
  97. while completed < n:
  98. idx = -1
  99. min_time = float('inf')
  100.  
  101. # Find process with minimum remaining time at current_time
  102. for i in range(n):
  103. if arrival[i] <= current_time and remaining_time[i] > 0:
  104. if remaining_time[i] < min_time:
  105. min_time = remaining_time[i]
  106. idx = i
  107.  
  108. if idx != -1:
  109. remaining_time[idx] -= 1
  110. current_time += 1
  111.  
  112. if remaining_time[idx] == 0:
  113. completed += 1
  114. completion[idx] = current_time
  115. turnaround[idx] = completion[idx] - arrival[idx]
  116. waiting[idx] = turnaround[idx] - burst[idx]
  117. else:
  118. current_time += 1
  119.  
  120. # Output
  121. print("Process\tArrival\tBurst\tCompletion\tTurnaround\tWaiting")
  122. for i in range(n):
  123. print(f"P{process_id[i]}\t{arrival[i]}\t{burst[i]}\t{completion[i]}\t\t{turnaround[i]}\t\t{waiting[i]}")
  124.  
  125. avg_wt = sum(waiting) / n
  126. avg_tat = sum(turnaround) / n
  127. print(f"\nAverage Turnaround Time = {avg_tat:.2f}")
  128. print(f"Average Waiting Time = {avg_wt:.2f}")
  129.  
  130.  
  131. # Example test case
  132. arrival_time = [2, 1, 4, 0, 6]
  133. burst_time = [6, 3, 2, 5, 4]
  134. sjf(arrival_time, burst_time)
  135.  
  136. '''
  137. #RoundRobin
  138. '''
  139. from collections import deque
  140.  
  141. def round_robin_preemptive(arrival, burst, quantum):
  142. n = len(arrival)
  143. process_id = list(range(1, n + 1))
  144. remaining_burst = list(burst)
  145. completion = [0] * n
  146. waiting = [0] * n
  147. turnaround = [0] * n
  148. current_time = 0
  149. completed = 0
  150. queue = deque()
  151. is_in_queue = [False] * n
  152.  
  153. while completed < n:
  154. # Add newly arrived processes to queue
  155. for i in range(n):
  156. if arrival[i] <= current_time and not is_in_queue[i] and remaining_burst[i] > 0:
  157. queue.append(i)
  158. is_in_queue[i] = True
  159.  
  160. if queue:
  161. idx = queue.popleft()
  162.  
  163. # Execute process for min(quantum, remaining burst)
  164. time_to_run = min(quantum, remaining_burst[idx])
  165. current_time += time_to_run
  166. remaining_burst[idx] -= time_to_run
  167.  
  168. # Check for newly arrived processes during execution
  169. for i in range(n):
  170. if arrival[i] <= current_time and not is_in_queue[i] and remaining_burst[i] > 0:
  171. queue.append(i)
  172. is_in_queue[i] = True
  173.  
  174. if remaining_burst[idx] == 0:
  175. completed += 1
  176. completion[idx] = current_time
  177. turnaround[idx] = completion[idx] - arrival[idx]
  178. waiting[idx] = turnaround[idx] - burst[idx]
  179. else:
  180. queue.append(idx) # Put back into queue if not finished
  181. else:
  182. current_time += 1 # CPU idle, no ready process
  183.  
  184. # Output
  185. print("Process\tArrival\tBurst\tCompletion\tTurnaround\tWaiting")
  186. for i in range(n):
  187. print(f"P{process_id[i]}\t{arrival[i]}\t{burst[i]}\t{completion[i]}\t\t{turnaround[i]}\t\t{waiting[i]}")
  188.  
  189. avgwt = sum(waiting) / n
  190. avgtat = sum(turnaround) / n
  191. print(f"\nAverage Turnaround Time = {avgtat:.2f}")
  192. print(f"Average Waiting Time = {avgwt:.2f}")
  193.  
  194.  
  195. # Example usage
  196. arrival_time = [0, 1, 2, 3, 4]
  197. burst_time = [6, 3, 5, 2, 4]
  198. quantum = 2
  199.  
  200. round_robin_preemptive(arrival_time, burst_time, quantum)
  201.  
  202. '''
  203. #PriorityScheduling
  204. '''
  205. def priority_scheduling_preemptive(arrival, burst, priority):
  206. n = len(arrival)
  207. process_id = list(range(1, n + 1))
  208. remaining_time = list(burst)
  209. completion = [0] * n
  210. turnaround = [0] * n
  211. waiting = [0] * n
  212. is_completed = [False] * n
  213.  
  214. current_time = 0
  215. completed = 0
  216.  
  217. while completed < n:
  218. # Find process with highest priority (lowest value) that has arrived
  219. idx = -1
  220. highest_priority = float('inf')
  221. for i in range(n):
  222. if arrival[i] <= current_time and not is_completed[i] and remaining_time[i] > 0:
  223. if priority[i] < highest_priority:
  224. highest_priority = priority[i]
  225. idx = i
  226.  
  227. if idx != -1:
  228. # Execute the process for 1 unit of time
  229. remaining_time[idx] -= 1
  230. current_time += 1
  231.  
  232. if remaining_time[idx] == 0:
  233. completion[idx] = current_time
  234. turnaround[idx] = completion[idx] - arrival[idx]
  235. waiting[idx] = turnaround[idx] - burst[idx]
  236. is_completed[idx] = True
  237. completed += 1
  238. else:
  239. # No process is ready, CPU is idle
  240. current_time += 1
  241.  
  242. # Output
  243. print("Process\tArrival\tBurst\tPriority\tCompletion\tTurnaround\tWaiting")
  244. for i in range(n):
  245. print(f"P{process_id[i]}\t{arrival[i]}\t{burst[i]}\t{priority[i]}\t\t{completion[i]}\t\t{turnaround[i]}\t\t{waiting[i]}")
  246.  
  247. avgwt = sum(waiting) / n
  248. avgtat = sum(turnaround) / n
  249. print(f"\nAverage Turnaround Time = {avgtat:.2f}")
  250. print(f"Average Waiting Time = {avgwt:.2f}")
  251.  
  252.  
  253. # Example usage
  254. arrival_time = [0, 1, 2, 3, 4]
  255. burst_time = [6, 3, 8, 2, 4]
  256. priority = [2, 1, 4, 5, 3] # Lower value means higher priority
  257.  
  258. priority_scheduling_preemptive(arrival_time, burst_time, priority)
  259.  
  260. '''
Success #stdin #stdout 0.02s 7124KB
stdin
Standard input is empty
stdout
Standard output is empty