Skip to content

Commit 80bd9d9

Browse files
committed
added user input for deadlock detection
1 parent f4560a2 commit 80bd9d9

File tree

3 files changed

+144
-1
lines changed

3 files changed

+144
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ py -3.11 -m pip install numpy
4343

4444
### Lab - 5 == Deadlock Detection
4545
[jupyter File](./deadlock_detection.ipynb) <br/>
46-
[Python File](./same_python_file/deadlock_detection.py)
46+
[Python File](./same_python_file/deadlock_detection.py) &nbsp;&nbsp; / &nbsp;&nbsp; [Python File Alternative](./same_python_file/deadlock_detection_alternative.py)
47+
4748

4849

4950
<hr/><hr/>

deadlock_detection.ipynb

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,85 @@
9191
" # Perform deadlock detection\n",
9292
" deadlock_detection(available_resources, max_claim_per_process, current_allocation)\n"
9393
]
94+
},
95+
{
96+
"cell_type": "markdown",
97+
"metadata": {},
98+
"source": [
99+
"### Alternative 2\n",
100+
"With input"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": null,
106+
"metadata": {},
107+
"outputs": [],
108+
"source": [
109+
"def input_data():\n",
110+
" global n, r, max_claim, allocation, avail\n",
111+
"\n",
112+
" n = int(input(\"Enter the number of processes: \"))\n",
113+
" r = int(input(\"Enter the number of resources: \"))\n",
114+
"\n",
115+
" print(\"Enter the Maximum Claim Matrix:\")\n",
116+
" max_claim = []\n",
117+
" for i in range(n):\n",
118+
" row = list(map(int, input().split()))\n",
119+
" max_claim.append(row)\n",
120+
"\n",
121+
" print(\"Enter the Allocation Matrix:\")\n",
122+
" allocation = []\n",
123+
" for i in range(n):\n",
124+
" row = list(map(int, input().split()))\n",
125+
" allocation.append(row)\n",
126+
"\n",
127+
" print(\"Enter the Available Resources:\")\n",
128+
" avail = list(map(int, input().split()))\n",
129+
"\n",
130+
"\n",
131+
"def show_data():\n",
132+
" global n, r, max_claim, allocation, avail\n",
133+
"\n",
134+
" print(\"Process\\tAllocation\\tMax\\t\\tAvailable\")\n",
135+
" for i in range(n):\n",
136+
" print(f\"P{i + 1}\\t\\t{allocation[i]}\\t\\t{max_claim[i]}\\t\\t{avail}\")\n",
137+
"\n",
138+
"\n",
139+
"def is_safe_state():\n",
140+
" global n, r, max_claim, allocation, avail\n",
141+
"\n",
142+
" work = avail.copy()\n",
143+
" finish = [False] * n\n",
144+
"\n",
145+
" while True:\n",
146+
" found = False\n",
147+
" for i in range(n):\n",
148+
" if not finish[i] and all(work[j] >= max_claim[i][j] - allocation[i][j] for j in range(r)):\n",
149+
" found = True\n",
150+
" finish[i] = True\n",
151+
" work = [work[j] + allocation[i][j] for j in range(r)]\n",
152+
" break\n",
153+
"\n",
154+
" if not found:\n",
155+
" break\n",
156+
"\n",
157+
" return all(finish)\n",
158+
"\n",
159+
"\n",
160+
"def deadlock_detection():\n",
161+
" if is_safe_state():\n",
162+
" print(\"The system is in a safe state. No deadlock detected.\")\n",
163+
" else:\n",
164+
" print(\"Deadlock detected. The system is in an unsafe state.\")\n",
165+
"\n",
166+
"\n",
167+
"if __name__ == \"__main__\":\n",
168+
" print(\"********** Deadlock Detection Algo ************\")\n",
169+
" input_data()\n",
170+
" show_data()\n",
171+
" deadlock_detection()\n"
172+
]
94173
}
95174
],
96175
"metadata": {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
def input_data():
2+
global n, r, max_claim, allocation, avail
3+
4+
n = int(input("Enter the number of processes: "))
5+
r = int(input("Enter the number of resources: "))
6+
7+
print("Enter the Maximum Claim Matrix:")
8+
max_claim = []
9+
for i in range(n):
10+
row = list(map(int, input().split()))
11+
max_claim.append(row)
12+
13+
print("Enter the Allocation Matrix:")
14+
allocation = []
15+
for i in range(n):
16+
row = list(map(int, input().split()))
17+
allocation.append(row)
18+
19+
print("Enter the Available Resources:")
20+
avail = list(map(int, input().split()))
21+
22+
23+
def show_data():
24+
global n, r, max_claim, allocation, avail
25+
26+
print("Process\tAllocation\tMax\t\tAvailable")
27+
for i in range(n):
28+
print(f"P{i + 1}\t\t{allocation[i]}\t\t{max_claim[i]}\t\t{avail}")
29+
30+
31+
def is_safe_state():
32+
global n, r, max_claim, allocation, avail
33+
34+
work = avail.copy()
35+
finish = [False] * n
36+
37+
while True:
38+
found = False
39+
for i in range(n):
40+
if not finish[i] and all(work[j] >= max_claim[i][j] - allocation[i][j] for j in range(r)):
41+
found = True
42+
finish[i] = True
43+
work = [work[j] + allocation[i][j] for j in range(r)]
44+
break
45+
46+
if not found:
47+
break
48+
49+
return all(finish)
50+
51+
52+
def deadlock_detection():
53+
if is_safe_state():
54+
print("The system is in a safe state. No deadlock detected.")
55+
else:
56+
print("Deadlock detected. The system is in an unsafe state.")
57+
58+
59+
if __name__ == "__main__":
60+
print("********** Deadlock Detection Algo ************")
61+
input_data()
62+
show_data()
63+
deadlock_detection()

0 commit comments

Comments
 (0)