For this task, consider the producer/consumer problem.
You have a buffer of 4096KB
there are 4 consumer threads and 2 producer threads.
2.1 What problem may arise when simultaneously performing 4 consume
from buffer
and 1 produce to buffer operations? Assume that there are currently
3 elements
of 1MB each in the buffer.
2.2 What problem may arise when performing 4 consume from buffer
operations
simultaneously if there are 4 elements of 1MB each in the
buffer?
2.3 Given that a consumer cannot consume from the buffer when the
producer is
writing to the buffer, how would you implement this
producer/consumer problem
such that the maximum parallelism can be achieved?
Answer-
2.1- Currently there are only 3 elements in the buffer. So we can only consume upto 3 elements of the buffer. After that consumer can not consume further element. Producer can produce 1 element and after performing this process only 1 element will present in the buffer.
2.2- In the buffer, total 4 elements are present. So we can perform 4 simultaneously consume operation and there will be no problem occur.
2.3- If either producer or consumer writing to the buffer, so in mean while other can not be perform it's task. This problem is known as inconsistency problem or concurrency problem. For solving this problem we need to synchronized the producer consumer problem while sharing the common data. There are some important points-
*Producer need to go sleep or discard the data if the buffer is already full.
*Consumer need to notify the producer when it going to remove element from the full buffer.
*Consumer need to go sleep or discard the operation when buffer is empty.
*Producer need to notify the consumer whenever it again start to produce element in the buffer.
Get Answers For Free
Most questions answered within 1 hours.