To solve this problem, we need two counting semaphores — Full and Empty. Semaphore : A semaphore S is an integer variable that can be accessed only through two standard operations :. Mutual Exclusion: One or more than one resource are non-sharable Only one process can use at a time. A mutex provides mutual exclusion, either producer or consumer can have the key mutex and proceed with their work.
As long as the buffer is filled by producer, the consumer needs to wait, and vice versa. Initialization of semaphores. Solution for Producer. The value of mutex is also reduced to prevent consumer to access the buffer. The value of mutex is also increased by 1 beacuse the task of producer has been completed and consumer can access the buffer.
Solution for Consumer. The value of mutex is also increased so that producer can access the buffer now. Performing thread-switch number 3.
The system has been running for 17 ticks. Switching from thread-ID 2 to thread-ID 3. Next Prime from producer 3 is Thread yielded after using 16 ticks. Performing thread-switch number 4.
The system has been running for 33 ticks. Switching from thread-ID 3 to thread-ID 4. The system has been running for ticks. Switching from thread-ID 4 to thread-ID 5. Switching from thread-ID 5 to thread-ID 0. Thread yielded after using 0 ticks. Performing thread-switch number 7. Performing thread-switch number 8.
Performing thread-switch number 9. Performing thread-switch number Improve this question. Niklas Rosencrantz. Niklas Rosencrantz Niklas Rosencrantz Add a comment. Active Oldest Votes. Nick, you have a couple of issues. Let me know if you need anything else. Improve this answer. No One in Particular No One in Particular 2, 3 3 gold badges 24 24 silver badges 32 32 bronze badges.
But it's still completely illogical that the first primes are not consumer. We find no explaination and no solution for this bug. This is an exercise that I'm doing learning C and we should not be expected to get this problems that ruins the entire studies for illogical bugs. These cookies do not store any personal information. Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies.
It is mandatory to procure user consent prior to running these cookies on your website. Share This Article!!! Let's Discuss Cancel reply. Iconic One Theme Powered by Wordpress. This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Close Privacy Overview This website uses cookies to improve your experience while you navigate through the website.
Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. Related Articles. Table of Contents. Improve Article. Save Article. Like Article. Press 1 for Producer". Press 2 for Consumer".
0コメント