{"id":346,"date":"2025-11-13T03:33:01","date_gmt":"2025-11-13T03:33:01","guid":{"rendered":"https:\/\/mitalgoswami.in\/?p=346"},"modified":"2025-11-13T03:33:27","modified_gmt":"2025-11-13T03:33:27","slug":"deadlock","status":"publish","type":"post","link":"https:\/\/mitalgoswami.in\/?p=346","title":{"rendered":"Deadlock"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>Definition:<\/strong><\/h2>\n\n\n\n<p>A <strong>deadlock<\/strong> is a situation in which <strong>two or more processes are waiting for resources<\/strong> that are <strong>held by each other<\/strong>, and <strong>none of them can proceed<\/strong>.<br>In short, it is a <strong>state of permanent waiting<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Process P1<\/strong> holds <strong>Resource R1<\/strong> and waits for <strong>R2<\/strong>.<\/li>\n\n\n\n<li><strong>Process P2<\/strong> holds <strong>Resource R2<\/strong> and waits for <strong>R1<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p>Both are <strong>waiting forever<\/strong> \u2014 this is a <strong>deadlock<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Four Necessary Conditions for Deadlock (Coffman Conditions):<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Mutual Exclusion:<\/strong><br>Each resource can be used by only one process at a time.<\/li>\n\n\n\n<li><strong>Hold and Wait:<\/strong><br>A process is holding at least one resource and waiting for others.<\/li>\n\n\n\n<li><strong>No Preemption:<\/strong><br>A resource cannot be forcibly taken from a process.<\/li>\n\n\n\n<li><strong>Circular Wait:<\/strong><br>A circular chain of processes exists, each waiting for a resource held by the next process.<\/li>\n<\/ol>\n\n\n\n<p>Deadlock occurs <strong>only if all four conditions<\/strong> hold simultaneously.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"> <strong>Deadlock Handling Methods<\/strong><\/h2>\n\n\n\n<p>There are <strong>three main strategies<\/strong> to deal with deadlocks:<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. <strong>Deadlock Prevention<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Definition:<\/strong><\/h3>\n\n\n\n<p>Deadlock prevention means <strong>designing the system in such a way<\/strong> that <strong>at least one of the four necessary conditions cannot occur<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How it works:<\/strong><\/h3>\n\n\n\n<p>We <strong>prevent<\/strong> deadlock by <strong>breaking<\/strong> one or more Coffman conditions:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Condition<\/th><th>Prevention Method<\/th><\/tr><\/thead><tbody><tr><td><strong>Mutual Exclusion<\/strong><\/td><td>Make resources sharable (where possible)<\/td><\/tr><tr><td><strong>Hold and Wait<\/strong><\/td><td>Process must request <strong>all resources at once<\/strong> before execution<\/td><\/tr><tr><td><strong>No Preemption<\/strong><\/td><td>Allow preemption \u2013 take resource from a process if needed<\/td><\/tr><tr><td><strong>Circular Wait<\/strong><\/td><td>Impose a strict <strong>ordering of resources<\/strong> (e.g., number them and request in increasing order)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 <strong>Advantage:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Prevents deadlock <strong>completely<\/strong><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u274c <strong>Disadvantage:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>May cause <strong>low resource utilization<\/strong> and <strong>process starvation<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. <strong>Deadlock Avoidance<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Definition:<\/strong><\/h3>\n\n\n\n<p>Deadlock avoidance means the system <strong>dynamically examines resource allocation<\/strong> and decides <strong>whether to grant or wait<\/strong> for a request so that <strong>deadlock never occurs<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Technique:<\/strong><\/h3>\n\n\n\n<p>The most common method is the <strong>Banker\u2019s Algorithm<\/strong> (used for multiple instances of resources).<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The system checks whether allocating a resource will <strong>keep it in a \u201csafe state\u201d<\/strong>.<\/li>\n\n\n\n<li>A <strong>safe state<\/strong> means there is <strong>at least one sequence<\/strong> of processes where each can finish execution.<\/li>\n<\/ul>\n\n\n\n<p>If the system stays <strong>safe<\/strong>, it grants the resource;<br>if not, the process must <strong>wait<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 <strong>Advantage:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Ensures system will <strong>never enter a deadlock<\/strong>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u274c <strong>Disadvantage:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Requires <strong>prior knowledge<\/strong> of maximum resource needs.<\/li>\n\n\n\n<li>Adds <strong>runtime overhead<\/strong> due to constant checking.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. <strong>Deadlock Detection and Recovery<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Definition:<\/strong><\/h3>\n\n\n\n<p>In this method, the system <strong>does not prevent or avoid<\/strong> deadlocks.<br>Instead, it <strong>allows<\/strong> them to occur and then <strong>detects<\/strong> and <strong>recovers<\/strong> from them.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How it works:<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Detection:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The system uses algorithms to <strong>periodically check<\/strong> for circular waits among processes.<\/li>\n\n\n\n<li>A <strong>wait-for graph<\/strong> is often used (especially in single-instance systems).<\/li>\n\n\n\n<li>If a <strong>cycle<\/strong> exists in the graph \u2192 <strong>deadlock detected<\/strong>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Recovery:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Once detected, the system <strong>recovers<\/strong> using one of these methods:\n<ul class=\"wp-block-list\">\n<li><strong>Terminate processes<\/strong> (one or more) to break the cycle.<\/li>\n\n\n\n<li><strong>Preempt resources<\/strong> and give them to other processes.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 <strong>Advantage:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Efficient use<\/strong> of resources (no restrictions upfront)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u274c <strong>Disadvantage:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overhead<\/strong> in detection and recovery<\/li>\n\n\n\n<li>May cause <strong>data loss<\/strong> or <strong>process restart<\/strong><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Definition: A deadlock is a situation in which two or more processes are waiting for resources that are held by each other, and none of them can proceed.In short, it is a state of permanent waiting. Example: Both are waiting forever \u2014 this is a deadlock. Four Necessary Conditions for Deadlock (Coffman Conditions): Deadlock occurs [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-346","post","type-post","status-publish","format-standard","hentry","category-unix"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/346","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=346"}],"version-history":[{"count":1,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/346\/revisions"}],"predecessor-version":[{"id":347,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/346\/revisions\/347"}],"wp:attachment":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}