-
Notifications
You must be signed in to change notification settings - Fork 24
/
BankersAlgorithm.py
32 lines (25 loc) · 1.33 KB
/
BankersAlgorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# -*- coding: UTF-8 -*-
import numpy as np # to run this make sure you have numpy library. To install run (on terminal): pip install numpy
n_processes = int(input('Number of processes > '))
n_resources = int(input('Number of resources > '))
available_resources = [int(x) for x in input('Claim vector? ').split(' ')]
currently_allocated = np.array(
[[int(x) for x in input('Currently allocated for process ' + str(i + 1) + '? ').split(' ')] for i in
range(n_processes)])
max_demand = np.array([[int(x) for x in input('Maximum demand from process ' + str(i + 1) + '? ').split(' ')] for i in
range(n_processes)])
total_available = available_resources - np.sum(currently_allocated, axis=0)
running = np.ones(n_processes) # An array with n_processes 1's to indicate if process is yet to run
while np.count_nonzero(running) > 0:
at_least_one_allocated = False
for p in range(n_processes):
if running[p]:
if all(i >= 0 for i in total_available - (max_demand[p] - currently_allocated[p])):
at_least_one_allocated = True
print(str(p) + ' is running')
running[p] = 0
total_available += currently_allocated[p]
if not at_least_one_allocated:
print('Status: Unsafe')
exit()
print('Status: Safe')