-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfcfss.py
More file actions
44 lines (32 loc) · 1003 Bytes
/
Copy pathfcfss.py
File metadata and controls
44 lines (32 loc) · 1003 Bytes
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
# FCFS Scheduling for given input
processes = [
["P1", 0, 2],
["P2", 1, 2],
["P3", 5, 3],
["P4", 6, 4]
]
# Sort by arrival time
processes.sort(key=lambda x: x[1])
completion_time = []
turnaround_time = []
waiting_time = []
time = 0
for p in processes:
name, arrival, burst = p
if time < arrival:
time = arrival # CPU waits for the process to arrive
ct = time + burst
completion_time.append(ct)
tat = ct - arrival
turnaround_time.append(tat)
wt = tat - burst
waiting_time.append(wt)
time = ct # update current time
# Display the results
print("Process | Arrival | Burst | Completion | Turn Around | Waiting")
for i in range(len(processes)):
p = processes[i]
print(" {:>5} | {:>6} | {:>5} | {:>8} | {:>9} | {:>6}"
.format(p[0], p[1], p[2], completion_time[i], turnaround_time[i], waiting_time[i]))
avg_wt = sum(waiting_time) / len(waiting_time)
print("\nAverage Waiting Time: {:.2f}".format(avg_wt))