Consider the producer/consumer problem where the producer and consumer execute in separate processes synchronized by sempahores. Now, assume the producer can generate (data) items quickly and it is decided to update the consumer to a multi-threaded consumer. Also, assume the consumer has been changed to form three new consumer threads in addition to the thread running main(). You DO NOT have to write code for forming three new threads in the consumer. Now it is a requirement that all consumer threads fetch items at roughly the same rate i.e. if the producer generates three items then each consumer fetches one item and if the producer generates six items then each consumer fetches two items and so on. You are required to:
a) Describe a strategy to achieve the above requirement.
b) Also describe a strategy to ensure that all threads in the consumer access the shared buffer in a coordinated manner e.g. to ensure no two threads try to fetch the same item. Answer in a clear and concise way such that an able programmer can implement your suggested approach. You may include code snippets or skeleton code to illustrate your suggested approach, if needed
Answer:
According to first scenerio, as we have the two process, we can use both process at the same time, with the help of semaphores, so we can avoid the critical section problem, talking about how we can implement the same we can have a semaphore variable initially with zero, and we will set a condition, if we have s == 0, then only we can allow the process to use that buffer, and make sure to decremnet the semphore, now it will be -1, so if the next process will come it will see it is not zero, so it will wait until semaphore become zero, we can create a case when a process release a resource it should increment the semaphores value.
If we see in the second case now we have two threads instead of that process, yes we can apply the above scenario here also, because we know that thread works same like the independent process, so we can apply the same scenario here as well.
#please do upvote my rating, Thank You,
Get Answers For Free
Most questions answered within 1 hours.