anyone can help with this programming assignment and teach me how to work on jBa
ID: 3872829 • Letter: A
Question
anyone can help with this programming assignment and teach me how to work on jBaci ?
Bounded Buffer producer/consumer:
In this programming assignment you have to implement the bounded buffer producer-consumer using the jBaci environment. The specifications are:
Buffer size: 4
Producer will iterate 20 times.
Consumer will iterate 20 times.
Produce item: To produce an item you have to initialize a variable, say x = 0; then in each iteration x is incremented by one therefore the generated items will be: 1 in he first iteration, 2 in the second iteration, 3 in the third iteration, and so on. Each time an item is inserted in buffer, the program must print: "item k placed into buffer"
Consume item: multiply by 10 the value got from buffer and create a dummy loop that iterates from 1 to item x 10 and then write " item j was taken from buffer"
Explanation / Answer
ANSWER::
monitor boundedbuffer
{
char buffer[N];
int nextin, nextout;
int count;
condition notfull, notempty;
void append (char x)
{
if (count==N) waitc(notfull);
buffer[nextin] = x;
nextin = (nextin + 1) % N;
count++;
signalc(notempty);
}
void take (char x)
{
if (count ==0) waitc (notempty);
x = buffer[nextout];
nextout = (nextout + 1) % N;
count--;
signalc(notfull);
}
init
{
nextin = 0;
nextout = 0;
count = 0;
}
}
void producer()
{
char x;
while (true)
{
produce(x);
append(x);
}
}
void consumer()
{
char x;
while (true)
{
take(x);
consume(x);
}
}
void main()
{
cobegin
{
producer;
consumer;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.