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?
a. As the producer and consumer will run the operations simultaneously, thus if the first operation would be done for the producer then it would be fine, and if the first operation is for the consumer. Then it can cause the problem as there are not enough buffer. Thus this can lead the producer to starve and will have to wait until the producer is done producing 1MB buffer.
b. This can go on seamless as there is the need of 4 and which is present in the buffer. Thus the consumer will not starve and will not have to wait for the buffer.
c. So for this problem, we can go for the mutex locks in which we will check for the lock before entering the producer or consumer operation. and if it is not locked means no operation is going on thus we can enter in and make the mutex lock enabled after entering. Thus no one can enter the loop while other is working.
Thanks
Get Answers For Free
Most questions answered within 1 hours.