{"id":3452,"date":"2021-04-21T03:45:10","date_gmt":"2021-04-21T03:45:10","guid":{"rendered":"https:\/\/cloudxlab.com\/blog\/?p=3452"},"modified":"2021-04-23T07:29:02","modified_gmt":"2021-04-23T07:29:02","slug":"race-condition-and-deadlock","status":"publish","type":"post","link":"https:\/\/cloudxlab.com\/blog\/race-condition-and-deadlock\/","title":{"rendered":"Race Condition and Deadlock"},"content":{"rendered":"\n<p>A good system needs to make sure that race condition and deadlock can&#8217;t occur. In this post, let us learn about Race Condition and Deadlock.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2>What is a Race Condition?<\/h2>\n\n\n\n<p>When two processes are competing with each other causing data corruption.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img width=\"1037\" height=\"551\" src=\"https:\/\/blog.cloudxlab.com\/wp-content\/uploads\/2021\/04\/Screenshot-163-1.png\" alt=\"race condition\" class=\"wp-image-3454\"\/><figcaption>Race Condition<\/figcaption><\/figure><\/div>\n\n\n\n<p>As shown in the diagram, two persons are trying to deposit 1 dollar online into the same bank account. The initial amount is 17 dollar. Both the persons would be able to see $17 initially. Each of them tries to deposit $1, and the final amount is expected to be $19. But due to race conditions, the final amount in the bank is $18 instead of $19. This is also known as dirty read.<\/p>\n\n\n\n<p>For example, if two processes\/threads are trying to execute the following conditions simultaneously, they cause data corruption:<\/p>\n\n\n\n<p>Thread 1:<br><code>total = num1 + num2<\/code><\/p>\n\n\n\n<p>Thread 2:<br><code>total = num1 - num2<\/code><\/p>\n\n\n\n<p>It is very common for the race conditions to go unnoticed during testing even after multiple tests and code reviews. But in production, there would be a lot of processes and threads working parallelly and compete for the same resources, and this problem would occur. <\/p>\n\n\n\n<h2>What is a Dead Lock?<\/h2>\n\n\n\n<p>When two processes are waiting for each other directly or indirectly, it is called deadlock.<\/p>\n\n\n\n<p>This usually occurs when two processes are waiting for shared resources acquired by others. For example, If thread T1 acquired resource R1 and it also needs resource R2 for it to accomplish its task. But the resource R2 is acquired by thread T2 which is waiting for resource R1(which is acquired by T1).. Neither of them will be able to accomplish its task, as they keep waiting for the other resources they need.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img src=\"https:\/\/blog.cloudxlab.com\/wp-content\/uploads\/2021\/04\/Screenshot-164.png\" alt=\"Deadlock\" class=\"wp-image-3455\" width=\"464\" height=\"280\"\/><figcaption>Deadlock<\/figcaption><\/figure>\n\n\n\n<p>As you can see in the second diagram, process 1 is waiting for process 2 and process 2 is waiting for process 3 to finish and process 3 is waiting for process 1 to finish. All these three processes would keep waiting and will never end. This is called deadlock.<\/p>\n\n\n\n<p>Want to test your knowledge with <a href=\"https:\/\/cloudxlab.com\/assessment\/playlist-intro\/780\/zookeeper-race-condition-and-deadlock?course_id=119&amp;playlist_id=780\" target=\"_blank\" rel=\"noreferrer noopener\">MCQs<\/a>? Feel free to visit here. Know more about <a href=\"https:\/\/cloudxlab.com\/home\" target=\"_blank\" rel=\"noreferrer noopener\">CloudxLab Courses<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A good system needs to make sure that race condition and deadlock can&#8217;t occur. In this post, let us learn about Race Condition and Deadlock.<\/p>\n","protected":false},"author":29,"featured_media":3454,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[24,15,1],"tags":[73,112,111,113],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Race Condition and Deadlock | CloudxLab Blog<\/title>\n<meta name=\"description\" content=\"Race condition is when two processes are competing with each other causing data corruption. Deadlock is when two processes are waiting for each other directly or indirectly.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudxlab.com\/blog\/race-condition-and-deadlock\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Race Condition and Deadlock | CloudxLab Blog\" \/>\n<meta property=\"og:description\" content=\"Race condition is when two processes are competing with each other causing data corruption. Deadlock is when two processes are waiting for each other directly or indirectly.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudxlab.com\/blog\/race-condition-and-deadlock\/\" \/>\n<meta property=\"og:site_name\" content=\"CloudxLab Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cloudxlab\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-21T03:45:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-23T07:29:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.cloudxlab.com\/wp-content\/uploads\/2021\/04\/Screenshot-163-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1037\" \/>\n\t<meta property=\"og:image:height\" content=\"551\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CloudxLab\" \/>\n<meta name=\"twitter:site\" content=\"@CloudxLab\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"2 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/#website\",\"url\":\"https:\/\/cloudxlab.com\/blog\/\",\"name\":\"CloudxLab Blog\",\"description\":\"Learn AI, Machine Learning, Deep Learning, Devops &amp; Big Data\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/cloudxlab.com\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/race-condition-and-deadlock\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2021\/04\/Screenshot-163-1.png\",\"contentUrl\":\"https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2021\/04\/Screenshot-163-1.png\",\"width\":1037,\"height\":551,\"caption\":\"race condition\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/race-condition-and-deadlock\/#webpage\",\"url\":\"https:\/\/cloudxlab.com\/blog\/race-condition-and-deadlock\/\",\"name\":\"Race Condition and Deadlock | CloudxLab Blog\",\"isPartOf\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/race-condition-and-deadlock\/#primaryimage\"},\"datePublished\":\"2021-04-21T03:45:10+00:00\",\"dateModified\":\"2021-04-23T07:29:02+00:00\",\"author\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/#\/schema\/person\/d3d0a11dfd64a63deaaa52e09d52049e\"},\"description\":\"Race condition is when two processes are competing with each other causing data corruption. Deadlock is when two processes are waiting for each other directly or indirectly.\",\"breadcrumb\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/race-condition-and-deadlock\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudxlab.com\/blog\/race-condition-and-deadlock\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/race-condition-and-deadlock\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/\",\"url\":\"https:\/\/cloudxlab.com\/blog\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/race-condition-and-deadlock\/#webpage\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/#\/schema\/person\/d3d0a11dfd64a63deaaa52e09d52049e\",\"name\":\"Vagdevi K\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/15df8c96b21e806c59505fe147d6fa92?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/15df8c96b21e806c59505fe147d6fa92?s=96&d=mm&r=g\",\"caption\":\"Vagdevi K\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts\/3452"}],"collection":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/comments?post=3452"}],"version-history":[{"count":7,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts\/3452\/revisions"}],"predecessor-version":[{"id":3470,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts\/3452\/revisions\/3470"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/media\/3454"}],"wp:attachment":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/media?parent=3452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/categories?post=3452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/tags?post=3452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}