11import copy
22import prettytable
3- at_prcs_mapping = {} # arrivaltime : processess mapping
4- bt_at_mapping = {} # burst time : arrival time mapping
3+ at_prcs_mapping = {} # arrivaltime : [ processess]
4+ bt_at_mapping = {} # burst time : [processess]
55
66class CpuSheduling ():
77 def __init__ (self , name :list = [], arrival_time :list = [], burst_time :list = [], time_quantum = None ) -> None :
@@ -10,6 +10,18 @@ def __init__(self, name:list = [], arrival_time:list = [], burst_time:list = [],
1010 self .burst_time = burst_time
1111 self .time_quantum = time_quantum
1212
13+ # checking if every process has a arrival time and burst time
14+ if len (self .process ) != len (self .arrival_time ):
15+ raise ValueError ("Number of process(s) don't match number of arrival time(s) or vice versa" )
16+ if len (self .process ) != len (self .burst_time ):
17+ raise ValueError ("Number of process(s) don't match number of burst time(s) or vice versa" )
18+
19+ # checking if arrival time and burst time are of integer or float type
20+ if not all (isinstance (at , (int , float )) for at in self .arrival_time ):
21+ raise ValueError ("arrival time can only have integer/float value(s)" )
22+ if not all (isinstance (bt , (int , float )) for bt in self .burst_time ):
23+ raise ValueError ("burst time can only have integer/float value(s)" )
24+
1325 # displaying processess, arival time and burst time in a tabular format
1426 print (10 * "-" ,"given process data" ,10 * "-" )
1527 table = prettytable .PrettyTable ()
@@ -19,7 +31,9 @@ def __init__(self, name:list = [], arrival_time:list = [], burst_time:list = [],
1931 print (table )
2032 print ()
2133
34+
2235 def unique_at (self )-> list :
36+ """ returns unique arrival time in ascending order"""
2337 unique_at = []
2438 for at in self .arrival_time :
2539 if at not in unique_at :
@@ -28,6 +42,7 @@ def unique_at(self)->list:
2842 return unique_at
2943
3044 def at_mapping (self )-> dict :
45+ """ returns mapping of arrival time and processess as a dictionary"""
3146 for index , at in enumerate (self .arrival_time ):
3247 if at not in at_prcs_mapping :
3348 at_prcs_mapping [at ] = [self .process [index ]]
@@ -36,6 +51,7 @@ def at_mapping(self)-> dict:
3651 return at_prcs_mapping
3752
3853 def bt_mapping (self )-> dict :
54+ """ returns mapping of burst time and arrival time as a dictionary"""
3955 for index , at in enumerate (self .arrival_time ):
4056 if at not in bt_at_mapping :
4157 bt_at_mapping [at ] = [self .burst_time [index ]]
@@ -44,13 +60,15 @@ def bt_mapping(self)->dict:
4460 return bt_at_mapping
4561
4662 def final_data (self ,mapping :dict )-> list :
63+ """ returns a list of processess in the order of their arrival time or burst time"""
4764 listed_data = []
4865 for prcs in self .unique_at ():
4966 listed_data .append (mapping [prcs ])
5067 data = [process for sublist in listed_data for process in sublist ]
5168 return data
5269
53- def correction (self ,arrival_time :list , ct :list )-> list :
70+ def check_halt (self ,arrival_time :list , ct :list )-> list :
71+ """ returns index and value if any halt is present in the process order"""
5472 correction_index = 0
5573 correction_value = 0
5674
@@ -64,9 +82,9 @@ def fcfs(self):
6482 """
6583 first come first serve short term shdeuling
6684 """
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 ())
85+ execution_order = self .final_data (self .at_mapping ()) # process order
86+ process_ord = copy .deepcopy (execution_order ) # process order for printing if correction is required
87+ bt_ord = self .final_data (self .bt_mapping ()) # burst time in the order of arrival time
7088
7189 # calculating completion time of each process
7290 ct = []
@@ -77,12 +95,10 @@ def fcfs(self):
7795 temp = j
7896 ct .append (temp )
7997
80- at = sorted (self .arrival_time )
81- print (at , ct )
82-
98+ at = sorted (self .arrival_time ) # sorted arrival time
99+ crrction_val , crrction_index = self .check_halt (at , ct ) # correction value and index
83100
84- crrction_val , crrction_index = self .correction (at , ct )
85- print (crrction_val , crrction_index )
101+ # inserting halt for correction
86102 if crrction_val == 0 :
87103 pass
88104 else :
@@ -92,6 +108,7 @@ def fcfs(self):
92108 ct [crrction_index ] += crrction_val
93109 crrction_index += 1
94110
111+ # printing process order
95112 print ("fcfs order: " ,end = "" )
96113 for process in process_ord :
97114 if process == process_ord [- 1 ]:
@@ -142,7 +159,7 @@ def rr(self):
142159
143160if __name__ == "__main__" :
144161 prcs = ["P1" ,"P2" ,"P3" ,"P4" ] #process
145- at = [0 ,1 ,5 ,6 ] # arrival time
162+ at = [0 ,1 ,5 ,12 ] # arrival time
146163 bt = [2 ,2 ,3 ,4 ] # burst time
147164 shedule = CpuSheduling (prcs ,at ,bt )
148165 shedule .fcfs ()
0 commit comments