{"id":2358,"date":"2021-02-11T14:10:47","date_gmt":"2021-02-11T08:40:47","guid":{"rendered":"https:\/\/binaryterms.com\/?p=2358"},"modified":"2021-02-11T17:26:59","modified_gmt":"2021-02-11T11:56:59","slug":"synchronization-hardware-in-os","status":"publish","type":"post","link":"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html","title":{"rendered":"Synchronization Hardware"},"content":{"rendered":"<p><strong>Synchronization hardware<\/strong> is a hardware-based solution to resolve the critical section problem. In our earlier content of the <a href=\"https:\/\/binaryterms.com\/critical-section-problem.html\" target=\"_blank\" rel=\"noopener noreferrer\">critical section<\/a>, we have discussed how the multiple processes sharing common resources must be synchronized to avoid inconsistent results.<\/p>\n<p>Well, we can synchronize the processes sharing a common variable in two ways. First is the <strong>software-based solution<\/strong> which includes Peterson\u2019s solution and the second is the <strong>hardware-based solution<\/strong> which is also referred to as synchronization hardware or hardware synchronization.<\/p>\n<p>In this context, we will discuss the two hardware-based solutions to the critical section problem. We will also discuss how these hardware solutions resolve the critical section problem.<\/p>\n<h2>Synchronization Hardware<\/h2>\n<p>Synchronization hardware i.e. hardware-based solution for the critical section problem which introduces the <strong>hardware instructions<\/strong> that can be used to resolve the critical section problem effectively. Hardware solutions are often easier and also improves the efficiency of the system.<\/p>\n<p>Consider, a system with a single processor where just by preventing the occurrence interrupts while a shared variable is being modified can assure that only the current sequence of the instructions will be executed without any preemption.<\/p>\n<p>This assures you that no unfair modifications will be made to the shared variable. This method is often used by the systems with non-preemptive kernels.<\/p>\n<p>Now, consider the system with multiple processors, where disabling interrupts would require a message to be sent to all the processors which would cause unnecessary delay to the processes to enter into their critical section. This also decreases the efficiency of the system.<\/p>\n<p>The hardware-based solution to critical section problem is based on a simple tool i.e. <strong>lock.<\/strong> The solution implies that before entering into the critical section the process must acquire a lock and must release the lock when it exits its critical section. Using of lock also prevent the <em>race condition<\/em>.<\/p>\n<p>The hardware synchronization provides two kinds of hardware instructions that are <strong>TestAndSet<\/strong> and <strong>Swap.<\/strong> We will discuss each of the instruction briefly. But before jumping on to the instructions let us discuss what conditions they must verify to resolve the critical section problem.<\/p>\n<ol>\n<li><strong>Mutual Exclusion:<\/strong> The hardware instruction must verify that at a point in time only one process can be in its critical section.<\/li>\n<li><strong>Bounded Waiting:<\/strong> The processes interested to execute their critical section must not wait for long to enter their critical section.<\/li>\n<li><strong>Progress:<\/strong> The process not interested in entering its critical section must not block other processes from entering into their critical section.<\/li>\n<\/ol>\n<h3>TestAndSet Hardware Instruction<\/h3>\n<p>The TestAndSet() hardware instruction is <strong>atomic<\/strong> instruction. Atomic means both the test operation and set operation are executed in one machine cycle at once. If the two different processes are executing TestAndSet() simultaneously each on different CPU. Still, they will be executed sequentially in some random order.<\/p>\n<p>The TestAndSet() instruction can be defined as in the code below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">do {\r\n\r\nwhile (TestAndSet(&amp;lock));\r\n\r\n\/\/ critical section\r\n\r\nlock = FALSE;\r\n\r\n\/\/ remainder section\r\n\r\n} while (TRUE);<\/pre>\n<p>Now, as a solution to the critical section, how this TestAndSet() instruction be implemented to achieve mutual exclusion, bounded wait and progress. Let us do it one by one, first, we will try to achieve mutual exclusion using TestAndSet() instruction and for that, you have to globally declare a Boolean variable <strong>lock<\/strong> and initialize it to <em>false.<\/em><\/p>\n<p>Consider we have three processes P<sub>0<\/sub>, and P<sub>1<\/sub>\u00a0are interested to enter their critical section. So the structure for achieving mutual exclusion is as follow.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">do {\r\n\r\nwhile (TestAndSet(&amp;lock));\r\n\r\n\/\/ critical section\r\n\r\nlock = FALSE;\r\n\r\n\/\/ remainder section\r\n\r\n} while (TRUE);<\/pre>\n<p>Let\u2019s say process P<sub>0<\/sub> wants to enter the critical section it executes the code above which let while loop invokes TestAndSet() instruction.<\/p>\n<p>Using the TestAndSet() instruction the P<sub>0 <\/sub>modifies the lock value to <em>true<\/em> to acquire the lock and enters the critical section.<\/p>\n<p>Now, when P<sub>0<\/sub> is already in its critical section process P<sub>1 <\/sub>also wants to enter in its critical section. So it will execute the do-while loop and invoke TestAndSet() instruction only to see that the lock is already set to <em>true<\/em> which means some process is in the critical section which will make P<sub>1<\/sub> repeat while loop unless P<sub>0 <\/sub>turns the lock to <em>false.<\/em><\/p>\n<p>Once the process P<sub>0<\/sub> complete executing its critical section its will turn the lock variable to <em>false.<\/em> Then P<sub>1<\/sub> can modify the lock variable to <em>true<\/em> using TestAndSet() instruction and enter its critical section.<\/p>\n<p>This is how you can achieve mutual exclusion with the do-while structure above i.e. it let only one process to execute its critical section at a time. But this structure does not assure bounded wait as it may happen that process P<sub>0<\/sub> may keep on executing its critical section again and again and P<sub>1<\/sub> has to keep on waiting.<\/p>\n<p>So we have another structure that will ensure the bounded wait condition. The structure below has a global Boolean array waiting[i] and a global Boolean variable lock where both are initialized to <em>false.<\/em> Also, a local variable <strong>key<\/strong> associated with each process that is interested to execute their critical section.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">{\r\n\r\ndo\r\n\r\nwaiting[i] = TRUE;\r\n\r\nkey = TRUE;\r\n\r\nwhile (waiting[i] &amp;&amp; key)\r\n\r\nkey = TestAndSet(&amp;lock);\r\n\r\nwaiting[i] = FALSE;\r\n\r\n\/\/ critical section\r\n\r\nj = (i + 1) % n;\r\n\r\nwhile ((j != i) &amp;&amp; !waiting[j])\r\n\r\nj = (j + 1) % n;\r\n\r\nif (j == i)\r\n\r\nlock = FALSE;\r\n\r\nelse\r\n\r\nwaiting[j] = FALSE;\r\n\r\n\/\/ remainder section\r\n\r\n}\r\nwhile\u00a0(TRUE);<\/pre>\n<p>Now first we will check how the structure above satisfies mutual exclusion condition. Any process P<sub>i<\/sub> is allowed to get into its critical section only if the first while loop turns to be <em>false<\/em> i.e. either waiting[i] <em>==false<\/em> or <em>key==false.<\/em><\/p>\n<p>The very first process executing this code will get the chance to set the <em>key==false<\/em> and will enter the critical section and refrains other processes from entering into their critical section. This satisfies mutual exclusion.<\/p>\n<p>Now about bounded wait and progress, the process executing the critical section will either set <em>lock==false<\/em> or it will set <em>waiting[j]==false.<\/em> Here, both the settings will let processes waiting to enter their critical section to proceed.<\/p>\n<p>The process exiting its critical section scans the waiting[j] array, where j = (i+ 1, i + 2, &#8230;, n-1, 0, &#8230;, i-1). Thus letting the processes waiting to enter its critical section in n-1 turns.<\/p>\n<h3>Swap Hardware Instruction<\/h3>\n<p>Like TestAndSet() instruction the swap() hardware instruction is also an atomic instruction. With a difference that it operates on two variables provided in its parameter. The structure of swap() instruction is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">do {\r\n\r\nkey = TRUE;\r\n\r\nwhile (key == TRUE)\r\n\r\nSwap(&amp;lock, &amp;key);\r\n\r\n\/\/ critical section\r\n\r\nlock = FALSE;\r\n\r\n\/\/ remainder section\r\n\r\n} while (TRUE);<\/pre>\n<p>To achieve mutual exclusion using swap() instruction, the structure we use is a follow:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">do {\r\n\r\nkey = TRUE;\r\n\r\nwhile (key == TRUE)\r\n\r\nSwap(&amp;lock, &amp;key);\r\n\r\n\/\/ critical section\r\n\r\nlock = FALSE;\r\n\r\n\/\/ remainder section\r\n\r\n} while (TRUE);<\/pre>\n<p>The structure above operates on one global shared Boolean variable <strong>lock<\/strong> and another local Boolean variable <strong>key.<\/strong> Both of which are initially set to <em>false.<\/em> The process P<sub>0<\/sub> interested in executing its critical section execute code above and set lock as <em>true<\/em> and enter its critical section. Thus refrain other processes from executing their critical section satisfying mutual exclusion.<\/p>\n<h3>Advantages and Disadvantages of Synchronization Hardware<\/h3>\n<p><strong>Advantages of Hardware Instruction<\/strong><\/p>\n<ul>\n<li>Hardware instructions are easy to implement and improves the efficiency of the system.<\/li>\n<li>Supports any number of processes may it be on the single or multiple processor system.<\/li>\n<li>With hardware instructions, you can implement multiple critical sections each defined with a unique variable.<\/li>\n<\/ul>\n<p><strong>Disadvantages of Hardware Instruction<\/strong><\/p>\n<ul>\n<li>Processes waiting for entering their critical section consumes a lot of processors time which increases busy waiting.<\/li>\n<li>As the selection of processes to enter their critical section is arbitrary. It may happen that some processes are waiting for the indefinite time which leads to process starvation.<\/li>\n<li>Deadlock is also possible.<\/li>\n<\/ul>\n<p>This is how we can use a hardware solution to overcome the critical section problem. But overviewing the drawback of hardware solution there we have to opt for some other mechanism i.e. semaphore.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Synchronization hardware is a hardware-based solution to resolve the critical section problem. In our earlier content of the critical section, we have discussed how the multiple processes sharing common resources must be synchronized to avoid inconsistent results. Well, we can synchronize the processes sharing a common variable in two ways. First is the software-based solution [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[2],"tags":[],"class_list":{"0":"post-2358","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-operating-system","7":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What is Synchronization Hardware? TestAndSet &amp; Swap - Binary Terms<\/title>\n<meta name=\"description\" content=\"Synchronization hardware is a hardware based solution to the critical section problem which make use of the harware instructions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is Synchronization Hardware? TestAndSet &amp; Swap - Binary Terms\" \/>\n<meta property=\"og:description\" content=\"Synchronization hardware is a hardware based solution to the critical section problem which make use of the harware instructions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html\" \/>\n<meta property=\"og:site_name\" content=\"Binary Terms\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-11T08:40:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-02-11T11:56:59+00:00\" \/>\n<meta name=\"author\" content=\"Neha T\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Neha T\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html#article\",\"isPartOf\":{\"@id\":\"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html\"},\"author\":{\"name\":\"Neha T\",\"@id\":\"https:\/\/binaryterms.com\/#\/schema\/person\/e495f1d57f5c0a4c521cc3dba95661fe\"},\"headline\":\"Synchronization Hardware\",\"datePublished\":\"2021-02-11T08:40:47+00:00\",\"dateModified\":\"2021-02-11T11:56:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html\"},\"wordCount\":1204,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/binaryterms.com\/#organization\"},\"articleSection\":[\"Operating System\"],\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html\",\"url\":\"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html\",\"name\":\"What is Synchronization Hardware? TestAndSet & Swap - Binary Terms\",\"isPartOf\":{\"@id\":\"https:\/\/binaryterms.com\/#website\"},\"datePublished\":\"2021-02-11T08:40:47+00:00\",\"dateModified\":\"2021-02-11T11:56:59+00:00\",\"description\":\"Synchronization hardware is a hardware based solution to the critical section problem which make use of the harware instructions.\",\"breadcrumb\":{\"@id\":\"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/binaryterms.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Synchronization Hardware\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/binaryterms.com\/#website\",\"url\":\"https:\/\/binaryterms.com\/\",\"name\":\"Binary Terms\",\"description\":\"The Computer Science &amp; IT Guide\",\"publisher\":{\"@id\":\"https:\/\/binaryterms.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/binaryterms.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/binaryterms.com\/#organization\",\"name\":\"Binary Terms\",\"url\":\"https:\/\/binaryterms.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/binaryterms.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/binaryterms.com\/wp-content\/uploads\/2020\/05\/binary-terms-logo1.png\",\"contentUrl\":\"https:\/\/binaryterms.com\/wp-content\/uploads\/2020\/05\/binary-terms-logo1.png\",\"width\":400,\"height\":63,\"caption\":\"Binary Terms\"},\"image\":{\"@id\":\"https:\/\/binaryterms.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/binaryterms.com\/#\/schema\/person\/e495f1d57f5c0a4c521cc3dba95661fe\",\"name\":\"Neha T\",\"url\":\"https:\/\/binaryterms.com\/author\/author\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is Synchronization Hardware? TestAndSet & Swap - Binary Terms","description":"Synchronization hardware is a hardware based solution to the critical section problem which make use of the harware instructions.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html","og_locale":"en_GB","og_type":"article","og_title":"What is Synchronization Hardware? TestAndSet & Swap - Binary Terms","og_description":"Synchronization hardware is a hardware based solution to the critical section problem which make use of the harware instructions.","og_url":"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html","og_site_name":"Binary Terms","article_published_time":"2021-02-11T08:40:47+00:00","article_modified_time":"2021-02-11T11:56:59+00:00","author":"Neha T","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Neha T","Estimated reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html#article","isPartOf":{"@id":"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html"},"author":{"name":"Neha T","@id":"https:\/\/binaryterms.com\/#\/schema\/person\/e495f1d57f5c0a4c521cc3dba95661fe"},"headline":"Synchronization Hardware","datePublished":"2021-02-11T08:40:47+00:00","dateModified":"2021-02-11T11:56:59+00:00","mainEntityOfPage":{"@id":"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html"},"wordCount":1204,"commentCount":1,"publisher":{"@id":"https:\/\/binaryterms.com\/#organization"},"articleSection":["Operating System"],"inLanguage":"en-GB","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/binaryterms.com\/synchronization-hardware-in-os.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html","url":"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html","name":"What is Synchronization Hardware? TestAndSet & Swap - Binary Terms","isPartOf":{"@id":"https:\/\/binaryterms.com\/#website"},"datePublished":"2021-02-11T08:40:47+00:00","dateModified":"2021-02-11T11:56:59+00:00","description":"Synchronization hardware is a hardware based solution to the critical section problem which make use of the harware instructions.","breadcrumb":{"@id":"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/binaryterms.com\/synchronization-hardware-in-os.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/binaryterms.com\/synchronization-hardware-in-os.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/binaryterms.com\/"},{"@type":"ListItem","position":2,"name":"Synchronization Hardware"}]},{"@type":"WebSite","@id":"https:\/\/binaryterms.com\/#website","url":"https:\/\/binaryterms.com\/","name":"Binary Terms","description":"The Computer Science &amp; IT Guide","publisher":{"@id":"https:\/\/binaryterms.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/binaryterms.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/binaryterms.com\/#organization","name":"Binary Terms","url":"https:\/\/binaryterms.com\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/binaryterms.com\/#\/schema\/logo\/image\/","url":"https:\/\/binaryterms.com\/wp-content\/uploads\/2020\/05\/binary-terms-logo1.png","contentUrl":"https:\/\/binaryterms.com\/wp-content\/uploads\/2020\/05\/binary-terms-logo1.png","width":400,"height":63,"caption":"Binary Terms"},"image":{"@id":"https:\/\/binaryterms.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/binaryterms.com\/#\/schema\/person\/e495f1d57f5c0a4c521cc3dba95661fe","name":"Neha T","url":"https:\/\/binaryterms.com\/author\/author"}]}},"_links":{"self":[{"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/posts\/2358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/comments?post=2358"}],"version-history":[{"count":9,"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/posts\/2358\/revisions"}],"predecessor-version":[{"id":2367,"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/posts\/2358\/revisions\/2367"}],"wp:attachment":[{"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/media?parent=2358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/categories?post=2358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/binaryterms.com\/wp-json\/wp\/v2\/tags?post=2358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}