{"id":3443,"date":"2023-08-14T18:39:38","date_gmt":"2023-08-14T13:09:38","guid":{"rendered":"https:\/\/www.interviewbit.com\/blog\/?p=3443"},"modified":"2023-08-14T18:45:24","modified_gmt":"2023-08-14T13:15:24","slug":"producer-consumer-problem","status":"publish","type":"post","link":"https:\/\/www.interviewbit.com\/blog\/producer-consumer-problem\/","title":{"rendered":"Producer Consumer Problem"},"content":{"rendered":"\n<div class=\"gutentoc tocactive nostyle\"><div class=\"gutentoc-toc-wrap\"><div class=\"gutentoc-toc-title-wrap\"><div class=\"gutentoc-toc-title\">Table Of Contents<\/div><div id=\"open\" class=\"text_open\">show<\/div><\/div><div id=\"toclist\"><div class=\"gutentoc-toc__list-wrap\"><ul class=\"gutentoc-toc__list\"><li><a href=\"#problem-statement\">Problem Statement<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#implementation-of-producer-code\">Implementation of producer code<\/a><\/li><li><a href=\"#implementation-of-consumer-code\">Implementation of consumer code<\/a><\/li><\/ul><li><a href=\"#faqs\">FAQs<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#q1-can-the-consumer-remove-an-item-from-the-buffer-if-full--0\">Q.1: Can the consumer remove an item from the buffer if full() = 0?<\/a><\/li><li><a href=\"#q2-can-the-producer-and-consumer-acquire-the-lock-simultaneously\">Q.2: Can the producer and consumer acquire the lock simultaneously?<\/a><\/li><\/ul><\/ul><\/div><\/div><\/div><\/div>\n\n\n\n<h2 id=\"problem-statement\">Problem Statement<\/h2>\n\n\n\n<p>Producer-Consumer Problem is also known as the <strong>bounded buffer problem<\/strong>. The Producer-Consumer<strong> Problem<\/strong> is one of the classic problems of synchronization.<\/p>\n\n\n\n<p>There is a buffer of <strong>N<\/strong> slots and each slot is capable of storing one unit of data.<br>There are two processes running, i.e. <strong>Producer <\/strong>and <strong>Consumer<\/strong>, which are currently operated in the buffer.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img  loading=\"lazy\"  width=\"656\"  height=\"539\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"\"  class=\"wp-image-3477 pk-lazyload\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 656px) 100vw, 656px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/11\/Image-1-2.png\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/11\/Image-1-2.png 656w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/11\/Image-1-2-300x246.png 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/11\/Image-1-2-380x312.png 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/11\/Image-1-2-550x452.png 550w\" ><\/figure>\n\n\n\n<p>There are certain restrictions\/conditions for both the <strong>producer <\/strong>and <strong>consumer<\/strong> process so that data synchronization can be done without interruption. These are as follows:<\/p>\n\n\n\n<ul><li>The <strong>producer<\/strong> tries to insert data into an empty slot of the buffer.<\/li><li>The <strong>consumer<\/strong> tries to remove data from a filled slot in the buffer.<\/li><li>The <strong>producer<\/strong> must not insert data when the buffer is full.<\/li><li>The <strong>consumer<\/strong> must not remove data when the buffer is empty.<\/li><li>The <strong>producer<\/strong> and <strong>consumer<\/strong> should not insert and remove data simultaneously.<\/li><\/ul>\n\n\n\n<p>For solving the <strong>producer-consumer<\/strong> problem, <strong>three semaphores <\/strong>are used:<\/p>\n\n\n\n<ul><li><strong>m(mutex): <\/strong>A binary semaphore which is used to acquire and release the lock.<\/li><li><strong>empty(), <\/strong>a counting semaphore whose initial value is the number of slots in the buffer, since initially, all slots are empty.<\/li><li><strong>full,<\/strong> a counting semaphore, whose initial value is 0.<\/li><\/ul>\n\n\n\n<h3 id=\"implementation-of-producer-code\">Implementation of producer code<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void Producer(){\n    do{\n        \/\/wait until empty > 0\n        wait(Empty);\n        wait(mutex);\n        add()\n        signal(mutex);\n        signal(Full);\n   }while(TRUE);\n}<\/pre>\n\n\n\n<p><strong>In the above code:<\/strong><\/p>\n\n\n\n<ul><li><strong>wait(empty): <\/strong>If the producer has to produce\/insert something into the buffer, it needs to first check whether there are empty slots in the buffer. If true, the producer inserts data into the buffer and then decrements one empty slot.<br><\/li><li><strong>wait(mutex): <\/strong>It is a binary semaphore, hence acquires the lock. This is shared among the <strong>producer <\/strong>and <strong>consumer<\/strong>. Hence, if the <strong>producer<\/strong> is acquiring the lock, the <strong>consumer <\/strong>cannot make any change in the buffer, until the lock is released.<br><\/li><li><strong>add(): <\/strong>It adds the data to the buffer.<br><\/li><li><strong>signal(mutex): <\/strong>It simply releases the lock acquired by the <strong>producer<\/strong> since the addition of data has been done in the buffer.<br><\/li><li><strong>signal(full): <\/strong>This increments the <strong>full semaphore<\/strong> since one of the empty slots has now been filled.<\/li><\/ul>\n\n\n\n<h3 id=\"implementation-of-consumer-code\">Implementation of consumer code<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void Producer(){\n    do{\n        \/\/wait until empty > 0\n        wait(full);\n        wait(mutex);\n        consume()\n        signal(mutex);\n        signal(empty);\n   }while(TRUE);\n}<\/pre>\n\n\n\n<p><strong>In the above code:<\/strong><\/p>\n\n\n\n<ul><li><strong>wait(full): <\/strong>If the consumer has to remove data from the buffer, it needs to first check whether the buffer contains some item or not. If true, the consumer removes the data from the buffer and then decrements one full slot.<br><\/li><li><strong>wait(mutex): <\/strong>It is a binary semaphore, hence acquires the lock. This is shared among the <strong>producer <\/strong>and <strong>consumer<\/strong>. Hence, if the <strong>consumer<\/strong> is acquiring the lock, the <strong>producer <\/strong>cannot make any change in the buffer, until the lock is released.<br><\/li><li><strong>consumer(): <\/strong>It removes the data from the buffer.<br><\/li><li><strong>signal(mutex): <\/strong>It simply releases the lock acquired by the <strong>producer<\/strong> since the addition of data has been done in the buffer.<br><\/li><li><strong>signal(empty): <\/strong>This increments the <strong>empty semaphore<\/strong> since one of the empty slots have now been emptied.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-black-background-color has-black-color is-style-wide\"\/>\n\n\n\n<h2 id=\"faqs\">FAQs<\/h2>\n\n\n\n<h3 id=\"q1-can-the-consumer-remove-an-item-from-the-buffer-if-full--0\"><span id=\"q-1-can-the-consumer-remove-an-item-from-the-buffer-if-full-0\">Q.1: Can the consumer remove an item from the buffer if full() = 0?<\/span><\/h3>\n\n\n\n<p><strong>Ans: <\/strong>No, if there are no items in the buffer, i.e. <strong>full<\/strong> semaphore\u00a0 = 0, the consumer cannot remove any item.<\/p>\n\n\n\n<h3 id=\"q2-can-the-producer-and-consumer-acquire-the-lock-simultaneously\"><span id=\"q-2-can-the-producer-and-consumer-acquire-the-lock-simultaneously\">Q.2: Can the producer and consumer acquire the lock simultaneously?<\/span><\/h3>\n\n\n\n<p><strong>Ans: <\/strong>No, either the <strong>producer <\/strong>or <strong>consumer <\/strong>can acquire the lock at a time. The operation <strong>wait(mutex)<\/strong> is used to acquire the lock.<\/p>\n","protected":false},"excerpt":{"rendered":"Problem Statement Producer-Consumer Problem is also known as the bounded buffer problem. The Producer-Consumer Problem is one of&hellip;\n","protected":false},"author":5,"featured_media":3480,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_daextam_enable_autolinks":"1","csco_singular_sidebar":"","csco_page_header_type":"","csco_appearance_grid":"","csco_page_load_nextpost":"","csco_post_video_location":[],"csco_post_video_location_hash":"","csco_post_video_url":"","csco_post_video_bg_start_time":0,"csco_post_video_bg_end_time":0},"categories":[145],"tags":[540],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/3443"}],"collection":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/comments?post=3443"}],"version-history":[{"count":5,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/3443\/revisions"}],"predecessor-version":[{"id":22725,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/3443\/revisions\/22725"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/media\/3480"}],"wp:attachment":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/media?parent=3443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/categories?post=3443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/tags?post=3443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}