{"id":136,"date":"2018-07-19T07:11:43","date_gmt":"2018-07-19T07:11:43","guid":{"rendered":"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=136"},"modified":"2018-08-07T05:30:40","modified_gmt":"2018-08-07T05:30:40","slug":"process-synchronization-solutions-iii","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/chapter\/process-synchronization-solutions-iii\/","title":{"rendered":"Process Synchronization \u2013 Solutions III"},"content":{"raw":"<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>13.1\u00a0 Introduction<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Cooperating processes may share data among themselves. Cooperating processes use common variables and data structures for sharing data. In a process, that portion of code where the process accesses shared data is called the critical section of the process. If there is no proper synchronization between the processes when they access their critical sections of code, then inconsistency of data may result. We saw a few solutions to the critical section problem in the previous modules (Modules 10, 11 and 12). In this module, we continue to learn more solutions to solve the critical section problem when there are multiple (&gt;=2) processes. The Test And Set and Swap are two hardware instructions that can be executed atomically and can provide solution to the critical section problem. We can use these special instructions to solve the critical section problem.<\/span><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n\r\n<strong>13.2\u00a0 Swap instruction<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"padding-left: 30px\">The Swap instruction is defined as follows:<\/p>\r\nvoid Swap(boolean &amp;a, boolean &amp;b)\r\n\r\n<strong>{<\/strong>\r\n\r\n<strong>boolean temp = a; <\/strong>\r\n\r\n<strong>a = b;<\/strong>\r\n\r\n<strong>b = temp;<\/strong>\r\n\r\n<strong>}<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The Swap instruction takes two Boolean variables and swaps the value of the variables. The swapping is done atomically.<\/p>\r\n&nbsp;\r\n\r\n<strong>13.2.1 Solution to the Critical Section Problem Using the Swap Instruction\u00a0<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A solution to the critical section problem using the Swap instruction is explained in this section. This is a multiple-process solution. More than two processes competing to enter their critical sections can use this solution to solve the critical section problem.<\/p>\r\n&nbsp;\r\n\r\nShared data:\r\n\r\n&nbsp;\r\n<p style=\"padding-left: 30px\"><strong>boolean lock; \/* <\/strong>(initialized to <strong>false<\/strong>) *\/<\/p>\r\n&nbsp;\r\n\r\nThe algorithm for process <em>P<\/em><em>i <\/em>is given below:\r\n<p style=\"padding-left: 60px\"><strong><span style=\"text-align: justify;font-size: 1em\">do{<\/span><\/strong><\/p>\r\n<p style=\"padding-left: 90px\"><strong style=\"font-size: 1em;text-align: initial\">key = true;<\/strong><\/p>\r\n<p style=\"padding-left: 90px\"><strong style=\"text-align: initial;font-size: 1em\">w<\/strong><strong style=\"text-align: initial;font-size: 1em\">hile (key == true)<\/strong><\/p>\r\n<p style=\"padding-left: 150px\"><strong style=\"text-align: initial;font-size: 1em\">Swap(lock, key);<\/strong><\/p>\r\n<p style=\"padding-left: 120px\"><span style=\"text-align: initial;font-size: 1em\">critical section<\/span><\/p>\r\n<p style=\"padding-left: 90px\"><strong><span style=\"text-align: initial;font-size: 1em\">lock=false;<\/span><\/strong><\/p>\r\n<p style=\"padding-left: 120px\"><span style=\"font-size: 1em;text-align: initial\">remainder section<\/span><\/p>\r\n<p style=\"padding-left: 60px\"><span style=\"text-align: initial;font-size: 1em\">}\u00a0<\/span><\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\nIn this algorithm, lock is a shared variable and key is a variable local to each process. When process <em>P<\/em>i executes this algorithm, it changes the value of its local variable key to true. Then, as long as key is true, <em>P<\/em>i keeps swapping the values of lock and key.\r\n\r\n&nbsp;\r\n\r\nIf the value of lock is true, even though lock and key are swapped, the value of key remains true. This makes process <em>P<\/em>i to continue to check in the while loop.\r\n\r\n&nbsp;\r\n\r\nIf the value of lock is false, swapping lock and key will change the value of key to false and the value of lock to true. This makes the process\u00a0 <em>P<\/em>i\u00a0 \u00a0to come out of the while loop (while(key==true) becomes while(false)). If <em>P<\/em>i \u00a0comes out of the while loop, it enters its critical section. The value of lock remains true during this time.\r\n\r\n&nbsp;\r\n\r\nWhen <em>P<\/em>i comes out of its critical section, it changes the value of lock to false.\r\n\r\n&nbsp;\r\n\r\n<strong>13.2.2. Mutual Exclusion\u00a0<\/strong>\r\n\r\n&nbsp;\r\n\r\nLet us now see how this algorithm satisfies mutual exclusion.\r\n\r\n&nbsp;\r\n\r\n<strong><em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 P<\/em><\/strong><strong><sub>0<\/sub>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/strong><strong>key(<em>P<\/em><\/strong><sub><strong>0<\/strong><\/sub><strong>)\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0lock\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0key(<em>P<\/em><\/strong><strong>1<\/strong><strong>)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>P<\/em><\/strong><strong>1<\/strong>\r\n<table style=\"height: 336px\" border=\"1\" width=\"622\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 156.063px\"><\/td>\r\n<td style=\"width: 95.0625px\"><\/td>\r\n<td style=\"width: 95.0625px\">false<\/td>\r\n<td style=\"width: 96.0625px\"><\/td>\r\n<td style=\"width: 109.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 156.063px\"><\/td>\r\n<td style=\"width: 95.0625px\">true<\/td>\r\n<td style=\"width: 95.0625px\"><\/td>\r\n<td style=\"width: 96.0625px\"><\/td>\r\n<td style=\"width: 109.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 156.063px\">Swap<\/td>\r\n<td style=\"width: 95.0625px\">false<\/td>\r\n<td style=\"width: 95.0625px\">true<\/td>\r\n<td style=\"width: 96.0625px\"><\/td>\r\n<td style=\"width: 109.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 156.063px\"><\/td>\r\n<td style=\"width: 95.0625px\"><\/td>\r\n<td style=\"width: 95.0625px\"><\/td>\r\n<td style=\"width: 96.0625px\">true<\/td>\r\n<td style=\"width: 109.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 156.063px\">In CS<\/td>\r\n<td style=\"width: 95.0625px\"><\/td>\r\n<td style=\"width: 95.0625px\"><\/td>\r\n<td style=\"width: 96.0625px\"><\/td>\r\n<td style=\"width: 109.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 156.063px\"><\/td>\r\n<td style=\"width: 95.0625px\"><\/td>\r\n<td style=\"width: 95.0625px\"><\/td>\r\n<td style=\"width: 96.0625px\"><\/td>\r\n<td style=\"width: 109.063px\">Swap<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 156.063px\"><\/td>\r\n<td style=\"width: 95.0625px\"><\/td>\r\n<td style=\"width: 95.0625px\"><\/td>\r\n<td style=\"width: 96.0625px\">true<\/td>\r\n<td style=\"width: 109.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 156.063px\"><\/td>\r\n<td style=\"width: 95.0625px\"><\/td>\r\n<td style=\"width: 95.0625px\"><\/td>\r\n<td style=\"width: 96.0625px\"><\/td>\r\n<td style=\"width: 109.063px\">waits<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 156.063px\">Out of CS<\/td>\r\n<td style=\"width: 95.0625px\"><\/td>\r\n<td style=\"width: 95.0625px\"><\/td>\r\n<td style=\"width: 96.0625px\"><\/td>\r\n<td style=\"width: 109.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 156.063px\"><\/td>\r\n<td style=\"width: 95.0625px\"><\/td>\r\n<td style=\"width: 95.0625px\">false<\/td>\r\n<td style=\"width: 96.0625px\"><\/td>\r\n<td style=\"width: 109.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 156.063px\"><\/td>\r\n<td style=\"width: 95.0625px\"><\/td>\r\n<td style=\"width: 95.0625px\">true<\/td>\r\n<td style=\"width: 96.0625px\">false<\/td>\r\n<td style=\"width: 109.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 156.063px\"><\/td>\r\n<td style=\"width: 95.0625px\"><\/td>\r\n<td style=\"width: 95.0625px\"><\/td>\r\n<td style=\"width: 96.0625px\"><\/td>\r\n<td style=\"width: 109.063px\">In CS<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">Let two processes <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">0 and <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1 compete to enter their respective critical sections. Each of the two processes has its own local key variable. Initially, lock is false. The key variables of each of the two processes are true initially. <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">0 wants to enter critical section first. <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">0 executes Swap(key(<\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">0), lock). This will make the value of lock true and the value of key false. As key becomes false, <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">0 comes out of the while loop and enters its critical section. Suppose, <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1 wants to enter its critical section when <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">0 is in its critical section. <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1 sets the value of its key to true. <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1 executes Swap(key(<\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1), lock). Since the value of lock is currently true, the value of key also continues to be true. <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1 continues to execute in the while loop. When <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">0 comes out of its critical section, it changes the value of lock to false. Now <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1, executing in the while loop, swaps lock and its key. <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1\u2019s key becomes false and <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1 comes out of the while loop and enters critical section. Thus, it is seen that <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1 is not allowed to enter its critical section when <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">0 is in its critical section. When one process is in its critical section, the value of lock is true and no other process can enter its critical section. Thus, mutual exclusion is satisfied.<\/span><\/p>\r\n\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n\r\n<strong>13.2.3 Swap \u2013 Bounded Waiting<\/strong>\r\n\r\n&nbsp;\r\n\r\nWe now see if this algorithm satisfies the bounded waiting requirement.\r\n\r\n&nbsp;\r\n\r\n<strong><em>\u00a0 \u00a0 \u00a0 P<\/em><\/strong><strong>0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><strong>key(<em>P<\/em><\/strong><strong>0<\/strong><strong>)\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0lock\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0key(<em>P<\/em><\/strong><strong>1<\/strong><strong>)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>P<\/em><\/strong><strong>1<\/strong>\r\n<table style=\"height: 338px\" border=\"1\" width=\"668\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 166.063px\"><\/td>\r\n<td style=\"width: 88.0625px\"><\/td>\r\n<td style=\"width: 88.0625px\">false<\/td>\r\n<td style=\"width: 84.0625px\"><\/td>\r\n<td style=\"width: 171.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 166.063px\"><\/td>\r\n<td style=\"width: 88.0625px\">true<\/td>\r\n<td style=\"width: 88.0625px\"><\/td>\r\n<td style=\"width: 84.0625px\"><\/td>\r\n<td style=\"width: 171.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 166.063px\">Swap<\/td>\r\n<td style=\"width: 88.0625px\">false<\/td>\r\n<td style=\"width: 88.0625px\">true<\/td>\r\n<td style=\"width: 84.0625px\"><\/td>\r\n<td style=\"width: 171.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 166.063px\"><\/td>\r\n<td style=\"width: 88.0625px\"><\/td>\r\n<td style=\"width: 88.0625px\"><\/td>\r\n<td style=\"width: 84.0625px\">true<\/td>\r\n<td style=\"width: 171.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 166.063px\">In CS<\/td>\r\n<td style=\"width: 88.0625px\"><\/td>\r\n<td style=\"width: 88.0625px\"><\/td>\r\n<td style=\"width: 84.0625px\"><\/td>\r\n<td style=\"width: 171.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 166.063px\"><\/td>\r\n<td style=\"width: 88.0625px\"><\/td>\r\n<td style=\"width: 88.0625px\"><\/td>\r\n<td style=\"width: 84.0625px\"><\/td>\r\n<td style=\"width: 171.063px\">Swap<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 166.063px\"><\/td>\r\n<td style=\"width: 88.0625px\"><\/td>\r\n<td style=\"width: 88.0625px\"><\/td>\r\n<td style=\"width: 84.0625px\">true<\/td>\r\n<td style=\"width: 171.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 166.063px\"><\/td>\r\n<td style=\"width: 88.0625px\"><\/td>\r\n<td style=\"width: 88.0625px\"><\/td>\r\n<td style=\"width: 84.0625px\"><\/td>\r\n<td style=\"width: 171.063px\">Waits<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 166.063px\">Out of CS<\/td>\r\n<td style=\"width: 88.0625px\"><\/td>\r\n<td style=\"width: 88.0625px\"><\/td>\r\n<td style=\"width: 84.0625px\"><\/td>\r\n<td style=\"width: 171.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 166.063px\">Swap<\/td>\r\n<td style=\"width: 88.0625px\">false<\/td>\r\n<td style=\"width: 88.0625px\">true<\/td>\r\n<td style=\"width: 84.0625px\"><\/td>\r\n<td style=\"width: 171.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 166.063px\">In CS<\/td>\r\n<td style=\"width: 88.0625px\"><\/td>\r\n<td style=\"width: 88.0625px\"><\/td>\r\n<td style=\"width: 84.0625px\"><\/td>\r\n<td style=\"width: 171.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 166.063px\"><\/td>\r\n<td style=\"width: 88.0625px\"><\/td>\r\n<td style=\"width: 88.0625px\"><\/td>\r\n<td style=\"width: 84.0625px\"><\/td>\r\n<td style=\"width: 171.063px\">Still waits<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let <em>P<\/em>0 and <em>P<\/em>1 be two processes that are competing to access their respective critical sections. Initially, lock is false. Initially, the key variable of each of the two processes is true. <em>P<\/em>0 wants to enter its critical section first. <em>P<\/em>0 executes Swap(key(<em>P<\/em>0), lock). This makes the value of lock true and the value of key false. As key becomes false, <em>P<\/em>0 comes out of the while loop and enters its critical section. Let <em>P<\/em>1 want to enter its critical section when <em>P<\/em>0 is in its critical section. <em>P<\/em>1 sets the value of its key to true. <em>P<\/em>1 executes Swap(key(<em>P<\/em>1), lock). Since the value of lock is currently true, the value of key also continues to be true. <em>P<\/em>1\u00a0 continues to execute in the while loop. When <em>P<\/em>0 comes out of its critical section, it changes the value of lock to false. Let <em>P<\/em>0 continue to use the CPU without context switch. Say, <em>P<\/em>0 wants to enter its critical section again. Since the value of lock is currently false, <em>P<\/em>0 can enter its critical section again. Process <em>P<\/em>1 continues to wait. Since there was no context switch after <em>P<\/em>0 came out of its critical section and <em>P<\/em>0 changed lock to true, <em>P<\/em>1 could not come out of the while loop. Thus, even though <em>P<\/em>1 had requested to enter its critical section, <em>P<\/em>0 is allowed to enter its critical section again and again. Therefore, the bounded waiting requirement is not satisfied.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Thus, it is seen that this algorithm does not satisfy the bounded waiting requirement, though it satisfies the mutual exclusion requirement.<\/p>\r\n&nbsp;\r\n\r\n<strong>13.3\u00a0 TestandSet - Bounded-Waiting - Mutual Exclusion\u00a0<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe algorithm given below makes use of the TestAndSet instruction and satisfies all the three requirements for a solution to the critical section problem. This is also a solution when there are <em>n <\/em>processes.\r\n\r\n&nbsp;\r\n\r\nThe shared variables used in the algorithm are as follows:\r\n\r\n&nbsp;\r\n<p style=\"padding-left: 180px\"><strong>boolean waiting[n];\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/initially false <\/strong><\/p>\r\n<p style=\"padding-left: 180px\"><strong>boolean lock\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/initially false\u00a0<\/strong><\/p>\r\n&nbsp;\r\n\r\nThe algorithm for process <em>P<\/em>i is given below:\r\n\r\ndo {\r\n<p style=\"padding-left: 30px\"><span style=\"text-align: initial;font-size: 1em\">waiting[i] = TRUE; key = TRUE;<\/span><\/p>\r\n<p style=\"padding-left: 30px\"><span style=\"text-align: initial;font-size: 1em\">while (waiting[i] &amp;&amp; key) \u00a0key = TestAndSet(lock); <\/span><\/p>\r\n<p style=\"padding-left: 30px\"><span style=\"text-align: initial;font-size: 1em\">waiting[i] = FALSE;<\/span><\/p>\r\n<p style=\"padding-left: 60px\"><span style=\"text-align: initial;font-size: 1em\">\/\/ critical section j = (i + 1) % n;<\/span><\/p>\r\n<p style=\"padding-left: 30px\"><span style=\"text-align: initial;font-size: 1em\">while ((j != i) &amp;&amp; !waiting[j])\u00a0\u00a0\u00a0\u00a0 j = (j + 1) % n;<\/span><\/p>\r\n<p style=\"padding-left: 30px\"><span style=\"text-align: initial;font-size: 1em\">if (j == i)<\/span><\/p>\r\n<p style=\"padding-left: 60px\"><span style=\"text-align: initial;font-size: 1em\">lock = FALSE;<\/span><\/p>\r\n<p style=\"padding-left: 30px\"><span style=\"text-align: initial;font-size: 1em\">else<\/span><\/p>\r\n<p style=\"padding-left: 30px\"><span style=\"text-align: initial;font-size: 1em\">waiting[j] = FALSE;<\/span><\/p>\r\n<p style=\"padding-left: 60px\"><span style=\"text-align: initial;font-size: 1em\">\/\/ remainder section<\/span><\/p>\r\n<span style=\"text-align: initial;font-size: 1em\">} while (TRUE);<\/span>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The algorithm given above is for process <em>P<\/em>i. <em>waiting <\/em>is an array with \u2018<em>n<\/em>\u2019 elements, where <em>n <\/em>is the number of processes competing to enter their respective critical sections. <em>lock <\/em>is a Boolean variable. Each line in the algorithm is explained below:<\/p>\r\n&nbsp;\r\n\r\nwaiting[i] = TRUE;\r\n\r\n&nbsp;\r\n\r\nProcess <em>P<\/em>i before checking to enter its critical section sets the value of waiting[i] to true.\r\n\r\n&nbsp;\r\n\r\nkey = TRUE;\r\n\r\n&nbsp;\r\n\r\nkey is a variable local to process <em>P<\/em>i. It is set to true.\r\n\r\n&nbsp;\r\n\r\nwhile (waiting[i] &amp;&amp; key)\u00a0\u00a0 key = TestAndSet(lock);\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">For process <em>P<\/em>i, waiting[i] and key are true, initially. Therefore, the condition (<em>waiting[i] &amp;&amp; key<\/em>) is true. Since this condition is true, <em>P<\/em>i executes TestAndSet(lock). Initially, lock is false. Since lock is false, TestAndSet(lock) returns false and the value of lock is changed to true. The return value is assigned to key. Thus, key is now assigned the value \u2013 false. Now, the condition (<em>waiting[i] &amp;&amp; key<\/em>) becomes false. Process <em>P<\/em>i comes out of the while loop.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">If lock is true, TestAndSet(lock) returns true. Therefore, key is assigned true. The condition in the while loop remains true. Process <em>P<\/em>i continues to execute in while loop.<\/p>\r\n&nbsp;\r\n\r\nwaiting[i] = FALSE;After coming out of the while loop, <em>P<\/em>i changes waiting[i] to false and enters its critical section.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Process <em>P<\/em>i, after coming out of its critical section, checks if any other process is waiting to enter its critical section. If any other process is waiting to enter its critical section, <em>P<\/em>i changes the value of <em>waiting <\/em>of that process to false. Else, changes the value of lock to false. Changing the value of <em>waiting <\/em>of a process to false will make that waiting process to come out of the while loop and enter its critical section. If no process is waiting, the value of lock is changed to false. This paves the way for any other process wanting to enter its critical section, to enter its critical section in future.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">j = (i + 1) % n;<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">The next process <\/span><em style=\"text-align: initial;font-size: 1em\">P<\/em><span style=\"text-align: initial;font-size: 1em\">j to be checked is found. j is found by incrementing i.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">while ((j != i) &amp;&amp; !waiting[j])\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 j = (j + 1) % n;<\/span><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><em>P<\/em>i checks if waiting[j] is true, for every other process j. If true, <em>P<\/em>i comes out of its while loop. This means that there is another process j in its entry section waiting to enter its critical section. If waiting[j] is not true, process j is not in its entry section. Therefore, the other processes are checked one after the other. When j becomes equal to i, it means that all other processes have been checked already. There is no other process waiting in its entry section.<\/p>\r\n&nbsp;\r\n\r\n<strong>if (j == i)<\/strong>\r\n<p style=\"padding-left: 30px\"><strong>lock = FALSE;<\/strong><\/p>\r\n<strong style=\"text-align: initial;font-size: 1em\">else<\/strong>\r\n<p style=\"padding-left: 30px\"><strong style=\"text-align: initial;font-size: 1em\">w<\/strong><strong style=\"text-align: initial;font-size: 1em\">a<\/strong><strong style=\"text-align: initial;font-size: 1em\">iting[j] = FALSE;<\/strong><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">If j==i, there is no other process in its entry section and lock is made false. Else, waiting[j] (j is the process waiting in its entry section) is made false.<\/p>\r\n&nbsp;\r\n\r\n<strong>13.3.1\u00a0 Mutual exclusion\u00a0<\/strong>\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><strong>w<\/strong><strong>a<\/strong><strong>iting[0]<\/strong><\/td>\r\n<td><strong><em>P<\/em><\/strong><strong>0<\/strong><\/td>\r\n<td><strong>key(0)<\/strong><\/td>\r\n<td><strong>lock<\/strong><\/td>\r\n<td><strong>key(1)<\/strong><\/td>\r\n<td><strong><em>P<\/em><\/strong><strong>1<\/strong><\/td>\r\n<td><strong>w<\/strong><strong>a<\/strong><strong>iting[1]<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>false<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>false<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>false<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>true<\/td>\r\n<td><\/td>\r\n<td>true<\/td>\r\n<td><\/td>\r\n<td>true<\/td>\r\n<td><\/td>\r\n<td>true<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>TestAndSet(lock)<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>false<\/td>\r\n<td>true<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>false<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>Enters CS<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>TestAndSet(lock)<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>True<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>Waits<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The sequence given above shows how the algorithm satisfies the mutual exclusion requirement. Let <em>P<\/em>0 and <em>P<\/em>1 be the two processes competing to enter their respective critical sections. Initially, lock, waiting[0] and waiting[1] are false. <em>P<\/em>0 changes waiting[0] and its local variable key(0) to true. <em>P<\/em>0 then executes TestAndSet(lock). Since lock is false, TestAndSet(lock) returns false and the value is assigned to key(0). The value of lock is changed to true. Since key(0) becomes false, <em>P<\/em>0 comes out of its while loop, changes waiting[0] to false and enters its critical section. While <em>P<\/em>0 is in its critical section, let <em>P<\/em>1 want to enter its critical section. <em>P<\/em>1 changes waiting[1] and its local variable key(1) to true. <em>P<\/em>0 then executes TestAndSet(lock). Since lock is true, TestAndSet(lock) returns true and the value \u2018true\u2019 is assigned to key(0). Since key(0) is true, <em>P<\/em>1 continues to execute in its while loop. Thus, when one process is in its critical section, the other processes are not allowed to enter their respective critical sections. Thus,\u00a0<span style=\"font-size: 1em\">mutual exclusion is satisfied.<\/span><\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>13.3.2\u00a0 Progress \u2013 Scenario 1<\/strong><\/p>\r\n\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><strong>w<\/strong><strong>a<\/strong><strong>iting[0]<\/strong><\/td>\r\n<td><strong><em>P<\/em><\/strong><sub><strong>0<\/strong><\/sub><\/td>\r\n<td><strong>key(0)<\/strong><\/td>\r\n<td><strong>lock<\/strong><\/td>\r\n<td><strong>key(1)<\/strong><\/td>\r\n<td><strong><em>P<\/em><\/strong><sub><strong>1<\/strong><\/sub><\/td>\r\n<td><strong>w<\/strong><strong>a<\/strong><strong>iting[1]<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>false<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>false<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>false<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>true<\/td>\r\n<td><\/td>\r\n<td>true<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>true<\/td>\r\n<td><\/td>\r\n<td>true<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>TestAndSet(lock)<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>false<\/td>\r\n<td>true<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>false<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>Enters CS<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>TestAndSet(lock)<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>True<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>Waits<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>Out of CS<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>false<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>Enters CS<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The sequence given above, shows how the algorithm satisfies the progress requirement. Let <em>P<\/em>0 and <em>P<\/em>1 be the two processes competing to enter their respective critical sections. Initially, lock, waiting[0] and waiting[1] are false. <em>P<\/em>0 changes waiting[0] and its local variable key(0) to true. Now, there is a context switch and <em>P<\/em>1 changes waiting[1] and key(1) to true. Again, there is a context switch and <em>P<\/em>0 executes TestAndSet(lock). Since lock is false, TestAndSet(lock) returns false and the value \u2018false\u2019 is assigned to key(0). The value of lock is changed to true. Since key(0) becomes false, <em>P<\/em>0 comes out of while loop, changes waiting[0] to false and enters its critical section. Now, if <em>P<\/em>1 gets the CPU, it executes TestAndSet(lock). Since lock is true, TestAndSet(lock) returns true and the value \u2018true\u2019 is assigned to key(0). Since key(0) is true, <em>P<\/em>1 continues to execute in its while loop. When <em>P<\/em>0 comes out of its critical section, it checks if the waiting value of any other process is true. This checking is done one process after the other. In this example, since there are only two processes, the waiting status of <em>P<\/em>1 is checked. Since waiting[1] is true, !waiting[1] is false. This makes the condition in the while loop false for <em>P<\/em>0. <em>P<\/em>0 comes out of the while loop and changes waiting[1] to false. When waiting[1] becomes false, <em>P<\/em>1 comes out of the while loop <strong>while (waiting[i] &amp;&amp; key) key = TestAndSet(lock);<\/strong>. <em>P<\/em>1 enters its critical section. Hence, there is progress.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>13.3.3 Progress \u2013 Scenario 2<\/strong><\/p>\r\n\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><strong>w<\/strong><strong>a<\/strong><strong>iting[0]<\/strong><\/td>\r\n<td><strong><em>P<\/em><\/strong><strong>0<\/strong><\/td>\r\n<td><strong>key(0)<\/strong><\/td>\r\n<td><strong>lock<\/strong><\/td>\r\n<td><strong>key(1)<\/strong><\/td>\r\n<td><strong><em>P<\/em><\/strong><strong>1<\/strong><\/td>\r\n<td><strong>w<\/strong><strong>a<\/strong><strong>iting[1]<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>false<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>false<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>false<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>true<\/td>\r\n<td><\/td>\r\n<td>true<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>TestAndSet(lock)<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td><\/td>\r\n<td>false<\/td>\r\n<td>true<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>false<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>Enters CS<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>Out of CS<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>No other process is waiting<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Another sequence of execution to show that progress is satisfied is given above. Let <em>P<\/em>0 and <em>P<\/em>1 be the two processes competing to enter their respective critical sections. Initially, lock, waiting[0] and waiting[1] are false. <em>P<\/em>0 changes waiting[0] and its local variable key(0) to true. Since lock is false, TestAndSet(lock) returns false and the value is assigned to key(0). The value of lock is changed to true. Since key(0) becomes false, <em>P<\/em>0\u00a0 comes out of the while loop, changes waiting[0] to false and enters its critical section. When <em>P<\/em>0 comes out of its critical section, it checks if the waiting value of any other process is true. This checking is done one process after the other. In this example, since there are only two processes, the waiting status of <em>P<\/em>1 is checked. Since waiting[1] is false, !waiting[1] becomes true. This makes the condition in the while loop true for <em>P<\/em>0. So, <em>P<\/em>0 executes j=j +1 % n. j was 1 earlier. Now, it becomes 0. Since j becomes equal to i, <em>P<\/em>0 comes out of the while loop and changes lock to false. Since lock is changed to false, any process can enter its critical section in future.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>13.3.4\u00a0 Bounded waiting:<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">After a process comes out of its critical section, it checks if the next process is waiting to enter its critical section. Similarly, every other process is checked. Only if no other process is in the entry section, the value of lock is changed to false. Thus, when there are <em>n <\/em>processes, a process may have to wait for <em>n<\/em>\u20131 critical section executions before its turn to enter critical section. Thus, the bounded waiting requirement is satisfied.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>13.4 Summary<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this module, we first learned a solution to the critical section problem using the Swap instruction. But the solution did not satisfy the bounded waiting requirement. Next, we learned a solution to the critical section problem using the TestAndSet instruction that satisfies all the three requirements.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>References<\/strong><\/p>\r\n\r\n<ol>\r\n \t<li style=\"text-align: justify\">Abraham Silberschatz, Peter B. Galvin, Greg Gagne, \u201cOperating System Concepts\u201d, Ninth Edition, John Wiley &amp; Sons Inc., 2012.<\/li>\r\n<\/ol>","rendered":"<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>13.1\u00a0 Introduction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Cooperating processes may share data among themselves. Cooperating processes use common variables and data structures for sharing data. In a process, that portion of code where the process accesses shared data is called the critical section of the process. If there is no proper synchronization between the processes when they access their critical sections of code, then inconsistency of data may result. We saw a few solutions to the critical section problem in the previous modules (Modules 10, 11 and 12). In this module, we continue to learn more solutions to solve the critical section problem when there are multiple (&gt;=2) processes. The Test And Set and Swap are two hardware instructions that can be executed atomically and can provide solution to the critical section problem. We can use these special instructions to solve the critical section problem.<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p><strong>13.2\u00a0 Swap instruction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"padding-left: 30px\">The Swap instruction is defined as follows:<\/p>\n<p>void Swap(boolean &amp;a, boolean &amp;b)<\/p>\n<p><strong>{<\/strong><\/p>\n<p><strong>boolean temp = a; <\/strong><\/p>\n<p><strong>a = b;<\/strong><\/p>\n<p><strong>b = temp;<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The Swap instruction takes two Boolean variables and swaps the value of the variables. The swapping is done atomically.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>13.2.1 Solution to the Critical Section Problem Using the Swap Instruction\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A solution to the critical section problem using the Swap instruction is explained in this section. This is a multiple-process solution. More than two processes competing to enter their critical sections can use this solution to solve the critical section problem.<\/p>\n<p>&nbsp;<\/p>\n<p>Shared data:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"padding-left: 30px\"><strong>boolean lock; \/* <\/strong>(initialized to <strong>false<\/strong>) *\/<\/p>\n<p>&nbsp;<\/p>\n<p>The algorithm for process <em>P<\/em><em>i <\/em>is given below:<\/p>\n<p style=\"padding-left: 60px\"><strong><span style=\"text-align: justify;font-size: 1em\">do{<\/span><\/strong><\/p>\n<p style=\"padding-left: 90px\"><strong style=\"font-size: 1em;text-align: initial\">key = true;<\/strong><\/p>\n<p style=\"padding-left: 90px\"><strong style=\"text-align: initial;font-size: 1em\">w<\/strong><strong style=\"text-align: initial;font-size: 1em\">hile (key == true)<\/strong><\/p>\n<p style=\"padding-left: 150px\"><strong style=\"text-align: initial;font-size: 1em\">Swap(lock, key);<\/strong><\/p>\n<p style=\"padding-left: 120px\"><span style=\"text-align: initial;font-size: 1em\">critical section<\/span><\/p>\n<p style=\"padding-left: 90px\"><strong><span style=\"text-align: initial;font-size: 1em\">lock=false;<\/span><\/strong><\/p>\n<p style=\"padding-left: 120px\"><span style=\"font-size: 1em;text-align: initial\">remainder section<\/span><\/p>\n<p style=\"padding-left: 60px\"><span style=\"text-align: initial;font-size: 1em\">}\u00a0<\/span><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>In this algorithm, lock is a shared variable and key is a variable local to each process. When process <em>P<\/em>i executes this algorithm, it changes the value of its local variable key to true. Then, as long as key is true, <em>P<\/em>i keeps swapping the values of lock and key.<\/p>\n<p>&nbsp;<\/p>\n<p>If the value of lock is true, even though lock and key are swapped, the value of key remains true. This makes process <em>P<\/em>i to continue to check in the while loop.<\/p>\n<p>&nbsp;<\/p>\n<p>If the value of lock is false, swapping lock and key will change the value of key to false and the value of lock to true. This makes the process\u00a0 <em>P<\/em>i\u00a0 \u00a0to come out of the while loop (while(key==true) becomes while(false)). If <em>P<\/em>i \u00a0comes out of the while loop, it enters its critical section. The value of lock remains true during this time.<\/p>\n<p>&nbsp;<\/p>\n<p>When <em>P<\/em>i comes out of its critical section, it changes the value of lock to false.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>13.2.2. Mutual Exclusion\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Let us now see how this algorithm satisfies mutual exclusion.<\/p>\n<p>&nbsp;<\/p>\n<p><strong><em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 P<\/em><\/strong><strong><sub>0<\/sub>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/strong><strong>key(<em>P<\/em><\/strong><sub><strong>0<\/strong><\/sub><strong>)\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0lock\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0key(<em>P<\/em><\/strong><strong>1<\/strong><strong>)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>P<\/em><\/strong><strong>1<\/strong><\/p>\n<table style=\"height: 336px; width: 622px;\">\n<tbody>\n<tr>\n<td style=\"width: 156.063px\"><\/td>\n<td style=\"width: 95.0625px\"><\/td>\n<td style=\"width: 95.0625px\">false<\/td>\n<td style=\"width: 96.0625px\"><\/td>\n<td style=\"width: 109.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 156.063px\"><\/td>\n<td style=\"width: 95.0625px\">true<\/td>\n<td style=\"width: 95.0625px\"><\/td>\n<td style=\"width: 96.0625px\"><\/td>\n<td style=\"width: 109.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 156.063px\">Swap<\/td>\n<td style=\"width: 95.0625px\">false<\/td>\n<td style=\"width: 95.0625px\">true<\/td>\n<td style=\"width: 96.0625px\"><\/td>\n<td style=\"width: 109.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 156.063px\"><\/td>\n<td style=\"width: 95.0625px\"><\/td>\n<td style=\"width: 95.0625px\"><\/td>\n<td style=\"width: 96.0625px\">true<\/td>\n<td style=\"width: 109.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 156.063px\">In CS<\/td>\n<td style=\"width: 95.0625px\"><\/td>\n<td style=\"width: 95.0625px\"><\/td>\n<td style=\"width: 96.0625px\"><\/td>\n<td style=\"width: 109.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 156.063px\"><\/td>\n<td style=\"width: 95.0625px\"><\/td>\n<td style=\"width: 95.0625px\"><\/td>\n<td style=\"width: 96.0625px\"><\/td>\n<td style=\"width: 109.063px\">Swap<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 156.063px\"><\/td>\n<td style=\"width: 95.0625px\"><\/td>\n<td style=\"width: 95.0625px\"><\/td>\n<td style=\"width: 96.0625px\">true<\/td>\n<td style=\"width: 109.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 156.063px\"><\/td>\n<td style=\"width: 95.0625px\"><\/td>\n<td style=\"width: 95.0625px\"><\/td>\n<td style=\"width: 96.0625px\"><\/td>\n<td style=\"width: 109.063px\">waits<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 156.063px\">Out of CS<\/td>\n<td style=\"width: 95.0625px\"><\/td>\n<td style=\"width: 95.0625px\"><\/td>\n<td style=\"width: 96.0625px\"><\/td>\n<td style=\"width: 109.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 156.063px\"><\/td>\n<td style=\"width: 95.0625px\"><\/td>\n<td style=\"width: 95.0625px\">false<\/td>\n<td style=\"width: 96.0625px\"><\/td>\n<td style=\"width: 109.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 156.063px\"><\/td>\n<td style=\"width: 95.0625px\"><\/td>\n<td style=\"width: 95.0625px\">true<\/td>\n<td style=\"width: 96.0625px\">false<\/td>\n<td style=\"width: 109.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 156.063px\"><\/td>\n<td style=\"width: 95.0625px\"><\/td>\n<td style=\"width: 95.0625px\"><\/td>\n<td style=\"width: 96.0625px\"><\/td>\n<td style=\"width: 109.063px\">In CS<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">Let two processes <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">0 and <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1 compete to enter their respective critical sections. Each of the two processes has its own local key variable. Initially, lock is false. The key variables of each of the two processes are true initially. <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">0 wants to enter critical section first. <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">0 executes Swap(key(<\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">0), lock). This will make the value of lock true and the value of key false. As key becomes false, <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">0 comes out of the while loop and enters its critical section. Suppose, <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1 wants to enter its critical section when <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">0 is in its critical section. <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1 sets the value of its key to true. <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1 executes Swap(key(<\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1), lock). Since the value of lock is currently true, the value of key also continues to be true. <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1 continues to execute in the while loop. When <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">0 comes out of its critical section, it changes the value of lock to false. Now <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1, executing in the while loop, swaps lock and its key. <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1\u2019s key becomes false and <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1 comes out of the while loop and enters critical section. Thus, it is seen that <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">1 is not allowed to enter its critical section when <\/span><em style=\"font-size: 1em\">P<\/em><span style=\"font-size: 1em\">0 is in its critical section. When one process is in its critical section, the value of lock is true and no other process can enter its critical section. Thus, mutual exclusion is satisfied.<\/span><\/p>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p><strong>13.2.3 Swap \u2013 Bounded Waiting<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>We now see if this algorithm satisfies the bounded waiting requirement.<\/p>\n<p>&nbsp;<\/p>\n<p><strong><em>\u00a0 \u00a0 \u00a0 P<\/em><\/strong><strong>0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><strong>key(<em>P<\/em><\/strong><strong>0<\/strong><strong>)\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0lock\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0key(<em>P<\/em><\/strong><strong>1<\/strong><strong>)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>P<\/em><\/strong><strong>1<\/strong><\/p>\n<table style=\"height: 338px; width: 668px;\">\n<tbody>\n<tr>\n<td style=\"width: 166.063px\"><\/td>\n<td style=\"width: 88.0625px\"><\/td>\n<td style=\"width: 88.0625px\">false<\/td>\n<td style=\"width: 84.0625px\"><\/td>\n<td style=\"width: 171.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 166.063px\"><\/td>\n<td style=\"width: 88.0625px\">true<\/td>\n<td style=\"width: 88.0625px\"><\/td>\n<td style=\"width: 84.0625px\"><\/td>\n<td style=\"width: 171.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 166.063px\">Swap<\/td>\n<td style=\"width: 88.0625px\">false<\/td>\n<td style=\"width: 88.0625px\">true<\/td>\n<td style=\"width: 84.0625px\"><\/td>\n<td style=\"width: 171.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 166.063px\"><\/td>\n<td style=\"width: 88.0625px\"><\/td>\n<td style=\"width: 88.0625px\"><\/td>\n<td style=\"width: 84.0625px\">true<\/td>\n<td style=\"width: 171.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 166.063px\">In CS<\/td>\n<td style=\"width: 88.0625px\"><\/td>\n<td style=\"width: 88.0625px\"><\/td>\n<td style=\"width: 84.0625px\"><\/td>\n<td style=\"width: 171.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 166.063px\"><\/td>\n<td style=\"width: 88.0625px\"><\/td>\n<td style=\"width: 88.0625px\"><\/td>\n<td style=\"width: 84.0625px\"><\/td>\n<td style=\"width: 171.063px\">Swap<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 166.063px\"><\/td>\n<td style=\"width: 88.0625px\"><\/td>\n<td style=\"width: 88.0625px\"><\/td>\n<td style=\"width: 84.0625px\">true<\/td>\n<td style=\"width: 171.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 166.063px\"><\/td>\n<td style=\"width: 88.0625px\"><\/td>\n<td style=\"width: 88.0625px\"><\/td>\n<td style=\"width: 84.0625px\"><\/td>\n<td style=\"width: 171.063px\">Waits<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 166.063px\">Out of CS<\/td>\n<td style=\"width: 88.0625px\"><\/td>\n<td style=\"width: 88.0625px\"><\/td>\n<td style=\"width: 84.0625px\"><\/td>\n<td style=\"width: 171.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 166.063px\">Swap<\/td>\n<td style=\"width: 88.0625px\">false<\/td>\n<td style=\"width: 88.0625px\">true<\/td>\n<td style=\"width: 84.0625px\"><\/td>\n<td style=\"width: 171.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 166.063px\">In CS<\/td>\n<td style=\"width: 88.0625px\"><\/td>\n<td style=\"width: 88.0625px\"><\/td>\n<td style=\"width: 84.0625px\"><\/td>\n<td style=\"width: 171.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 166.063px\"><\/td>\n<td style=\"width: 88.0625px\"><\/td>\n<td style=\"width: 88.0625px\"><\/td>\n<td style=\"width: 84.0625px\"><\/td>\n<td style=\"width: 171.063px\">Still waits<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let <em>P<\/em>0 and <em>P<\/em>1 be two processes that are competing to access their respective critical sections. Initially, lock is false. Initially, the key variable of each of the two processes is true. <em>P<\/em>0 wants to enter its critical section first. <em>P<\/em>0 executes Swap(key(<em>P<\/em>0), lock). This makes the value of lock true and the value of key false. As key becomes false, <em>P<\/em>0 comes out of the while loop and enters its critical section. Let <em>P<\/em>1 want to enter its critical section when <em>P<\/em>0 is in its critical section. <em>P<\/em>1 sets the value of its key to true. <em>P<\/em>1 executes Swap(key(<em>P<\/em>1), lock). Since the value of lock is currently true, the value of key also continues to be true. <em>P<\/em>1\u00a0 continues to execute in the while loop. When <em>P<\/em>0 comes out of its critical section, it changes the value of lock to false. Let <em>P<\/em>0 continue to use the CPU without context switch. Say, <em>P<\/em>0 wants to enter its critical section again. Since the value of lock is currently false, <em>P<\/em>0 can enter its critical section again. Process <em>P<\/em>1 continues to wait. Since there was no context switch after <em>P<\/em>0 came out of its critical section and <em>P<\/em>0 changed lock to true, <em>P<\/em>1 could not come out of the while loop. Thus, even though <em>P<\/em>1 had requested to enter its critical section, <em>P<\/em>0 is allowed to enter its critical section again and again. Therefore, the bounded waiting requirement is not satisfied.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Thus, it is seen that this algorithm does not satisfy the bounded waiting requirement, though it satisfies the mutual exclusion requirement.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>13.3\u00a0 TestandSet &#8211; Bounded-Waiting &#8211; Mutual Exclusion\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The algorithm given below makes use of the TestAndSet instruction and satisfies all the three requirements for a solution to the critical section problem. This is also a solution when there are <em>n <\/em>processes.<\/p>\n<p>&nbsp;<\/p>\n<p>The shared variables used in the algorithm are as follows:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"padding-left: 180px\"><strong>boolean waiting[n];\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/initially false <\/strong><\/p>\n<p style=\"padding-left: 180px\"><strong>boolean lock\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/initially false\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The algorithm for process <em>P<\/em>i is given below:<\/p>\n<p>do {<\/p>\n<p style=\"padding-left: 30px\"><span style=\"text-align: initial;font-size: 1em\">waiting[i] = TRUE; key = TRUE;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"text-align: initial;font-size: 1em\">while (waiting[i] &amp;&amp; key) \u00a0key = TestAndSet(lock); <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"text-align: initial;font-size: 1em\">waiting[i] = FALSE;<\/span><\/p>\n<p style=\"padding-left: 60px\"><span style=\"text-align: initial;font-size: 1em\">\/\/ critical section j = (i + 1) % n;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"text-align: initial;font-size: 1em\">while ((j != i) &amp;&amp; !waiting[j])\u00a0\u00a0\u00a0\u00a0 j = (j + 1) % n;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"text-align: initial;font-size: 1em\">if (j == i)<\/span><\/p>\n<p style=\"padding-left: 60px\"><span style=\"text-align: initial;font-size: 1em\">lock = FALSE;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"text-align: initial;font-size: 1em\">else<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"text-align: initial;font-size: 1em\">waiting[j] = FALSE;<\/span><\/p>\n<p style=\"padding-left: 60px\"><span style=\"text-align: initial;font-size: 1em\">\/\/ remainder section<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">} while (TRUE);<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The algorithm given above is for process <em>P<\/em>i. <em>waiting <\/em>is an array with \u2018<em>n<\/em>\u2019 elements, where <em>n <\/em>is the number of processes competing to enter their respective critical sections. <em>lock <\/em>is a Boolean variable. Each line in the algorithm is explained below:<\/p>\n<p>&nbsp;<\/p>\n<p>waiting[i] = TRUE;<\/p>\n<p>&nbsp;<\/p>\n<p>Process <em>P<\/em>i before checking to enter its critical section sets the value of waiting[i] to true.<\/p>\n<p>&nbsp;<\/p>\n<p>key = TRUE;<\/p>\n<p>&nbsp;<\/p>\n<p>key is a variable local to process <em>P<\/em>i. It is set to true.<\/p>\n<p>&nbsp;<\/p>\n<p>while (waiting[i] &amp;&amp; key)\u00a0\u00a0 key = TestAndSet(lock);<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">For process <em>P<\/em>i, waiting[i] and key are true, initially. Therefore, the condition (<em>waiting[i] &amp;&amp; key<\/em>) is true. Since this condition is true, <em>P<\/em>i executes TestAndSet(lock). Initially, lock is false. Since lock is false, TestAndSet(lock) returns false and the value of lock is changed to true. The return value is assigned to key. Thus, key is now assigned the value \u2013 false. Now, the condition (<em>waiting[i] &amp;&amp; key<\/em>) becomes false. Process <em>P<\/em>i comes out of the while loop.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">If lock is true, TestAndSet(lock) returns true. Therefore, key is assigned true. The condition in the while loop remains true. Process <em>P<\/em>i continues to execute in while loop.<\/p>\n<p>&nbsp;<\/p>\n<p>waiting[i] = FALSE;After coming out of the while loop, <em>P<\/em>i changes waiting[i] to false and enters its critical section.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Process <em>P<\/em>i, after coming out of its critical section, checks if any other process is waiting to enter its critical section. If any other process is waiting to enter its critical section, <em>P<\/em>i changes the value of <em>waiting <\/em>of that process to false. Else, changes the value of lock to false. Changing the value of <em>waiting <\/em>of a process to false will make that waiting process to come out of the while loop and enter its critical section. If no process is waiting, the value of lock is changed to false. This paves the way for any other process wanting to enter its critical section, to enter its critical section in future.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">j = (i + 1) % n;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">The next process <\/span><em style=\"text-align: initial;font-size: 1em\">P<\/em><span style=\"text-align: initial;font-size: 1em\">j to be checked is found. j is found by incrementing i.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">while ((j != i) &amp;&amp; !waiting[j])\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 j = (j + 1) % n;<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><em>P<\/em>i checks if waiting[j] is true, for every other process j. If true, <em>P<\/em>i comes out of its while loop. This means that there is another process j in its entry section waiting to enter its critical section. If waiting[j] is not true, process j is not in its entry section. Therefore, the other processes are checked one after the other. When j becomes equal to i, it means that all other processes have been checked already. There is no other process waiting in its entry section.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>if (j == i)<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong>lock = FALSE;<\/strong><\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">else<\/strong><\/p>\n<p style=\"padding-left: 30px\"><strong style=\"text-align: initial;font-size: 1em\">w<\/strong><strong style=\"text-align: initial;font-size: 1em\">a<\/strong><strong style=\"text-align: initial;font-size: 1em\">iting[j] = FALSE;<\/strong><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">If j==i, there is no other process in its entry section and lock is made false. Else, waiting[j] (j is the process waiting in its entry section) is made false.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>13.3.1\u00a0 Mutual exclusion\u00a0<\/strong><\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><strong>w<\/strong><strong>a<\/strong><strong>iting[0]<\/strong><\/td>\n<td><strong><em>P<\/em><\/strong><strong>0<\/strong><\/td>\n<td><strong>key(0)<\/strong><\/td>\n<td><strong>lock<\/strong><\/td>\n<td><strong>key(1)<\/strong><\/td>\n<td><strong><em>P<\/em><\/strong><strong>1<\/strong><\/td>\n<td><strong>w<\/strong><strong>a<\/strong><strong>iting[1]<\/strong><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td>false<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>false<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td>false<\/td>\n<\/tr>\n<tr>\n<td>true<\/td>\n<td><\/td>\n<td>true<\/td>\n<td><\/td>\n<td>true<\/td>\n<td><\/td>\n<td>true<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>TestAndSet(lock)<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<td>false<\/td>\n<td>true<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>false<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>Enters CS<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td>TestAndSet(lock)<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td>True<\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td>Waits<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The sequence given above shows how the algorithm satisfies the mutual exclusion requirement. Let <em>P<\/em>0 and <em>P<\/em>1 be the two processes competing to enter their respective critical sections. Initially, lock, waiting[0] and waiting[1] are false. <em>P<\/em>0 changes waiting[0] and its local variable key(0) to true. <em>P<\/em>0 then executes TestAndSet(lock). Since lock is false, TestAndSet(lock) returns false and the value is assigned to key(0). The value of lock is changed to true. Since key(0) becomes false, <em>P<\/em>0 comes out of its while loop, changes waiting[0] to false and enters its critical section. While <em>P<\/em>0 is in its critical section, let <em>P<\/em>1 want to enter its critical section. <em>P<\/em>1 changes waiting[1] and its local variable key(1) to true. <em>P<\/em>0 then executes TestAndSet(lock). Since lock is true, TestAndSet(lock) returns true and the value \u2018true\u2019 is assigned to key(0). Since key(0) is true, <em>P<\/em>1 continues to execute in its while loop. Thus, when one process is in its critical section, the other processes are not allowed to enter their respective critical sections. Thus,\u00a0<span style=\"font-size: 1em\">mutual exclusion is satisfied.<\/span><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>13.3.2\u00a0 Progress \u2013 Scenario 1<\/strong><\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><strong>w<\/strong><strong>a<\/strong><strong>iting[0]<\/strong><\/td>\n<td><strong><em>P<\/em><\/strong><sub><strong>0<\/strong><\/sub><\/td>\n<td><strong>key(0)<\/strong><\/td>\n<td><strong>lock<\/strong><\/td>\n<td><strong>key(1)<\/strong><\/td>\n<td><strong><em>P<\/em><\/strong><sub><strong>1<\/strong><\/sub><\/td>\n<td><strong>w<\/strong><strong>a<\/strong><strong>iting[1]<\/strong><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td>false<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>false<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td>false<\/td>\n<\/tr>\n<tr>\n<td>true<\/td>\n<td><\/td>\n<td>true<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td>true<\/td>\n<td><\/td>\n<td>true<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>TestAndSet(lock)<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<td>false<\/td>\n<td>true<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>false<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>Enters CS<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td>TestAndSet(lock)<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td>True<\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td>Waits<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>Out of CS<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td>false<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td>Enters CS<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The sequence given above, shows how the algorithm satisfies the progress requirement. Let <em>P<\/em>0 and <em>P<\/em>1 be the two processes competing to enter their respective critical sections. Initially, lock, waiting[0] and waiting[1] are false. <em>P<\/em>0 changes waiting[0] and its local variable key(0) to true. Now, there is a context switch and <em>P<\/em>1 changes waiting[1] and key(1) to true. Again, there is a context switch and <em>P<\/em>0 executes TestAndSet(lock). Since lock is false, TestAndSet(lock) returns false and the value \u2018false\u2019 is assigned to key(0). The value of lock is changed to true. Since key(0) becomes false, <em>P<\/em>0 comes out of while loop, changes waiting[0] to false and enters its critical section. Now, if <em>P<\/em>1 gets the CPU, it executes TestAndSet(lock). Since lock is true, TestAndSet(lock) returns true and the value \u2018true\u2019 is assigned to key(0). Since key(0) is true, <em>P<\/em>1 continues to execute in its while loop. When <em>P<\/em>0 comes out of its critical section, it checks if the waiting value of any other process is true. This checking is done one process after the other. In this example, since there are only two processes, the waiting status of <em>P<\/em>1 is checked. Since waiting[1] is true, !waiting[1] is false. This makes the condition in the while loop false for <em>P<\/em>0. <em>P<\/em>0 comes out of the while loop and changes waiting[1] to false. When waiting[1] becomes false, <em>P<\/em>1 comes out of the while loop <strong>while (waiting[i] &amp;&amp; key) key = TestAndSet(lock);<\/strong>. <em>P<\/em>1 enters its critical section. Hence, there is progress.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>13.3.3 Progress \u2013 Scenario 2<\/strong><\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><strong>w<\/strong><strong>a<\/strong><strong>iting[0]<\/strong><\/td>\n<td><strong><em>P<\/em><\/strong><strong>0<\/strong><\/td>\n<td><strong>key(0)<\/strong><\/td>\n<td><strong>lock<\/strong><\/td>\n<td><strong>key(1)<\/strong><\/td>\n<td><strong><em>P<\/em><\/strong><strong>1<\/strong><\/td>\n<td><strong>w<\/strong><strong>a<\/strong><strong>iting[1]<\/strong><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td>false<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>false<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td>false<\/td>\n<\/tr>\n<tr>\n<td>true<\/td>\n<td><\/td>\n<td>true<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>TestAndSet(lock)<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<td>false<\/td>\n<td>true<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>false<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>Enters CS<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>Out of CS<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>No other process is waiting<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Another sequence of execution to show that progress is satisfied is given above. Let <em>P<\/em>0 and <em>P<\/em>1 be the two processes competing to enter their respective critical sections. Initially, lock, waiting[0] and waiting[1] are false. <em>P<\/em>0 changes waiting[0] and its local variable key(0) to true. Since lock is false, TestAndSet(lock) returns false and the value is assigned to key(0). The value of lock is changed to true. Since key(0) becomes false, <em>P<\/em>0\u00a0 comes out of the while loop, changes waiting[0] to false and enters its critical section. When <em>P<\/em>0 comes out of its critical section, it checks if the waiting value of any other process is true. This checking is done one process after the other. In this example, since there are only two processes, the waiting status of <em>P<\/em>1 is checked. Since waiting[1] is false, !waiting[1] becomes true. This makes the condition in the while loop true for <em>P<\/em>0. So, <em>P<\/em>0 executes j=j +1 % n. j was 1 earlier. Now, it becomes 0. Since j becomes equal to i, <em>P<\/em>0 comes out of the while loop and changes lock to false. Since lock is changed to false, any process can enter its critical section in future.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>13.3.4\u00a0 Bounded waiting:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">After a process comes out of its critical section, it checks if the next process is waiting to enter its critical section. Similarly, every other process is checked. Only if no other process is in the entry section, the value of lock is changed to false. Thus, when there are <em>n <\/em>processes, a process may have to wait for <em>n<\/em>\u20131 critical section executions before its turn to enter critical section. Thus, the bounded waiting requirement is satisfied.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>13.4 Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this module, we first learned a solution to the critical section problem using the Swap instruction. But the solution did not satisfy the bounded waiting requirement. Next, we learned a solution to the critical section problem using the TestAndSet instruction that satisfies all the three requirements.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>References<\/strong><\/p>\n<ol>\n<li style=\"text-align: justify\">Abraham Silberschatz, Peter B. Galvin, Greg Gagne, \u201cOperating System Concepts\u201d, Ninth Edition, John Wiley &amp; Sons Inc., 2012.<\/li>\n<\/ol>\n","protected":false},"author":4,"menu_order":10,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-mary-anitha-rajam"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-136","chapter","type-chapter","status-publish","hentry","contributor-dr-mary-anitha-rajam"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters\/136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":5,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters\/136\/revisions"}],"predecessor-version":[{"id":415,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters\/136\/revisions\/415"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters\/136\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/media?parent=136"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapter-type?post=136"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/contributor?post=136"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/license?post=136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}