Skip to content

Commit f4560a2

Browse files
committed
added deadlock detetion code
1 parent acf6d8e commit f4560a2

File tree

3 files changed

+196
-7
lines changed

3 files changed

+196
-7
lines changed

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,8 @@ py -3.11 -m pip install numpy
4242
[Python File](./same_python_file/bankers_algorithm.py)    /    [Python File Alternative 2](./same_python_file/bankers_algorithm_alternative_2.py)    /    [Python File Alternative 3](./same_python_file/bankers_algorithm_alternative_3.py)    /    [Python File Alternative 4](./same_python_file/bankers_algorithm_alternative_4.py)
4343

4444
### Lab - 5 == Deadlock Detection
45-
[jupyter File](./inverse_exponential_distribution.ipynb) <br/>
46-
[Python File](./same_python_file/inverse_exponential_distribution.py)
45+
[jupyter File](./deadlock_detection.ipynb) <br/>
46+
[Python File](./same_python_file/deadlock_detection.py)
4747

4848

49-
<hr/><hr/><hr/>
50-
51-
### Sample
52-
[Jupyter File Alternative 2](./Sample.ipynb) &nbsp;&nbsp; / &nbsp;&nbsp; [Jupyter File Alternative 3](./Sample.ipynb) &nbsp;&nbsp; / &nbsp;&nbsp; [Jupyter File](./Sample.ipynb) <br/>
53-
[Python File Alternative 2](./same_python_file/Sample.py) &nbsp;&nbsp; / &nbsp;&nbsp; [Python File Alternative 3](./same_python_file/Sample.py)
49+
<hr/><hr/>

