File tree 2 files changed +29
-0
lines changed
2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change 71
71
" \n " ,
72
72
" # The main function to demonstrate the Banker's algorithm\n " ,
73
73
" if __name__ == \" __main__\" :\n " ,
74
+ " # case 1\n " ,
74
75
" max_resources = [\n " ,
75
76
" [7, 5, 3],\n " ,
76
77
" [3, 2, 2],\n " ,
105
106
"metadata" : {},
106
107
"outputs" : [],
107
108
"source" : [
109
+ " # case 2\n " ,
108
110
" if __name__ == \" __main__\" :\n " ,
109
111
" max_resources = [\n " ,
110
112
" [7, 5, 3],\n " ,
Original file line number Diff line number Diff line change @@ -63,6 +63,8 @@ def check_safety(self):
63
63
64
64
# The main function to demonstrate the Banker's algorithm
65
65
if __name__ == "__main__" :
66
+
67
+ # case 1
66
68
max_resources = [
67
69
[7 , 5 , 3 ],
68
70
[3 , 2 , 2 ],
@@ -89,3 +91,28 @@ def check_safety(self):
89
91
print ("Request is safe to be granted." )
90
92
else :
91
93
print ("Request may lead to a deadlock and cannot be granted." )
94
+
95
+
96
+ # case 2
97
+ max_resources = [
98
+ [7 , 5 , 3 ],
99
+ [3 , 2 , 2 ],
100
+ [9 , 0 , 2 ],
101
+ ]
102
+
103
+ allocated_resources = [
104
+ [0 , 1 , 0 ],
105
+ [2 , 0 , 0 ],
106
+ [3 , 0 , 2 ],
107
+ ]
108
+
109
+ total_resources = [10 , 5 , 7 ]
110
+
111
+ banker = BankersAlgorithm (max_resources , allocated_resources , total_resources )
112
+ request = [5 , 0 , 0 ]
113
+ process_id = 0
114
+
115
+ if banker .is_safe_state (request , process_id ):
116
+ print ("Request is safe to be granted." )
117
+ else :
118
+ print ("Request may lead to a deadlock and cannot be granted." )
You can’t perform that action at this time.
0 commit comments