Created
May 15, 2020 19:30
-
-
Save sXakil/0a39d593fff3ee6514ba931cfa938485 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
#######---TEST DATA for QUICK CHECKING---####### | |
# processes = 5 | |
# resources = 3 | |
# allocated = [[0, 1, 0 ],[ 2, 0, 0 ],[3, 0, 2 ],[2, 1, 1] ,[ 0, 0, 2]] | |
# max_demand = [[7, 5, 3 ],[3, 2, 2 ],[ 9, 0, 2 ],[2, 2, 2],[4, 3, 3]] | |
# available = [3, 3, 2] | |
processes = int(input('Number of processes: ')) | |
resources = int(input('Number of resources: ')) | |
print(f"Inputs for each allocated and maximum demand should be {resources} integer numbers separated by space") | |
allocated = np.array([[int(x) for x in input(f"Currently allocated for process P{i + 1}: ").split(' ')] for i in range(processes)]) | |
max_demand = np.array([[int(x) for x in input(f"Maximum demand from process P{i + 1}: ").split(' ')] for i in range(processes)]) | |
available = [int(x) for x in input('Available resources: ').split(' ')] | |
is_running = [True] * processes | |
final_sequence = [0] * processes | |
idx = 0 | |
resource_need = np.subtract(max_demand, allocated) | |
safe = False | |
count = processes | |
while count != 0: | |
safe = False | |
for i in range(processes): | |
if is_running[i]: | |
executing = True | |
for j in range(resources): | |
if (resource_need[i][j] > available[j]): | |
executing = False | |
break | |
if executing: | |
safe = True | |
count -= 1 | |
final_sequence[idx] = i | |
idx += 1 | |
for j in range(resources): | |
available[j] += allocated[i][j] | |
is_running[i] = False | |
if not safe: | |
print("Deadlock detected") | |
break | |
if safe: | |
for i in range(processes - 1): | |
print(f" P{final_sequence[i]} ->", end="") | |
print(f" P{final_sequence[processes - 1]}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment