Skip to content

Commit 06529ab

Browse files
authored
basic implemenation of fcfs
- as if for now processess can't have same arrival time
1 parent 87654dd commit 06529ab

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

cpu_sheduling.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import prettytable
2+
at_prcs_mapping = {} # arrivaltime : processess mapping
3+
bt_at_mapping = {} # burst time : arrival time mapping
4+
5+
class CpuSheduling():
6+
def __init__(self, name:list = [], arrival_time:list = [], burst_time:list = [], time_quantum= None) -> None:
7+
self.process = name
8+
self.arrival_time = arrival_time
9+
self.burst_time = burst_time
10+
self.time_quantum = time_quantum
11+
12+
# displaying processess, arival time and burst time in a tabular format
13+
print(10*"-","given process data",10*"-")
14+
table = prettytable.PrettyTable()
15+
table.field_names = ["Process", "Arrival Time", "Burst Time"]
16+
for i in range(len(self.process)):
17+
table.add_row([self.process[i], self.arrival_time[i], self.burst_time[i]])
18+
print(table)
19+
print()
20+
21+
def fcfs(self):
22+
"""
23+
first come first serve short term shdeuling
24+
"""
25+
for i in range(len(self.process)):
26+
# creating arrival time : process & burst time : arrival time mapping
27+
at_prcs_mapping.update({self.arrival_time[i]:self.process[i]})
28+
bt_at_mapping.update({self.arrival_time[i]:self.burst_time[i]})
29+
30+
# sorted arriavl time
31+
self.arrival_time.sort()
32+
33+
# burst time in order of process execution
34+
bt_ord = []
35+
36+
print("fcfs order: ",end="")
37+
for k in self.arrival_time:
38+
# printing keys(process) of arrival time(from sorted arrival time list(self.arrival_time))
39+
if k == self.arrival_time[-1]:
40+
print(f"{at_prcs_mapping.get(k)}",end="")
41+
else:
42+
print(f"{at_prcs_mapping.get(k)} -> ",end="")
43+
44+
# appending burts time of process in the order of their execution
45+
bt_ord.append(bt_at_mapping.get(k))
46+
print()
47+
48+
# calculating completion time of each process
49+
ct = []
50+
for j in bt_ord:
51+
if ct:
52+
temp = ct[-1] + j
53+
else:
54+
temp = j
55+
ct.append(temp)
56+
57+
print()
58+
print(30*"-","first come first serve shedule",30*"-")
59+
print()
60+
61+
# list of turn around time for everyprocess
62+
tat_list = [a-b for a,b in zip(ct,at)]
63+
64+
# average turn around time
65+
tat = sum(tat_list) / len(tat_list)
66+
67+
# list of waiting time for each process
68+
wt_list = [a-b for a,b in zip(tat_list,bt)]
69+
70+
# average waiting time
71+
wt = sum(wt_list) / len(wt_list)
72+
73+
# printing process, arival time, burst time, completion time, turn around time, waiting time
74+
table = prettytable.PrettyTable()
75+
table.field_names = ["Process", "Arrival Time", "Burst Time", "Completion Time", "Turn around time", "waiting time"]
76+
for i in range(len(self.process)):
77+
table.add_row([at_prcs_mapping.get(i), self.arrival_time[i], self.burst_time[i],ct[i],tat_list[i],wt_list[i]])
78+
print(table)
79+
print(f"turn around time -> {tat}")
80+
print(f"average waiting time was -> {wt}")
81+
82+
def sjf(self):
83+
"""
84+
shortest job first: non-preemtive
85+
"""
86+
...
87+
88+
def srtf(self):
89+
"""
90+
shortest remaining time first : preemitive
91+
"""
92+
...
93+
94+
def rr(self):
95+
"""
96+
round robbin
97+
"""
98+
...
99+
100+
if __name__ == "__main__":
101+
prcs = ["P1","P2","P3","P4","P5"] #process
102+
at = [0,1,2,3,4] # arrival time
103+
bt = [8,1,3,2,6] # burst time
104+
shedule = CpuSheduling(prcs,at,bt)
105+
shedule.fcfs()
106+
107+
108+
109+
110+
111+
112+

0 commit comments

Comments
 (0)