1
1
import copy
2
2
import 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]
5
5
6
6
class CpuSheduling ():
7
7
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 = [],
10
10
self .burst_time = burst_time
11
11
self .time_quantum = time_quantum
12
12
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
+
13
25
# displaying processess, arival time and burst time in a tabular format
14
26
print (10 * "-" ,"given process data" ,10 * "-" )
15
27
table = prettytable .PrettyTable ()
@@ -19,7 +31,9 @@ def __init__(self, name:list = [], arrival_time:list = [], burst_time:list = [],
19
31
print (table )
20
32
print ()
21
33
34
+
22
35
def unique_at (self )-> list :
36
+ """ returns unique arrival time in ascending order"""
23
37
unique_at = []
24
38
for at in self .arrival_time :
25
39
if at not in unique_at :
@@ -28,6 +42,7 @@ def unique_at(self)->list:
28
42
return unique_at
29
43
30
44
def at_mapping (self )-> dict :
45
+ """ returns mapping of arrival time and processess as a dictionary"""
31
46
for index , at in enumerate (self .arrival_time ):
32
47
if at not in at_prcs_mapping :
33
48
at_prcs_mapping [at ] = [self .process [index ]]
@@ -36,6 +51,7 @@ def at_mapping(self)-> dict:
36
51
return at_prcs_mapping
37
52
38
53
def bt_mapping (self )-> dict :
54
+ """ returns mapping of burst time and arrival time as a dictionary"""
39
55
for index , at in enumerate (self .arrival_time ):
40
56
if at not in bt_at_mapping :
41
57
bt_at_mapping [at ] = [self .burst_time [index ]]
@@ -44,13 +60,15 @@ def bt_mapping(self)->dict:
44
60
return bt_at_mapping
45
61
46
62
def final_data (self ,mapping :dict )-> list :
63
+ """ returns a list of processess in the order of their arrival time or burst time"""
47
64
listed_data = []
48
65
for prcs in self .unique_at ():
49
66
listed_data .append (mapping [prcs ])
50
67
data = [process for sublist in listed_data for process in sublist ]
51
68
return data
52
69
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"""
54
72
correction_index = 0
55
73
correction_value = 0
56
74
@@ -64,9 +82,9 @@ def fcfs(self):
64
82
"""
65
83
first come first serve short term shdeuling
66
84
"""
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
70
88
71
89
# calculating completion time of each process
72
90
ct = []
@@ -77,12 +95,10 @@ def fcfs(self):
77
95
temp = j
78
96
ct .append (temp )
79
97
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
83
100
84
- crrction_val , crrction_index = self .correction (at , ct )
85
- print (crrction_val , crrction_index )
101
+ # inserting halt for correction
86
102
if crrction_val == 0 :
87
103
pass
88
104
else :
@@ -92,6 +108,7 @@ def fcfs(self):
92
108
ct [crrction_index ] += crrction_val
93
109
crrction_index += 1
94
110
111
+ # printing process order
95
112
print ("fcfs order: " ,end = "" )
96
113
for process in process_ord :
97
114
if process == process_ord [- 1 ]:
@@ -142,7 +159,7 @@ def rr(self):
142
159
143
160
if __name__ == "__main__" :
144
161
prcs = ["P1" ,"P2" ,"P3" ,"P4" ] #process
145
- at = [0 ,1 ,5 ,6 ] # arrival time
162
+ at = [0 ,1 ,5 ,12 ] # arrival time
146
163
bt = [2 ,2 ,3 ,4 ] # burst time
147
164
shedule = CpuSheduling (prcs ,at ,bt )
148
165
shedule .fcfs ()
0 commit comments