Skip to content

Commit fb7e47d

Browse files
committed
FCFS
1 parent a191ced commit fb7e47d

File tree

1 file changed

+78
-40
lines changed

1 file changed

+78
-40
lines changed

cpu_sheduling.py

Lines changed: 78 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import prettytable
23
at_prcs_mapping = {} # arrivaltime : processess mapping
34
bt_at_mapping = {} # burst time : arrival time mapping
@@ -18,54 +19,95 @@ def __init__(self, name:list = [], arrival_time:list = [], burst_time:list = [],
1819
print(table)
1920
print()
2021

22+
def unique_at(self)->list:
23+
unique_at = []
24+
for at in self.arrival_time:
25+
if at not in unique_at:
26+
unique_at.append(at)
27+
unique_at.sort()
28+
return unique_at
29+
30+
def at_mapping(self)-> dict:
31+
for index, at in enumerate(self.arrival_time):
32+
if at not in at_prcs_mapping:
33+
at_prcs_mapping[at] = [self.process[index]]
34+
else:
35+
at_prcs_mapping[at].append(self.process[index])
36+
return at_prcs_mapping
37+
38+
def bt_mapping(self)->dict:
39+
for index, at in enumerate(self.arrival_time):
40+
if at not in bt_at_mapping:
41+
bt_at_mapping[at] = [self.burst_time[index]]
42+
else:
43+
bt_at_mapping[at].append(self.burst_time[index])
44+
return bt_at_mapping
45+
46+
def final_data(self,mapping:dict)->list:
47+
listed_data = []
48+
for prcs in self.unique_at():
49+
listed_data.append(mapping[prcs])
50+
data = [process for sublist in listed_data for process in sublist]
51+
return data
52+
53+
def correction(self,arrival_time:list, ct:list)->list:
54+
correction_index = 0
55+
correction_value = 0
56+
57+
for at in range(len(ct)-1):
58+
if arrival_time[at+1] > ct[at]:
59+
correction_value = arrival_time[at+1] - ct[at]
60+
correction_index = at+1
61+
return [correction_value, correction_index]
62+
2163
def fcfs(self):
2264
"""
2365
first come first serve short term shdeuling
2466
"""
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]})
67+
execution_order = self.final_data(self.at_mapping())
68+
process_ord = copy.deepcopy(execution_order)
69+
bt_ord = self.final_data(self.bt_mapping())
2970

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-
4871
# calculating completion time of each process
4972
ct = []
5073
for j in bt_ord:
5174
if ct:
5275
temp = ct[-1] + j
5376
else:
5477
temp = j
55-
ct.append(temp)
78+
ct.append(temp)
79+
80+
at = sorted(self.arrival_time)
81+
print(at, ct)
5682

57-
print()
58-
print(30*"-","first come first serve shedule",30*"-")
59-
print()
6083

84+
crrction_val, crrction_index = self.correction(at, ct)
85+
print(crrction_val, crrction_index)
86+
if crrction_val == 0:
87+
pass
88+
else:
89+
process_ord.insert(crrction_index,f"halt for {crrction_val} sec(s)")
90+
91+
for correction in ct[crrction_index:]:
92+
ct[crrction_index] += crrction_val
93+
crrction_index += 1
94+
95+
print("fcfs order: ",end="")
96+
for process in process_ord:
97+
if process == process_ord[-1]:
98+
print(f"{process}",end="")
99+
else:
100+
print(f"{process} -> ",end="")
101+
print();print()
102+
61103
# list of turn around time for everyprocess
62-
tat_list = [a-b for a,b in zip(ct,at)]
104+
tat_list = [a-b for a,b in zip(ct,sorted(self.arrival_time))]
63105

64106
# average turn around time
65107
tat = sum(tat_list) / len(tat_list)
66108

67109
# list of waiting time for each process
68-
wt_list = [a-b for a,b in zip(tat_list,bt)]
110+
wt_list = [a-b for a,b in zip(tat_list,bt_ord)]
69111

70112
# average waiting time
71113
wt = sum(wt_list) / len(wt_list)
@@ -74,11 +116,12 @@ def fcfs(self):
74116
table = prettytable.PrettyTable()
75117
table.field_names = ["Process", "Arrival Time", "Burst Time", "Completion Time", "Turn around time", "waiting time"]
76118
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]])
119+
table.add_row([execution_order[i], at[i], bt_ord[i],ct[i],tat_list[i],wt_list[i]])
78120
print(table)
79121
print(f"turn around time -> {tat}")
80122
print(f"average waiting time was -> {wt}")
81-
123+
124+
82125
def sjf(self):
83126
"""
84127
shortest job first: non-preemtive
@@ -96,17 +139,12 @@ def rr(self):
96139
round robbin
97140
"""
98141
...
99-
142+
100143
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
144+
prcs =["P1","P2","P3","P4"] #process
145+
at = [0,1,5,6] # arrival time
146+
bt = [2,2,3,4] # burst time
104147
shedule = CpuSheduling(prcs,at,bt)
105148
shedule.fcfs()
106-
107-
108-
109-
110-
111-
149+
112150

0 commit comments

Comments
 (0)