-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprod.html
291 lines (243 loc) · 9.41 KB
/
prod.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Producer_Consumer</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body style="background-image: url('blck.jpg');height:1500px">
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="../index.html">OS simulator</a>
</div>
<ul class="nav navbar-nav">
<li class="active"><a href="index.html">Home</a></li>
<li class="active"><a href="wiki.html">Wiki</a></li>
</ul>
</div>
</nav>
<br><br><br>
<div class="jumbotron">
<h1 style="color: rgb(15, 8, 8);float:center;text-align: center;"><b><u>Producer Consumer</b> </u></h1>
</div>
<div style="padding: 23%;">
<br><br><br><br>
<button class="btn btn-primary" id ="prod", type ="button", onclick ="barrieradd()", style ="height:10%;width:15%;position:absolute;top:38%;left:1%;font-size:25px;border:solid 4px black;">Proceed</button>
<button class="btn btn-primary" id ="prod1", type ="button", style ="height:10%;width:12%;position:absolute;top:80%;left:15%;font-size:25px;background: red;border:solid 4px black;" disabled="">Produce</button>
<button class="btn btn-primary" id ="con", type ="button", style ="height:10%;width:12%;position:absolute;top:80%;right:15%;font-size:25px;background: red;border:solid 4px black;" disabled="">Consume</button>
<button class="btn btn-primary" type = "button", id ="num", name = "num", class = "btn btn-primary", style="height:70px;width:300px;position:absolute;top:55%;left:63%;border:solid 4px black;font-size:17px">Press on Produce </button>
<button class="btn btn-primary" type = "button", id ="num", name = "num", class = "btn btn-primary", style="height:70px;width:300px;position:absolute;top:55%;left:20%;border:solid 4px black;font-size:17px">Buffer size:5 </button>
<button type = "button", id ="one", name = "one", style="height:100px;width:165px;position:absolute;border:solid 4px black;top:65%;left:45%;background-color:white"> </button>
<button type = "button", id ="two", name = "two", style="height:100px;width:165px;border:solid 4px black;position:absolute;top:75%;left:45%;background-color:white"> </button>
<button type = "button", id ="three", name = "three", style="height:100px;width:165px;border:solid 4px black;position:absolute;top:85%;left:45%;background-color:white"> </button>
<button type = "button", id ="four", name = "four", style="height:100px;width:165px;border:solid 4px black;position:absolute;top:95%;left:45%;background-color:white"> </button>
<button type = "button", id ="five", name = "five", style="height:100px;width:165px;border:solid 4px black;position:absolute;top:105%;left:45%;background-color:white"> </button>
</div>
<button class="btn btn-warning" id ="count", type ="button" style ="height:100px;width:165px;border:solid 4px black;position:absolute;top:46%;left:45%;">counter = 0</button>
<div class="jumbotron jumbotron-fluid" style="text-align: left;">
<div class="container-fluid">
<p>The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue. The producer's job is to generate data, put it into the buffer, and start again. At the same time, the consumer is consuming the data (i.e., removing it from the buffer), one piece at a time. The problem is to make sure that the producer won't try to add data into the buffer if it's full and that the consumer won't try to remove data from an empty buffer.</p>
<br><br>
<h2><b><u>Producer algo</u></b></h2>
<p><br>
while(1)<br>
{ <br>
/* produce an item in next_produced */<br>
while (counter==BUFFER_SIZE)<br>
; /* do nothing */<br>
buffer[in] = next_produced;<br>
in = (in + 1) % BUFFER_SIZE;<br>
counter++;<br>
}
</p>
</div>
</div>
<div class="jumbotron jumbotron-fluid" style="text-align: left;">
<div class="container-fluid">
<h2><b><u>Consumer algo</u></b></h2>
<p><br>
while(1)<br>
{<br>
while(counter == 0)<br>
; /* do nothing */<br>
next_consumed = buffer[out];<br>
out = (out+1) % BUFFER_SIZE;<br>
counter--;<br>
/* consume an item in next_consumed */<br>
}<br>
</p>
</div>
</div>
</body>
<script type="text/javascript">
var queue = [0,0,0,0,0]; var flag=0;
var rear =-1; var front =-1;
function counter()
{
var counter = rear-front +1;
// console.log(rear)
if(rear == -1 )
{
var change = document.getElementById("count");
change.innerHTML ="counter = 0"
}
else if(counter==1)
{
var change = document.getElementById("count");
change.innerHTML ="counter = 1"
}
if(counter==2)
{
var change = document.getElementById("count");
change.innerHTML ="counter = 2"
}
if(counter==3)
{
var change = document.getElementById("count");
change.innerHTML ="counter = 3"
}
if(counter==4)
{
var change = document.getElementById("count");
change.innerHTML ="counter = 4"
}
if(counter==5)
{
var change = document.getElementById("count");
change.innerHTML ="counter = 5"
}
}
function check()
{
if(flag>0)
{
document.getElementById("con").disabled = false;
}
if(flag<4)
{
document.getElementById("prod1").disabled = false;
}
}
function color()
{
if(queue[0]==1)
{
document.getElementById("one").style.backgroundColor = '#00BFFF';
}
if(queue[1]==1)
{
document.getElementById("two").style.backgroundColor = '#00BFFF';
}
if(queue[2]==1)
{
document.getElementById("three").style.backgroundColor = '#00BFFF';
}
if(queue[3]==1)
{
document.getElementById("four").style.backgroundColor = '#00BFFF';
}
if(queue[4]==1)
{
document.getElementById("five").style.backgroundColor = '#00BFFF';
}
if(queue[0]==0)
{
document.getElementById("one").style.backgroundColor = '#FFFFFF';
}
if(queue[1]==0)
{
document.getElementById("two").style.backgroundColor = '#FFFFFF';
}
if(queue[2]==0)
{
document.getElementById("three").style.backgroundColor = '#FFFFFF';
}
if(queue[3]==0)
{
document.getElementById("four").style.backgroundColor = '#FFFFFF';
}
if(queue[4]==0)
{
document.getElementById("five").style.backgroundColor = '#FFFFFF';
}
check();
}
function produce()
{
flag++;
if(flag==5)
{
document.getElementById("prod1").disabled = true;
}
// document.getElementById("prod1").style.color=green;
if(front == -1 && rear ==-1)
{
front =0;
rear =0;
queue[rear] =1;
}
else
{
rear = (rear+1)%5;
queue[rear]=1;
}
color();
counter();
}
function consume()
{
// myMove();
flag--;
if(flag==0)
{
document.getElementById("con").disabled = true;
}
if(front==rear)
{
queue[front] =0;
front =-1;
rear =-1;
}
else
{
queue[front]=0;
front = (front+1)%5;
}
color();
counter();
}
function barrieradd()
{
var n = Math.random();
num = Math.floor(n*2);
if(flag==0 && num==1)
{
alert("No item to consume for the consumer!");
document.getElementById("num").innerHTML = "No item to consume";
}
else if(flag==5 && num==0)
{
document.getElementById("num").innerHTML = "buffer Full to produce";
alert("Buffer full can't add the produced item to the buffer!");
}
else if(num==0)
{
document.getElementById("prod1").style.backgroundColor='green';
document.getElementById("con").style.backgroundColor='red';
document.getElementById("num").innerHTML = "producer";
produce();
}
else
{
document.getElementById("con").style.backgroundColor='green';
document.getElementById("prod1").style.backgroundColor='red';
document.getElementById("num").innerHTML = "consumer";
consume();
}
}
</script>
</html>