deadlock_detection.ipynb

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"def is_safe_state(available, max_claim, allocation):\n",
10+
" # Get the number of resources and processes\n",
11+
" num_resources = len(available)\n",
12+
" num_processes = len(max_claim)\n",
13+
"\n",
14+
" # Create a copy of available resources (work) and initialize finish array\n",
15+
" work = available.copy()\n",
16+
" finish = [False] * num_processes\n",
17+
"\n",
18+
" while True:\n",
19+
" found = False\n",
20+
" # Iterate through each process\n",
21+
" for i in range(num_processes):\n",
22+
" # Check if the process is not finished and if it can be executed\n",
23+
" if not finish[i] and all(work[j] >= max_claim[i][j] - allocation[i][j] for j in range(num_resources)):\n",
24+
" found = True\n",
25+
" # Mark the process as finished\n",
26+
" finish[i] = True\n",
27+
" # Add the allocated resources to the work vector\n",
28+
" work = [work[j] + allocation[i][j] for j in range(num_resources)]\n",
29+
" break\n",
30+
"\n",
31+
" # If no process can be executed, break out of the loop\n",
32+
" if not found:\n",
33+
" break\n",
34+
"\n",
35+
" # Check if all processes have finished (i.e., system is in a safe state)\n",
36+
" return all(finish)\n",
37+
"\n",
38+
"def deadlock_detection(available, max_claim, allocation):\n",
39+
" # Check if the current state is safe or in a deadlock\n",
40+
" if is_safe_state(available, max_claim, allocation):\n",
41+
" print(\"The system is in a safe state. No deadlock detected.\")\n",
42+
" else:\n",
43+
" print(\"Deadlock detected. The system is in an unsafe state.\")\n",
44+
"\n",
45+
"if __name__ == \"__main__\":\n",
46+
" # case 1\n",
47+
" # Define the available resources, maximum claim of each process, and current allocation\n",
48+
" available_resources = [3, 3, 2]\n",
49+
" max_claim_per_process = [\n",
50+
" [7, 5, 3],\n",
51+
" [3, 2, 2],\n",
52+
" [9, 0, 2],\n",
53+
" [2, 2, 2],\n",
54+
" [4, 3, 3]\n",
55+
" ]\n",
56+
" current_allocation = [\n",
57+
" [0, 1, 0],\n",
58+
" [2, 0, 0],\n",
59+
" [3, 0, 2],\n",
60+
" [2, 1, 1],\n",
61+
" [0, 0, 2]\n",
62+
" ]\n",
63+
"\n",
64+
" # Perform deadlock detection\n",
65+
" deadlock_detection(available_resources, max_claim_per_process, current_allocation)\n"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": null,
71+
"metadata": {},
72+
"outputs": [],
73+
"source": [
74+
"# case 2\n",
75+
"if __name__ == \"__main__\":\n",
76+
" # Define the deadlock scenario\n",
77+
" available_resources = [2, 2, 2]\n",
78+
" max_claim_per_process = [\n",
79+
" [3, 2, 2],\n",
80+
" [1, 2, 4],\n",
81+
" [4, 4, 3],\n",
82+
" [2, 3, 2]\n",
83+
" ]\n",
84+
" current_allocation = [\n",
85+
" [0, 1, 0],\n",
86+
" [0, 0, 1],\n",
87+
" [2, 1, 1],\n",
88+
" [1, 0, 0]\n",
89+
" ]\n",
90+
"\n",
91+
" # Perform deadlock detection\n",
92+
" deadlock_detection(available_resources, max_claim_per_process, current_allocation)\n"
93+
]
94+
}
95+
],
96+
"metadata": {
97+
"kernelspec": {
98+
"display_name": "Python 3",
99+
"language": "python",
100+
"name": "python3"
101+
},
102+
"language_info": {
103+
"codemirror_mode": {
104+
"name": "ipython",
105+
"version": 3
106+
},
107+
"file_extension": ".py",
108+
"mimetype": "text/x-python",
109+
"name": "python",
110+
"nbconvert_exporter": "python",
111+
"pygments_lexer": "ipython3",
112+
"version": "3.11.0"
113+
},
114+
"orig_nbformat": 4
115+
},
116+
"nbformat": 4,
117+
"nbformat_minor": 2
118+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
def is_safe_state(available, max_claim, allocation):
2+
# Get the number of resources and processes
3+
num_resources = len(available)
4+
num_processes = len(max_claim)
5+
6+
# Create a copy of available resources (work) and initialize finish array
7+
work = available.copy()
8+
finish = [False] * num_processes
9+
10+
while True:
11+
found = False
12+
# Iterate through each process
13+
for i in range(num_processes):
14+
# Check if the process is not finished and if it can be executed
15+
if not finish[i] and all(work[j] >= max_claim[i][j] - allocation[i][j] for j in range(num_resources)):
16+
found = True
17+
# Mark the process as finished
18+
finish[i] = True
19+
# Add the allocated resources to the work vector
20+
work = [work[j] + allocation[i][j] for j in range(num_resources)]
21+
break
22+
23+
# If no process can be executed, break out of the loop
24+
if not found:
25+
break
26+
27+
# Check if all processes have finished (i.e., system is in a safe state)
28+
return all(finish)
29+
30+
def deadlock_detection(available, max_claim, allocation):
31+
# Check if the current state is safe or in a deadlock
32+
if is_safe_state(available, max_claim, allocation):
33+
print("The system is in a safe state. No deadlock detected.")
34+
else:
35+
print("Deadlock detected. The system is in an unsafe state.")
36+
37+
if __name__ == "__main__":
38+
# Define the available resources, maximum claim of each process, and current allocation
39+
available_resources = [3, 3, 2]
40+
max_claim_per_process = [
41+
[7, 5, 3],
42+
[3, 2, 2],
43+
[9, 0, 2],
44+
[2, 2, 2],
45+
[4, 3, 3]
46+
]
47+
current_allocation = [
48+
[0, 1, 0],
49+
[2, 0, 0],
50+
[3, 0, 2],
51+
[2, 1, 1],
52+
[0, 0, 2]
53+
]
54+
55+
# Perform deadlock detection
56+
deadlock_detection(available_resources, max_claim_per_process, current_allocation)
57+
58+
# case 2
59+
# Define the deadlock scenario
60+
available_resources = [2, 2, 2]
61+
max_claim_per_process = [
62+
[3, 2, 2],
63+
[1, 2, 4],
64+
[4, 4, 3],
65+
[2, 3, 2]
66+
]
67+
current_allocation = [
68+
[0, 1, 0],
69+
[0, 0, 1],
70+
[2, 1, 1],
71+
[1, 0, 0]
72+
]
73+
74+
# Perform deadlock detection
75+
deadlock_detection(available_resources, max_claim_per_process, current_allocation)

0 commit comments

Comments
 (0)