{"id":11393,"date":"2022-02-21T02:11:10","date_gmt":"2022-02-21T02:11:10","guid":{"rendered":"https:\/\/www.softwareeverydayblog.com\/?p=11393"},"modified":"2022-02-21T02:11:27","modified_gmt":"2022-02-21T02:11:27","slug":"programming-problem-the-dining-philosophers","status":"publish","type":"post","link":"https:\/\/www.softwareeverydayblog.com\/?p=11393","title":{"rendered":"[Programming Problem] The Dining Philosophers"},"content":{"rendered":"<p>[<a href=\"https:\/\/leetcode.com\/problems\/the-dining-philosophers\/\" rel=\"noopener\" target=\"_blank\">Problem Link<\/a>]<\/p>\n<ul>\n<li>Solution here is to add some `ordering` on the resources (i.e. forks) requested by the processes (i.e. the philosophers).<\/li>\n<li>Identify the resource id&#8217;s (or fork id&#8217;s) that a philosopher is surround with <\/li>\n<li>We make sure the process requests the `lower` id fork before the `higher` id fork to avoid deadlocks<\/li>\n<li>Identify which fork (left or right) is the lower id fork and request in that order (i.e. first left then right  <or>  first right then left)<\/li>\n<li>Make sure mutex&#8217;s are held until forks are released<\/li>\n<\/ul>\n<pre lang=\"java\">\r\nclass DiningPhilosophers {\r\n\r\n    private static ReentrantLock[] mutex = new ReentrantLock[] {\r\n        new ReentrantLock(),\r\n        new ReentrantLock(),\r\n        new ReentrantLock(),\r\n        new ReentrantLock(),\r\n        new ReentrantLock(),\r\n    };\r\n\r\n    public DiningPhilosophers() {\r\n        \r\n    }\r\n\r\n    \/\/ call the run() method of any runnable to execute its code\r\n    public void wantsToEat(int philosopher,\r\n                           Runnable pickLeftFork,\r\n                           Runnable pickRightFork,\r\n                           Runnable eat,\r\n                           Runnable putLeftFork,\r\n                           Runnable putRightFork) throws InterruptedException {\r\n            \r\n        int left = philosopher;\r\n        int right = (philosopher-1) == -1 ? 4 : philosopher-1;\r\n                \r\n        if (Math.min(left, right) == left) {\r\n\r\n            DiningPhilosophers.mutex[left].lock();\r\n            DiningPhilosophers.mutex[right].lock();\r\n            pickLeftFork.run();\r\n            pickRightFork.run();\r\n            \r\n        } else {\r\n            \r\n            DiningPhilosophers.mutex[right].lock();\r\n            DiningPhilosophers.mutex[left].lock();\r\n            pickRightFork.run();\r\n            pickLeftFork.run();\r\n            \r\n        }\r\n        \r\n        \r\n        eat.run();\r\n        \r\n        putLeftFork.run();\r\n        putRightFork.run();\r\n        DiningPhilosophers.mutex[left].unlock();\r\n        DiningPhilosophers.mutex[right].unlock();\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>[Problem Link] Solution here is to add some `ordering` on the resources (i.e. forks) requested by the processes (i.e. the philosophers). Identify the resource id&#8217;s (or fork id&#8217;s) that a philosopher is surround with We make sure the process requests the `lower` id fork before the `higher` id fork to avoid deadlocks Identify which fork [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11393","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11393"}],"version-history":[{"count":3,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11393\/revisions"}],"predecessor-version":[{"id":11395,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11393\/revisions\/11395"}],"wp:attachment":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}