-
Notifications
You must be signed in to change notification settings - Fork 0
/
bankers.js
190 lines (182 loc) · 8.46 KB
/
bankers.js
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
function validation(){
var a = document.getElementById('resourceA').value;
var b = document.getElementById('resourceB').value;
var c = document.getElementById('resourceC').value;
if( a=='' || b=='' || c==''){
alert("Resource instance value can't blank");
}
if( a<0 || b<0 || c<0){
alert("Resource instance value can't be negative")
}
let count = 0;
let count2 = 0;
for(var i=1; i<=5; i++){
for(var j=1; j<=3; j++){
if(document.getElementById('a'+i+j).value<0 || document.getElementById('a'+i+j).value==''){
count++;
}
if(document.getElementById('m'+i+j).value<0 || document.getElementById('m'+i+j).value==''){
count2++;
}
}
document.getElementById('calc'+i).value = '';
}
if(count>0){
alert("Allocation matrix can't be negative or blank!")
}
if(count2>0){
alert("Max Need matrix can't be negative or blank!")
}
document.getElementById('calc0').value = '';
}
function reset(){
document.getElementById('resourceA').value = '';
document.getElementById('resourceB').value = '';
document.getElementById('resourceC').value = '';
for(var i=1; i<=5; i++){
for(var j=1; j<=3; j++){
document.getElementById('a'+i+j).value='';
document.getElementById('m'+i+j).value='';
document.getElementById('n'+i+j).value='';
}
document.getElementById('p'+i).value = ''
document.getElementById('calc'+i).innerHTML = '';
}
document.getElementById('av11').value = '';
document.getElementById('av12').value = '';
document.getElementById('av13').value = '';
document.getElementById('calc0').innerHTML = '';
}
function sample(){
sam = [[0,1,0],
[2,0,0],
[3,0,2],
[2,1,1],
[0,0,2]];
max = [[7,5,3],
[3,2,2],
[9,0,2],
[2,2,2],
[4,3,3]];
for(var i=1; i<=5; i++){
for(var j=1; j<=3; j++){
document.getElementById('a'+i+j).value = sam[i-1][j-1];
document.getElementById('m'+i+j).value = max[i-1][j-1];
}
}
document.getElementById('resourceA').value = 10;
document.getElementById('resourceB').value = 5;
document.getElementById('resourceC').value = 7;
document.getElementById('calc0').innerHTML = ' Sample Loaded...';
}
function find_avai(){
var a = document.getElementById('resourceA').value;
var b = document.getElementById('resourceB').value;
var c = document.getElementById('resourceC').value;
var x = 0;
var y = 0;
var z = 0;
document.getElementById('calc1').innerHTML = '  Calculation for available matrix:'+'<br/>';
document.getElementById('calc1').innerHTML += 'Available instance of A = '+a+' - ';
document.getElementById('calc2').innerHTML = 'Available instance of B = '+b+' - ';
document.getElementById('calc3').innerHTML = 'Available instance of C = '+c+' - ';
for(var i=1; i<=5; i++){
var x = x + parseInt(document.getElementById('a'+i+'1').value);
document.getElementById('calc1').innerHTML += parseInt(document.getElementById('a'+i+'1').value);
if(i<5){
document.getElementById('calc1').innerHTML += ' + ';
}
else{
document.getElementById('calc1').innerHTML += ' = '
}
var y = y + parseInt(document.getElementById('a'+i+'2').value);
document.getElementById('calc2').innerHTML += parseInt(document.getElementById('a'+i+'1').value);
if(i<5){
document.getElementById('calc2').innerHTML += '+';
}
else{
document.getElementById('calc2').innerHTML += ' = '
}
var z = z + parseInt(document.getElementById('a'+i+'3').value);
document.getElementById('calc3').innerHTML += parseInt(document.getElementById('a'+i+'1').value);
if(i<5){
document.getElementById('calc3').innerHTML += '+';
}
else{
document.getElementById('calc3').innerHTML += ' = '
}
}
document.getElementById('av11').value = a-x;
document.getElementById('calc1').innerHTML += a-x;
document.getElementById('av12').value = b-y;
document.getElementById('calc2').innerHTML += b-y;
document.getElementById('av13').value = c-z;
document.getElementById('calc3').innerHTML += c-z;
document.getElementById('calc4').innerHTML = '';
document.getElementById('calc5').innerHTML = '';
document.getElementById('calc0').innerHTML = '';
validation();
}
function find_need(){
// find_avai();
document.getElementById('calc1').innerHTML = 'Calculation for Need Matrix:'+'<br/>';
document.getElementById('calc2').innerHTML = '';
document.getElementById('calc3').innerHTML = '';
document.getElementById('calc4').innerHTML = '';
document.getElementById('calc5').innerHTML = '';
document.getElementById('calc0').innerHTML = '';
for(var i=1; i<=5; i++){
for(var j=1; j<=3; j++){
document.getElementById('n'+i+j).value = parseInt(document.getElementById('m'+i+j).value) - parseInt(document.getElementById('a'+i+j).value);
document.getElementById('calc'+i).innerHTML += 'Need['+(i-1)+']'+'['+(j-1)+']'+' = '+document.getElementById('m'+i+j).value + ' - ' + document.getElementById('a'+i+j).value+' = '+document.getElementById('n'+i+j).value+'      ';
}
}
}
function find_sequence(){
var dp = 0;//updated if process cannot be allocated resources due to insufficient availability.(else condn)
var checker = 0;//helps in determining if a deadlock situation has occurred.
var q = 1;//the position where the process ID (e.g., 'P1', 'P2', etc.) will be inserted in the HTML document
var k=1;//the starting point for the next iteration of process allocation.
document.getElementById('calc0').innerHTML = '  Calculation of final order'+'<br/>';
for(var j=1; j<=5; j++){
x1 = parseInt(document.getElementById('av11').value);
x2 = parseInt(document.getElementById('av12').value);
x3 = parseInt(document.getElementById('av13').value);
document.getElementById('calc'+j).innerHTML = 'Step'+j+':  '+'Available instance = '+x1+', '+x2+', '+x3 +'<br/>';
for(var i=k; i<=5; i++){
var ex1 = parseInt(document.getElementById('a'+i+'1').value);
var ex2 = parseInt(document.getElementById('a'+i+'2').value);
var ex3 = parseInt(document.getElementById('a'+i+'3').value);
if(ex1!= 0 || ex2!= 0 || ex3!= 0){
if(x1 >= parseInt(document.getElementById('n'+i+'1').value) && x2 >= parseInt(document.getElementById('n'+i+'2').value) && x3 >= parseInt(document.getElementById('n'+i+'3').value)){
document.getElementById('p'+q).value = 'P'+i;
document.getElementById('calc'+j).innerHTML += '   As Need['+(i)+'] = ( '+document.getElementById('n'+i+'1').value+', '+document.getElementById('n'+i+'2').value+', '
+document.getElementById('n'+i+'3').value+' ) < Available = ( '+x1+', '+x2+', '+x3
+' ) => Process P'+i+' is selected.'+'<br/>'+'And New Available Matrix is ( '+document.getElementById('av11').value+', '+document.getElementById('av12').value+', '+document.getElementById('av13').value+' ) + ( '+document.getElementById('a'+i+'1').value+', '+document.getElementById('a'+i+'2').value+', '
+document.getElementById('a'+i+'3').value+' ) = ';
document.getElementById('av11').value = parseInt(document.getElementById('av11').value) + parseInt(document.getElementById('a'+i+'1').value);
document.getElementById('av12').value = parseInt(document.getElementById('av12').value) + parseInt(document.getElementById('a'+i+'2').value);
document.getElementById('av13').value = parseInt(document.getElementById('av13').value) + parseInt(document.getElementById('a'+i+'3').value);
document.getElementById('calc'+j).innerHTML += '( '+document.getElementById('av11').value+', '+document.getElementById('av12').value+', '+document.getElementById('av13').value+' )';
document.getElementById('a'+i+'1').value = '0';
document.getElementById('a'+i+'2').value = '0';
document.getElementById('a'+i+'3').value = '0';
k=i+1;
if (k==6){
k=1;
}
q = q + 1;
checker = checker + 1;
break;
}
else{
dp = i;
}
}
}
}
if(checker == 0){
alert("Deadlock!!");
reset();
}
}