-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiple_instance.html
More file actions
230 lines (184 loc) · 7.99 KB
/
Copy pathmultiple_instance.html
File metadata and controls
230 lines (184 loc) · 7.99 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<!DOCTYPE html>
<html>
<head>
<title>Deadlock Detection for Multiple Instance Resources</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: #f1f1f1;
background-image: url("images/bg.jpg");
}
.container {
max-width: 600px;
margin: 0 auto;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 5px;
padding: 20px;
text-align: center;
}
h1 {
margin-top: 0;
text-align: center;
}
form {
margin-bottom: 20px;
}
label {
display: block;
font-weight: bold;
margin-bottom: 5px;
}
table {
width: 100%;
margin-bottom: 10px;
border-collapse: collapse;
display: inline-block;
text-align: left;
}
table th,
table td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
table th {
background-color: #f2f2f2;
}
.btn {
padding: 8px 16px;
background-color: #4CAF50;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
}
.result {
margin-top: 20px;
padding: 10px;
background-color: #fff;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<h1>Deadlock Detection for Multiple Instance Resources</h1>
<form id="deadlockForm">
<label for="numResources">Number of Resources:</label>
<input type="number" id="numResources" required><br>
<label for="numProcesses">Number of Processes:</label>
<input type="number" id="numProcesses" required><br>
<h2>Allocation Matrix</h2>
<table id="allocationMatrix"></table>
<h2>Request Matrix</h2>
<table id="requestMatrix"></table>
<button type="button" class="btn" onclick="detectDeadlock()">Detect Deadlock</button>
</form>
<div id="result" class="result"></div>
<script>
// Generate input fields for allocation and request matrices based on the user's input
function generateInputFields() {
var numResources = parseInt(document.getElementById("numResources").value);
var numProcesses = parseInt(document.getElementById("numProcesses").value);
var allocationMatrix = document.getElementById("allocationMatrix");
allocationMatrix.innerHTML = generateMatrixHTML(numProcesses, numResources, "allocation");
var requestMatrix = document.getElementById("requestMatrix");
requestMatrix.innerHTML = generateMatrixHTML(numProcesses, numResources, "request");
}
// Generate HTML code for the allocation and request matrices
function generateMatrixHTML(numRows, numCols, matrixType) {
var matrixHTML = "<tr><th></th>";
for (var j = 0; j < numCols; j++) {
matrixHTML += "<th>R" + j + "</th>";
}
matrixHTML += "</tr>";
for (var i = 0; i < numRows; i++) {
matrixHTML += "<tr><th>P" + i + "</th>";
for (var j = 0; j < numCols; j++) {
matrixHTML += "<td><input type='number' id='" + matrixType + "-" + i + "-" + j + "' required></td>";
}
matrixHTML += "</tr>";
}
return matrixHTML;
}
// Function to detect deadlock in the resource allocation graph
function detectDeadlock() {
var numResources = parseInt(document.getElementById("numResources").value);
var numProcesses = parseInt(document.getElementById("numProcesses").value);
var allocation = [];
var request = [];
// Read values from the allocation and request matrices
for (var i = 0; i < numProcesses; i++) {
var allocationRow = [];
var requestRow = [];
for (var j = 0; j < numResources; j++) {
var allocationValue = parseInt(document.getElementById("allocation-" + i + "-" + j).value);
var requestValue = parseInt(document.getElementById("request-" + i + "-" + j).value);
allocationRow.push(allocationValue);
requestRow.push(requestValue);
}
allocation.push(allocationRow);
request.push(requestRow);
}
var deadlock = detectDeadlockHelper(numResources, numProcesses, allocation, request);
var resultElement = document.getElementById("result");
resultElement.innerHTML = deadlock ? "Deadlock detected!" : "No deadlock detected.";
}
// Function to check if a cycle exists in the wait-for graph
function isCycle(process, visited, recursionStack, n, wfg) {
if (visited[process] === false) {
visited[process] = true;
recursionStack[process] = true;
for (var i = 0; i < n; i++) {
if (wfg[process][i]) {
if (!visited[i] && isCycle(i, visited, recursionStack, n, wfg))
return true;
else if (recursionStack[i])
return true;
}
}
}
recursionStack[process] = false;
return false;
}
// Function to detect deadlock in the resource allocation graph
function detectDeadlockHelper(m, n, allocation, request) {
var wfg = [];
var visited = [];
var recursionStack = [];
// Create wait-for graph (WFG) based on resource allocation and request matrices
for (var i = 0; i < n; i++) {
wfg.push(new Array(n).fill(false));
visited.push(false);
recursionStack.push(false);
for (var j = 0; j < m; j++) {
if (request[i][j] > 0) {
// Process i is waiting for resource j
for (var k = 0; k < n; k++) {
if (allocation[k][j] > 0) {
// Resource j is allocated to process k
wfg[i][k] = true;
}
}
}
}
}
// Check for cycles in the wait-for graph (WFG)
for (var i = 0; i < n; i++) {
if (isCycle(i, visited, recursionStack, n, wfg))
return true;
}
return false;
}
document.getElementById("deadlockForm").addEventListener("submit", function (event) {
event.preventDefault();
});
// Call generateInputFields function when the number of resources and processes are entered
document.getElementById("numResources").addEventListener("change", generateInputFields);
document.getElementById("numProcesses").addEventListener("change", generateInputFields);
</script>
</div>
</body>
</html>