fork download
  1. import random
  2. import time
  3.  
  4. class process:
  5. def __init__(self, id, arrival, burst):
  6. self.id = id
  7. self.arrival = arrival
  8. self.burst = burst
  9. self.completion = 0
  10. self.turnaround = 0
  11. self.waiting = 0
  12.  
  13. def fcfs_scheduling(processes):
  14. processes.sort(key=lambda x: x.arrival)
  15.  
  16. current_time = 0
  17. for proc in processes:
  18. if current_time < proc.arrival:
  19. current_time = proc.arrival
  20.  
  21. proc.completion = current_time + proc.burst
  22. proc.turnaround = proc.completion - proc.arrival
  23. proc.waiting = proc.turnaround - proc.burst
  24. current_time = proc.completion
  25.  
  26. return processes
  27.  
  28. random_processes = [
  29. process(chr(97+i), random.randint(0,5), random.randint(1,10))
  30. for i in range(random.randint(3,7))
  31. ]
  32.  
  33. scheduled = fcfs_scheduling(random_processes)
  34.  
  35. print("id arrival burst completion turnaround waiting")
  36. for proc in scheduled:
  37. time.sleep(0.5)
  38. print(f"{proc.id} {proc.arrival} {proc.burst} {proc.completion} {proc.turnaround} {proc.waiting}")
Success #stdin #stdout 0.14s 14304KB
stdin
id  arrival  burst
a     3       4
b     1       6
c     0       2
d     5       8
stdout
id  arrival  burst  completion  turnaround  waiting
a     0       4       4         4          0
c     0       7       11         11          4
e     0       6       17         17          11
b     1       8       25         24          16
d     2       5       30         28          23
g     3       2       32         29          27
f     5       9       41         36          27