Question

Given the following semaphores: int mutex //mutual exclusion to shared buffer: initialized to 1 int empty;...

Given the following semaphores:

int mutex //mutual exclusion to shared buffer: initialized to 1

int empty; //count of empty buffer slots: initialized to n


producer {

while (true) {

Produce new resource;

wait(empty); // wait for empty buffer

wait(mutex); // lock buffer

Add resource to an empty slot;

signal(mutex); // unlock buffer

}

}

consumer {

while (true) {

wait(mutex); // lock buffer

Remove resource;

signal(mutex); // unlock buffer

signal(empty);

Consume resource;

}

}


Describe why this solution does not work. Give an example of a situation where this will fail.

Homework Answers

Answer #1

In this code;

On the producer side,

1. Producer produces a new item

2. It then waits on the semaphore to become empty.

3. It then waits for the lock to become available. When the lock is available, it acquires it and proceeds with the insert operation into the slot.

4. After adding the next item produced to the bounded buffer, the producer process calls signal(mutex) to release the lock.

On the consumer side,

1. Here consumer waits for the lock to become available. When the lock is available, it acquires it and proceeds with the delete operation into the slot.

2. After deletion, it will release the lock

3.  And then increments empty (signal(empty)).

This code doesn't increment the number of item in the buffer after adding an element to the buffer.

This code will results in bounded buffer problem

This problem is also called the Producers and Consumers problem. A finite supply of containers is available. Producers take an empty container and fill it with a product. Consumers take a full container, consume the product and leave an empty container. The main complexity of this problem is that we must maintain the count for both the number of empty and full containers that are available.

Without proper synchronization, the following errors may occur.

  • The producers doesn’t block when the buffer is full.
  • A Consumer consumes an empty slot in the buffer.
  • A consumer attempts to consume a slot that is only half-filled by a producer.
  • Two producers writes into the same slot.
  • Two consumers reads the same slot.

upvote pls...

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT