{"id":2908,"date":"2021-10-23T10:52:12","date_gmt":"2021-10-23T05:22:12","guid":{"rendered":"https:\/\/www.interviewbit.com\/blog\/?p=2908"},"modified":"2021-10-23T10:52:13","modified_gmt":"2021-10-23T05:22:13","slug":"dining-philosophers-problem","status":"publish","type":"post","link":"https:\/\/www.interviewbit.com\/blog\/dining-philosophers-problem\/","title":{"rendered":"Dining Philosophers Problem"},"content":{"rendered":"\n<div class=\"gutentoc tocactive nostyle\" style=\"max-width:400px\"><div class=\"gutentoc-toc-wrap\"><div class=\"gutentoc-toc-title-wrap\"><div class=\"gutentoc-toc-title\">Table Of Contents<\/div><div id=\"open\" class=\"toggletwo\">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><li><a href=\"#approach---\">Approach<strong><br><\/strong><\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#c-implementation\">C++ Implementation<\/a><\/li><li><a href=\"#java-implementation\">Java Implementation<\/a><\/li><li><a href=\"#python-implementation\">Python Implementation<\/a><\/li><\/ul><li><a href=\"#frequently-asked-questions\">Frequently Asked Questions<\/a><\/li><\/ul><\/div><\/div><\/div><\/div>\n\n\n\n<h2 id=\"problem-statement\">Problem Statement<\/h2>\n\n\n\n<p>The <strong>Dining Philosophers<\/strong> <strong>Problem <\/strong>is a classic resource-sharing synchronization problem. It is particularly used for situations, where multiple resources need to be allocated.<\/p>\n\n\n\n<p>There are five philosophers sitting around a circular dining table. The table has a bowl of spaghetti and five chopsticks.<\/p>\n\n\n\n<p>At any given time, a philosopher will either think or eat. For eating, he uses two chopsticks- one from his left and another from his right. When a philosopher thinks, he keeps down both the chopsticks at their place.<\/p>\n\n\n\n<p>A fork can be held by only one philosopher at any given time i.e. no two philosophers can use the same fork.&nbsp; After a philosopher finishes eating, they put the forks back to their original places. A philosopher can only eat when both the forks are available to him, i.e. forks on his left and right should not be used by any other philosopher at the same time.<\/p>\n\n\n\n<p>The problem is to design an effective algorithm, such that no philosopher has to starve, i.e. he can continue thinking and eating alternately for an indefinite amount of time.<br><\/p>\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=\"approach---\"><span id=\"approach\">Approach<strong><br><\/strong><\/span><\/h2>\n\n\n\n<p>This problem was structured to tackle the issue of deadlocks which occurs during multiple resource sharing on an operating system.<\/p>\n\n\n\n<p>A deadlock is a situation in which two computer programs sharing the same resource are effectively preventing each other from accessing the resource, resulting in both programs ceasing to function.<\/p>\n\n\n\n<p>Now let us understand how to effectively solve the <strong>Dining Philosophers Problem<\/strong>.<\/p>\n\n\n\n<p>Let us consider the philosophers to be processed in an <strong>OS<\/strong> and the chopsticks to be shared resources. Now if we observe clearly, each process needs two resources, out of which, one of the resources it has already acquired, but another resource is in use for another process. Due to this, till the time the other process does not release its resource, the current process cannot proceed further.<\/p>\n\n\n\n<p>In turn, the other processes are dependent on another process for its resources and this goes on and on.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img  loading=\"lazy\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"\"  class=\"wp-image-3086 pk-lazyload\"  width=\"621\"  height=\"494\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 621px) 100vw, 621px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-8.png\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-8.png 812w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-8-300x239.png 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-8-768x612.png 768w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-8-380x303.png 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-8-550x438.png 550w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-8-800x637.png 800w\" ><\/figure><\/div>\n\n\n\n<p>Image Source &#8211; Google images<br><\/p>\n\n\n\n<p>Therefore, every process is waiting for some other process, hence they are in a <strong>circular wait<\/strong>. This leads the system to a <strong>deadlock<\/strong>.<\/p>\n\n\n\n<p>Let us consider the philosophers as <strong>P0, P1, P2, P3, P4,<\/strong> and chopsticks as <strong>C0, C1, C2, C3, C4<\/strong><\/p>\n\n\n\n<p><strong>Algorithm<\/strong><\/p>\n\n\n\n<ul><li><strong>P0<\/strong> enters the process and since the system contains no other processes at the current moment, it acquires the resource <strong>C0<\/strong> and <strong>C1.<\/strong><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img  loading=\"lazy\"  width=\"1024\"  height=\"647\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"\"  class=\"wp-image-3087 pk-lazyload\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 1024px) 100vw, 1024px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-2-4.png\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-2-4.png 1024w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-2-4-300x190.png 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-2-4-768x485.png 768w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-2-4-380x240.png 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-2-4-550x348.png 550w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-2-4-800x505.png 800w\" ><\/figure>\n\n\n\n<ul><li>Now, process <strong>P1<\/strong> enters the system. Since <strong>P0<\/strong> is already acquiring the resource <strong>C0<\/strong> and <strong>C1<\/strong>, <strong>P1<\/strong> cannot acquire the process <strong>C1. <\/strong>Therefore, <strong>P1 <\/strong>keeps waiting until <strong>C1<\/strong> gets freed, and after that, it can acquire the resources <strong>C1 <\/strong>and <strong>C2.<\/strong><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img  loading=\"lazy\"  width=\"1024\"  height=\"647\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"\"  class=\"wp-image-3088 pk-lazyload\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 1024px) 100vw, 1024px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-1-4.png\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-1-4.png 1024w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-1-4-300x190.png 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-1-4-768x485.png 768w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-1-4-380x240.png 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-1-4-550x348.png 550w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-1-4-800x505.png 800w\" ><\/figure>\n\n\n\n<ul><li>Now, process <strong>P2<\/strong> enters the system. Since resources <strong>C2<\/strong> and <strong>C3 <\/strong>are both free, <strong>P2<\/strong> acquires both.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img  loading=\"lazy\"  width=\"1024\"  height=\"647\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"\"  class=\"wp-image-3089 pk-lazyload\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 1024px) 100vw, 1024px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-3-1.png\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-3-1.png 1024w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-3-1-300x190.png 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-3-1-768x485.png 768w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-3-1-380x240.png 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-3-1-550x348.png 550w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-3-1-800x505.png 800w\" ><\/figure>\n\n\n\n<ul><li>Now, process <strong>P3<\/strong> enters the system. Since <strong>resource C3<\/strong> has been acquired, it keeps waiting.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img  loading=\"lazy\"  width=\"1024\"  height=\"647\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"\"  class=\"wp-image-3090 pk-lazyload\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 1024px) 100vw, 1024px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-6.png\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-6.png 1024w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-6-300x190.png 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-6-768x485.png 768w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-6-380x240.png 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-6-550x348.png 550w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-6-800x505.png 800w\" ><\/figure>\n\n\n\n<ul><li>Now, process <strong>P4<\/strong> enters the system. Since <strong>resource C0<\/strong> has been acquired, it keeps waiting.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img  loading=\"lazy\"  width=\"1024\"  height=\"647\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"\"  class=\"wp-image-3091 pk-lazyload\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 1024px) 100vw, 1024px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-9.png\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-9.png 1024w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-9-300x190.png 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-9-768x485.png 768w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-9-380x240.png 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-9-550x348.png 550w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-9-800x505.png 800w\" ><\/figure>\n\n\n\n<ul><li>Now, let\u2019s consider a situation, that process <strong>P0 <\/strong>completed eating. Hence, resource <strong>C0<\/strong> and <strong>C1<\/strong> gets freed. Therefore, process <strong>P4<\/strong> starts acquires resources <strong>C0<\/strong> and <strong>C4<\/strong>, since both are free.<br><strong>Note:<\/strong> <strong>Process P1<\/strong> cannot start executing, since <strong>C2<\/strong> isn\u2019t available.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img  loading=\"lazy\"  width=\"1024\"  height=\"647\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"\"  class=\"wp-image-3092 pk-lazyload\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 1024px) 100vw, 1024px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-10.png\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-10.png 1024w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-10-300x190.png 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-10-768x485.png 768w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-10-380x240.png 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-10-550x348.png 550w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-10-800x505.png 800w\" ><\/figure>\n\n\n\n<ul><li>Now, if <strong>P2<\/strong> completes executing, resource <strong>C2<\/strong> and <strong>C3<\/strong> becomes free, hence process <strong>P1<\/strong> starts executing.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img  loading=\"lazy\"  width=\"1024\"  height=\"647\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"\"  class=\"wp-image-3093 pk-lazyload\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 1024px) 100vw, 1024px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-5.png\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-5.png 1024w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-5-300x190.png 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-5-768x485.png 768w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-5-380x240.png 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-5-550x348.png 550w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/10\/Image-5-800x505.png 800w\" ><\/figure>\n\n\n\n<ul><li>Now, if <strong>P4<\/strong> completes executing, resource <strong>C0<\/strong> and <strong>C4<\/strong> becomes free, hence process <strong>P3<\/strong> starts executing.<\/li><\/ul>\n\n\n\n<p><br>Hence, by this design, every philosopher does not need to wait for an infinite amount of time, hence avoiding <strong>circular wait<\/strong> and no <strong>deadlock<\/strong>.<\/p>\n\n\n\n<h3 id=\"c-implementation\">C++ Implementation<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class DiningPhilosophers {\nprivate:\n    \/\/ one mutex per fork\n    std::array&lt;std::mutex, 5> mutexes;\npublic:\n    DiningPhilosophers() {\n        \n    }\n \n    void wantsToEat(int philosopher,\n                    function&lt;void()> pickLeftFork,\n                    function&lt;void()> pickRightFork,\n                    function&lt;void()> eat,\n                    function&lt;void()> putLeftFork,\n                    function&lt;void()> putRightFork) {\n        \n \n        int left = philosopher;\n        int right = (philosopher + 1) % 5;\n        \n        \/\/ ordered lock to avoid deadlock: fork with min number goes first\n        std::lock_guard&lt;std::mutex> first(mutexes[std::min(left, right)]);\n        std::lock_guard&lt;std::mutex> second(mutexes[std::max(left, right)]);\n        \n        \/\/ do the work - fork order does not actualy matter here\n        pickLeftFork(); pickRightFork(); eat(); putRightFork(); putLeftFork();\n    }\n};<\/pre>\n\n\n\n<h3 id=\"java-implementation\">Java Implementation<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class DiningPhilosophers {\n \n    private Semaphore[] leftForks, rightForks;\n    \n    public DiningPhilosophers() {\n        leftForks = new Semaphore[5];\n        rightForks = new Semaphore[5];\n        \n        for (int i = 0; i &lt; 5; i++) {\n            leftForks[i] = new Semaphore(1, true);\n            rightForks[(i + 1) % 5] = leftForks[i]; \/\/ left fork of a philosopher is the right fork of the next philosopher, hence use the \"same\" semaphore (reference of the semaphore)\n        }\n        \n        \n    }\n \n    \/\/ call the run() method of any runnable to execute its code\n    public void wantsToEat(int philosopher,\n                           Runnable pickLeftFork,\n                           Runnable pickRightFork,\n                           Runnable eat,\n                           Runnable putLeftFork,\n                           Runnable putRightFork) throws InterruptedException {\n        \n        leftForks[philosopher].acquire();\n        pickLeftFork.run();\n        rightForks[philosopher].acquire();\n        pickRightFork.run();\n        eat.run();\n        putLeftFork.run();\n        leftForks[philosopher].release();\n        putRightFork.run();\n        rightForks[philosopher].release();\n        \n    }\n}<\/pre>\n\n\n\n<h3 id=\"python-implementation\">Python Implementation<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import threading\n \nclass DiningPhilosophers:\n    def __init__(self):\n        # List of semaphores, one for each fork\n        self.forks = [threading.Semaphore(1) for _ in range(5)]\n \n    # call the functions directly to execute, for example, eat()\n    def wantsToEat(self,\n                   philosopher: int,\n                   pickLeftFork: 'Callable[[], None]',\n                   pickRightFork: 'Callable[[], None]',\n                   eat: 'Callable[[], None]',\n                   putLeftFork: 'Callable[[], None]',\n                   putRightFork: 'Callable[[], None]') -> None:\n        # Take the right fork, then left fork\n        self.forks[philosopher].acquire()\n        self.forks[(philosopher + 1) % 5].acquire()\n \n        # Eat!\n        pickRightFork()\n        pickLeftFork()\n        eat()\n        putRightFork()\n        putLeftFork()\n \n        # Release right fork, then left fork\n        self.forks[philosopher].release()\n        self.forks[(philosopher + 1) % 5].release()<\/pre>\n\n\n\n<p><strong>Time Complexity:<\/strong> O(N), where N is the number of processes or philosophers.<\/p>\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=\"frequently-asked-questions\">Frequently Asked Questions<\/h2>\n\n\n\n<p><strong>What are the necessary conditions for deadlock?<\/strong><br>There are four necessary conditions for deadlock-<\/p>\n\n\n\n<ul><li>Mutual exclusion<\/li><li>Hold and Wait<\/li><li>No preemption<\/li><li>Circular Wait<\/li><\/ul>\n\n\n\n<p><strong>Does the above solution always guarantee no deadlock for the Dining Philosopher problem?<\/strong><br>Dining the philosopher problem is a classic problem, The above approach is a solution that holds true for most of the situations, but there can still arise some situations when the system can get into a deadlock.<\/p>\n","protected":false},"excerpt":{"rendered":"Problem Statement The Dining Philosophers Problem is a classic resource-sharing synchronization problem. It is particularly used for situations,&hellip;\n","protected":false},"author":5,"featured_media":3136,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_daextam_enable_autolinks":"","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":[478],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/2908"}],"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=2908"}],"version-history":[{"count":3,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/2908\/revisions"}],"predecessor-version":[{"id":3094,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/2908\/revisions\/3094"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/media\/3136"}],"wp:attachment":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/media?parent=2908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/categories?post=2908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/tags?post=2908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}