-
Notifications
You must be signed in to change notification settings - Fork 1
/
p&c.html
299 lines (267 loc) · 9.92 KB
/
p&c.html
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Producer & Consumer</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
/* background-color: rgb(77, 75, 75); */
}
body {
background-color: #e6e6e6;
}
body {
font-family: Arial, Helvetica, sans-serif;
margin:0;
padding:0;
}
#navbar {
overflow: hidden;
background-color: #1a1a1a;
}
#navbar a {
float: left;
font-size: 20px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
#dropdown {
float: right;
overflow: hidden;
min-width:260px;
/*position: absolute;
right:50px;*/
}
#dropdown #dropbtn {
font-size: 20px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
width:260px;
}
#navbar a:hover, #dropdown:hover #dropbtn {background-color: red;
}
#dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 260px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
#dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
#dropdown-content a:hover {
background-color: #ddd;
}
#dropdown:hover #dropdown-content {
display: block;
}
#heading {
background-color: #595959;
height: 65px;
text-align: center;
color: #d3d2d2;
width:450px;
margin:auto;
margin-bottom: 50px;
border-bottom-left-radius: 25px;
border-bottom-right-radius: 25px;
}
.submit-btn{
width: 60%;
}
.buffer-wrapper{
display: inline-flex;
}
.buffers{
margin: 5px;
padding: 10px;
border: 2px solid black;
border-radius: 8px;
}
</style>
</head>
<body>
<div id="navbar">
<a href="index.html">Deadlock & Concurrency</a>
<div id="dropdown">
<button id="dropbtn">Algorithms
<i class="fa fa-caret-down"></i>
</button>
<div id="dropdown-content">
<a href="lock_variable.html">Lock Variable</a>
<a href="TSL.html">TSL Mechanism</a>
<a href="turn_variable.html">Turn Variable</a>
<a href="binary_semaphore.html">Binary Semaphore</a>
<a href="counting_semaphore.html">Counting Semaphore</a>
<a href="Peterson's_algorithm.html">Peterson Solution</a>
<a href="p&c.html">Producer & Consumer</a>
<a href="dining_philosophers.html">Dining Philosopher</a>
<a href="Bankers.html">Banker's algorithm</a>
</div>
</div>
</div>
<div id="heading">
<h1 style="padding-top: 6px;">Producer & Consumer</h1>
</div>
<div class="container">
<div class="p-4">
<div class="text-center">
<h3>Producer & Consumer</h3>
</div>
</div>
<div class="row">
<div class="col-6">
<div class="form-group">
<label>Enter Maximum Buffer Size</label>
<input type="number" class="form-control" id="buffersize">
<strong><small class="text-danger" id="err1"></small></strong>
</div>
</div>
<div class="col-6">
<div class="pt-4 mt-1">
<button class="btn btn-info submit-btn" id="submit-buffer-size">Submit</button>
</div>
</div>
</div>
<button id="produce" class="btn btn-outline-success">PRODUCER</button>
<button id="consume" class="btn btn-outline-danger">CONSUMER</button>
<div id="buffer-goes-here">
</div>
</div>
<div class="mt-4">
<div class="toast" id="myToast" style="position: absolute; top: 10%; right: 0;">
<div class="toast-header bg-danger">
<strong class="mr-auto text-white" id="errmsg"></strong>
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast">
<span aria-hidden="true">×</span>
</button>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script>
$(document).ready(function () {
let value;
let semaphore = true;
let track = -1;
let inc = 0;
let counsumeTrack = -1;
$('#submit-buffer-size').click(function (e) {
e.preventDefault();
value = $('#buffersize').val();
let designBuffer = '';
if (value !== undefined && value !== '') {
if (value > 0) {
$('#err1').text('');
$('#design-goes-here').empty();
designBuffer += '<div class="m-4 buffer-wrapper">';
for (let index = 0; index < value; index++) {
const idname = 'buffer' + index;
designBuffer += '<div class="buffers" id="'+ idname +'"></div>';
}
designBuffer += '</div>';
$('#buffer-goes-here').html(designBuffer);
track = -1;
counsumeTrack = -1;
} else {
$('#err1').text('Value Must Be Grater Than 0');
}
} else {
$('#err1').text('Please Enter Value To Proceed');
}
});
$('#produce').click(function (e) {
e.preventDefault();
if (value > 0) {
if (semaphore) {
semaphore = false;
if (track === value - 1) {
track = 0;
console.log('object');
} else {
const ind = track + 1;
console.log('#buffer' + ind);
console.log($('#buffer' + ind).text());
if ($('#buffer' + ind).text() === '') {
track++;
}
}
if ($('#buffer' + track).text() === '') {
inc++;
$('#buffer' + track).text(inc);
} else {
$('#errmsg').text('No Space Avaiable To Produce');
$("#myToast").toast({ delay: 3000 });
$("#myToast").toast('show');
}
semaphore = true;
} else {
$('#errmsg').text('This operation is not available as it is performed by the consumer')
$('#myToast').toast({ delay: 3000 });
$('#myToast').toast('show');
}
} else {
$('#errmsg').text('Please Add Buffer First');
$('#myToast').toast({ delay: 3000 });
$('#myToast').toast('show');
}
});
$('#consume').click(function (e) {
e.preventDefault();
if (value > 0) {
if (semaphore) {
semaphore = false;
if (counsumeTrack === value - 1) {
counsumeTrack = 0;
} else {
const index = counsumeTrack + 1;
if (counsumeTrack === -1) {
counsumeTrack++;
} else if ($('#buffer' + index).text() !== '') {
counsumeTrack++;
}
}
if ($('#buffer' + counsumeTrack).text() !== '') {
$('#buffer' + counsumeTrack).text('');
} else {
$('#errmsg').text('Nothing To Consume');
$("#myToast").toast({ delay: 3000 });
$("#myToast").toast('show');
}
semaphore = true;
} else {
$('#errmsg').text('This operation is not available as it is performed by the producer');
$('#myToast').toast({ delay: 3000 });
$('#myToast').toast('show');
}
} else {
$('#errmsg').text('Please Add Buffer First');
$('#myToast').toast({ delay: 3000 });
$('#myToast').toast('show');
}
});
});
</script>
</body>
</html>