-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfcfs.py
More file actions
45 lines (37 loc) · 1.55 KB
/
Copy pathfcfs.py
File metadata and controls
45 lines (37 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def main():
try:
num = int(input("How many process you want to enter : "))
if num <= 0:
raise ValueError("Number of processes must be positive")
except ValueError as e:
print(f"Invalid input: {e}")
return
input_list = []
p_time = []
index = []
for i in range(num):
print(f"Process {i + 1}:")
arrival_time = int(input(" Enter arrival time: "))
input_list.append(arrival_time)
processing_time = int(input(" Enter processing time: "))
p_time.append(processing_time)
index.append(f"P{i + 1}")
# Sort processes by arrival time
processes = list(zip(input_list, p_time, index))
processes.sort(key=lambda x: x[0]) # Sort by arrival time
input_list, p_time, index = zip(*processes)
input_list, p_time, index = list(input_list), list(p_time), list(index)
print("\n---------------------FCFS-------------------------\n")
# Show the actual execution order after sorting
print("Final Process order:", ", ".join(index))
print("\nGantt Chart:")
currenttime = max(input_list[0], 0) # Start at arrival time or 0
print(f"{index[0]} --- {currenttime} : {currenttime + p_time[0]}")
currenttime += p_time[0]
for i in range(1, len(index)):
# Check if there's idle time before next process
if input_list[i] > currenttime:
print(f"IDLE --- {currenttime} : {input_list[i]}")
currenttime = input_list[i]
print(f"{index[i]} --- {currenttime} : {currenttime + p_time[i]}")
currenttime += p_time[i]