Customer buyProduct () { while ( numProduct = = 0) { signal ( customers); wait (
ID: 3858209 • Letter: C
Question
Customer buyProduct () {
while ( numProduct = = 0) {
signal ( customers);
wait (Product);
}
while (Product.NotEmpty)signal(Product);
numProduct --;
}
Clerk
sellProduct(){
if(numProduct!=0)wait(customers);
numProduct = P;
}
a) Change the code such that it will properly work with a Signal and Exit policy. Consider the monitor concept and not its java implementation.
b) What will be the outcome of eliminating if(numProduct!=0) in the clerk code? Clearly explain and give an execution sequence that shows the outcome.
Explanation / Answer
a)Here I am having a variable cust.product which will get value if customer select product.
Customer buyProduct () {
while ( numProduct = = 0) {
signal ( customers);
wait (Product);
}
if(cust.Product!=0)
while (Product.NotEmpty)signal(Product);
if(Product==cust.Product)
numProduct --;
}
Clerk
sellProduct(){
if(numProduct!=0)wait(customers);
numProduct = P;
}
b.
If we eliminate if(numProduct!=0) then if product is not available it will wait the customer and try to process the product but in this case it will go in loop where it will not find any product.
Clerk
sellProduct(){
wait(customers);
numProduct = P;
}
if num.product =0
then it will hold the customer and try to assign P to numProduct which is 0 in this case.
if num.Product !=0 then the code will run properly.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.