{"id":90,"date":"2018-07-19T06:07:12","date_gmt":"2018-07-19T06:07:12","guid":{"rendered":"http:\/\/csp3.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=90"},"modified":"2018-08-07T05:13:46","modified_gmt":"2018-08-07T05:13:46","slug":"process-synchronization","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/chapter\/process-synchronization\/","title":{"rendered":"Process Synchronization"},"content":{"raw":"<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>10.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 share data either directly or through files. When two or more processes access shared data concurrently, data inconsistency may result. Maintaining data consistency requires mechanisms that will ensure the orderly execution of cooperating processes. The section of code in each process in which the process accesses shared data is called the critical-section of the process. During the execution of a process, the process may access many such shared data or the same shared data a number of times. Thus, a process may execute critical-sections of code multiple times during its execution. \u00a0This module introduces the critical-section problem and discusses solutions to 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>10.2\u00a0 Bounded-buffer problem\u00a0<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">To understand the critical-section problem, we will revisit the bounded-buffer problem we learnt in an earlier module. The shared-memory solution to the bounded-buffer problem, discussed in the earlier module, allows at most N \u2013 1 items in the buffer at the same time. The solution is as follows:<\/p>\r\n&nbsp;\r\n\r\n<strong>Shared data:<\/strong>\r\n\r\n#define BUFFER_SIZE 10\r\n\r\ntypedef struct {\r\n\r\n. . .\r\n\r\n} item;\r\n\r\nitem buffer[BUFFER_SIZE];\r\n\r\nint in = 0;\r\n\r\nint out = 0;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">There is a buffer of size BUFFER_SIZE (10), which is used by the producer and the consumer processes to place and remove items respectively. There are two variables <em>in <\/em>and <em>out<\/em>, which point to the locations\u00a0 in the buffer where the producer places\u00a0 items and the consumer consumes items respectively. Initially, <em>in <\/em>and <em>out <\/em>point to the same location in the buffer.<\/p>\r\n&nbsp;\r\n\r\n<strong>C<\/strong><strong>od<\/strong><strong>e for producer process: <\/strong>\r\n\r\nitem next Produced;\r\n\r\nwhile (1) {\r\n<p style=\"padding-left: 60px\">while (((in + 1) % BUFFER_SIZE) == out) ; \/* do nothing *\/<\/p>\r\n<p style=\"padding-left: 60px\">buffer[in] = nextProduced;<\/p>\r\n<p style=\"padding-left: 60px\">in = (in + 1) % BUFFER_SIZE;<\/p>\r\n}\r\n\r\n<\/div>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">When (<\/span><em style=\"text-align: initial;font-size: 1em\">in <\/em><span style=\"text-align: initial;font-size: 1em\">+ 1) % BUFFER_SIZE == <\/span><em style=\"text-align: initial;font-size: 1em\">out<\/em><span style=\"text-align: initial;font-size: 1em\">, buffer is full. The producer waits if the buffer is full. Else, the producer places the item in the location pointed to by the <\/span><em style=\"text-align: initial;font-size: 1em\">in <\/em><span style=\"text-align: initial;font-size: 1em\">variable and increments the <\/span><em style=\"text-align: initial;font-size: 1em\">in <\/em><span style=\"text-align: initial;font-size: 1em\">variable.<\/span><\/p>\r\n\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n\r\nCode for consumer process:Item nextConsumed;\r\n\r\nwhile (1) {\r\n<p style=\"padding-left: 60px\">while (in == out); \/* do nothing *\/<\/p>\r\n<p style=\"padding-left: 60px\">nextConsumed = buffer[out];<\/p>\r\n<p style=\"padding-left: 60px\">out = (out + 1) % BUFFER_SIZE;<\/p>\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The buffer is empty when <em>in <\/em>== <em>out<\/em>. The consumer waits when the buffer is empty. If the buffer is not empty, the consumer consumes the item pointed to by the <em>out <\/em>variable and increments the <em>out <\/em>variable.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Suppose the consumer had not consumed any data. Then, the initial location will be pointed to by the <em>out <\/em>pointer. When the <em>in <\/em>pointer is pointing to the N \u2013 1th location, the condition \u201c(<em>in <\/em>+ 1) % BUFFER_SIZE == <em>out<\/em>\u201d will be true and the producer will wait. Thus, this solution uses only N \u2013 1 locations of the buffer.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">A solution, where all N buffers are used is not simple. A solution is possible when we modify the producer-consumer code by adding a variable <em>counter<\/em>, initialized to 0 and incrementing it each time a new item is added to the buffer. The code for the solution to the bounded buffer problem using the <em>counter <\/em>variable is given below:<\/p>\r\n&nbsp;\r\n\r\n#define BUFFER_SIZE 10\r\n\r\ntypedef struct {\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 . . .\r\n\r\n} item;\r\n\r\nitem buffer[BUFFER_SIZE];\r\n\r\nint in = 0;\r\n\r\nint out = 0;\r\n\r\nint counter = 0;\r\n\r\n<strong>\/*Producer process<\/strong>\r\n<p style=\"padding-left: 30px\">item nextProduced;<\/p>\r\n<p style=\"padding-left: 30px\">while (1) {<\/p>\r\n<p style=\"padding-left: 60px\">while (counter == BUFFER_SIZE); \u00a0\/* do nothing *\/<\/p>\r\n<p style=\"padding-left: 60px\">buffer[in] = nextProduced;<\/p>\r\n<p style=\"padding-left: 60px\">in = (in + 1) % BUFFER_SIZE;<\/p>\r\n<p style=\"padding-left: 60px\">counter++;<\/p>\r\n<p style=\"padding-left: 30px\">}<\/p>\r\n<strong>\/*Consumer process<\/strong>\r\n<p style=\"padding-left: 30px\">item nextConsumed; while (1) {<\/p>\r\n<p style=\"padding-left: 60px\">while (counter == 0); \u00a0\/* do nothing *\/<\/p>\r\n<p style=\"padding-left: 60px\">nextConsumed = buffer[out];<\/p>\r\n<p style=\"padding-left: 60px\">out = (out + 1) % BUFFER_SIZE;<\/p>\r\n<p style=\"padding-left: 60px\">counter--;<\/p>\r\n<p style=\"padding-left: 30px\">}<\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">In this solution, the variable <\/span><em style=\"text-align: initial;font-size: 1em\">counter is <\/em><span style=\"text-align: initial;font-size: 1em\">shared between the producer and the consumer. Whenever the producer places an item in the buffer, it increases the value of the variable. When the consumer consumes an item from the buffer, it decreases the value of the <\/span><em style=\"text-align: initial;font-size: 1em\">counter <\/em><span style=\"text-align: initial;font-size: 1em\">variable. Thus, the value of the variable <\/span><em style=\"text-align: initial;font-size: 1em\">counter <\/em><span style=\"text-align: initial;font-size: 1em\">maintains the count of the items present in the buffer. Now, we will see how the increment and decrement operations performed on the variable <\/span><em style=\"text-align: initial;font-size: 1em\">counter <\/em><span style=\"text-align: initial;font-size: 1em\">can lead to inconsistencies.<\/span><\/p>\r\n\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The statements \u201c<strong>counter++;\u201d <\/strong>and <strong>\u201ccounter--;\u201d <\/strong>must be performed <em>atomically<\/em>. Atomic operation means an operation that completes in its entirety without interruption. The reason for this is explained below:<\/p>\r\n&nbsp;\r\n\r\nThe statement \u201c<strong>counter++<\/strong>\u201d may be implemented in machine language as:\r\n\r\n&nbsp;\r\n\r\n<strong>register1 = counter <\/strong>\r\n\r\n<strong>register1 = register1 + 1 <\/strong>\r\n\r\n<strong>counter = register1\u00a0<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Thus, it is seen that the single statement <strong>counter++ <\/strong>in the producer\u2019s code is not executed as a single instruction. Rather, it is executed in three different instructions. Similarly, the single statement <strong>counter-- <\/strong>in the consumer\u2019s code is implemented in three different instructions.<\/p>\r\n&nbsp;\r\n\r\nThe statement \u201c<strong>counter--<\/strong>\" may be implemented as:\r\n\r\n&nbsp;\r\n\r\n<strong>register2 = counter <\/strong>\r\n\r\n<strong>register2 = register2 \u2013 1 <\/strong>\r\n\r\n<strong>counter = register2\u00a0<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">If both the producer and consumer attempt to update the buffer concurrently, the assembly language statements may get interleaved. Interleaving depends upon how\u00a0 the producer and consumer processes are scheduled.<\/p>\r\n&nbsp;\r\n\r\nAssume <strong>counter <\/strong>is initially 5.\r\n\r\nOne such interleaving of statements is:\r\n\r\n&nbsp;\r\n\r\nproducer: <strong>register1 = counter <\/strong>(<em>register1 = 5<\/em>)\r\n\r\nproducer: <strong>register1 = register1 + 1 <\/strong>(<em>register1 = 6<\/em>)\r\n\r\nconsumer: <strong>register2 = counter <\/strong>(<em>register2 = 5<\/em>)\r\n\r\nconsumer: <strong>register2 = register2 \u2013 1 <\/strong>(<em>register2 = 4<\/em>)\r\n\r\nproducer: <strong>counter = register1 <\/strong>(<em>counter = 6<\/em>)\r\n\r\nconsumer: <strong>counter = register2 <\/strong>(<em>counter = 4<\/em>)\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Thus, the value of <strong>counter <\/strong>may be either 4 or 6, where the correct result should be 5.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">This situation is called a race condition, that is, the situation where several processes access and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last. To prevent race conditions, concurrent processes must be synchronized. Thus, by understanding the above-mentioned race condition, the critical- section problem can be understood.<\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n\r\n<strong>10.3\u00a0 The Critical-Section Problem\u00a0<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">There are <em>n <\/em>processes that compete to use some shared data. Each process has a code segment, called <em>critical-section<\/em>, in which the shared data is accessed. Hence, the problem is to ensure that, when one process is executing in its critical-section, no other process is allowed to execute in its critical-section. In the above example, the critical-section of code for the producer is <em>counter++ <\/em>and the critical-section of code for the consumer is <em>counter--<\/em>. It should be ensured that when the producer is executing <em>counter<\/em>++, the consumer should not be allowed to access <em>counter<\/em>--.<\/p>\r\n&nbsp;\r\n\r\n<strong>10.3.1\u00a0 Solution to the Critical-Section Problem\u00a0<\/strong>\r\n\r\n&nbsp;\r\n\r\nAny solution to the critical-section problem should satisfy the following conditions:\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Mutual Exclusion<\/strong>: If process <em>P<\/em><em>i <\/em>is executing in its critical-section, then no other processes can be executing in their critical-sections.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Progress<\/strong>: If no process is executing in its critical-section and there exist some processes that wish to enter their critical-section, then the selection of the processes that will enter the critical-section next cannot be postponed indefinitely.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Bounded Waiting<\/strong>: After a process has made a request to enter its critical-section and before that request is granted, a bound must exist on the number of times that \u00a0other processes are allowed to enter their critical-sections.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Many algorithms were designed by researchers to solve the critical-section problem. The initial attempt to solve the critical-section problem provided a two-process solution.<\/p>\r\n&nbsp;\r\n\r\nThe general structure of any process <em>P<\/em>i (the other process being <em>P<\/em>j) is given below:\r\n\r\n&nbsp;\r\n\r\n<strong>d<\/strong><strong>o <\/strong>{\r\n<p style=\"padding-left: 30px\"><em>en<\/em><em>t<\/em><em>ry section<\/em><\/p>\r\n<p style=\"padding-left: 60px\">critical-section<\/p>\r\n<p style=\"padding-left: 30px\"><em>e<\/em><em>x<\/em><em>i<\/em><em>t section<\/em><\/p>\r\n<p style=\"padding-left: 60px\">remainder section<\/p>\r\n} <strong>while (1)<\/strong>;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Before entering the critical-section, each process executes a piece of code called the <em>entry section <\/em>and after coming out of the critical-section, the process executes a piece of code called the <em>exit section<\/em>. The entry and the exit sections of code help in providing synchronization between the two processes. In the solutions provided, the processes share some common variables to synchronize their actions. One of the solutions to the critical- section problem, providing a two-process solution is given in Algorithm 1. The two processes are <em>P<\/em>0 and <em>P<\/em>1.<\/p>\r\n&nbsp;\r\n\r\n<strong>10.3.2\u00a0 Algorithm 1<\/strong>\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0 Shared variables:\r\n<p style=\"padding-left: 30px\">\u2013\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>in<\/strong><strong>t turn<\/strong>; (initially <strong>turn = 0)<\/strong><\/p>\r\n<p style=\"padding-left: 30px\">\u2013\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>turn - i <\/strong>\u00de <em>P<\/em><em>i <\/em>can enter its critical-section<\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022\u00a0 Process <\/span><em style=\"text-align: initial;font-size: 1em\">P<\/em><em style=\"text-align: initial;font-size: 1em\">i<\/em><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">do { while (turn != i) ;<\/span><\/p>\r\n<p style=\"text-align: justify;padding-left: 60px\"><span style=\"text-align: initial;font-size: 1em\">critical-section<\/span><\/p>\r\n<p style=\"text-align: justify;padding-left: 30px\"><span style=\"text-align: initial;font-size: 1em\">turn = j;<\/span><\/p>\r\n<p style=\"text-align: justify;padding-left: 60px\"><span style=\"text-align: initial;font-size: 1em\">remainder section<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">} <\/span><strong style=\"text-align: initial;font-size: 1em\">while (1)<\/strong><span style=\"text-align: initial;font-size: 1em\">;<\/span><\/p>\r\n\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n\r\nThe variable <em>turn <\/em>is common between the two processes. The value of the variable\u00a0<em>turn <\/em>can be initialized to either 0 or 1. In this example, the value of <em>turn <\/em>is initialised to 0.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this algorithm, the entry section is\u00a0 the code \u201c<strong>while (turn != i)<\/strong>;\u201d. Any process before entering the critical-section, executes the entry section. The process checks the value of <em>turn<\/em>. If <em>P<\/em>0 finds that the value of <em>turn <\/em>is 0, it enters critical-section. If <em>P<\/em>0 finds that the value of <em>turn <\/em>is 1, it waits till the value of <em>turn <\/em>becomes 1. Similar is the case with process <em>P<\/em>1. Here, we need to note the semicolon (;) at the end of the while statement.<\/p>\r\n&nbsp;\r\n\r\n\u201cwhile (turn != i) ;\u201d\r\n\r\nis the same as\r\n\r\nwhile(turn!=i)\r\n\r\n{\r\n\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">As long as the condition (turn !=i) in the while loop is true, the process continues to loop in while. Only when the condition becomes false, the process comes out of the while loop.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Therefore, when <em>P<\/em>0 executes the entry section, if the value of <em>turn <\/em>is 0, the condition t<strong>u<\/strong><strong>rn!=0 <\/strong>becomes false and therefore, <em>P<\/em>0 comes out of the while loop and enters critical section. If the value of <em>turn <\/em>is 1, the condition t<strong>urn!=0 <\/strong>is true and therefore, <em>P<\/em>0 continues to execute in the while loop till the value of <em>turn <\/em>is changed to 0.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">When a process comes out of the critical-section, it executes the exit section. In this algorithm, the exit section is the code \u201c<strong>turn = j<\/strong>;\u201d. The process changes the value of <em>turn<\/em>. That is, when process <em>P<\/em>0 is in the critical-section, the value of <em>turn <\/em>would have been 0. When it comes out of the critical-section, it changes the value of <em>turn <\/em>to 1. Similarly, when process <em>P<\/em>1 comes out of the critical-section, it changes the value of <em>turn <\/em>to 0.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">This\u00a0 \u00a0algorithm\u00a0 \u00a0satisfies\u00a0 \u00a0the \u00a0mutual \u00a0exclusion \u00a0condition, \u00a0but \u00a0not \u00a0the \u00a0progress condition. This is explained below:<\/p>\r\n&nbsp;\r\n\r\nAlgorithm 1 \u2013 Mutual Exclusion\r\n\r\n&nbsp;\r\n\r\n<em>P<\/em>0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 turn\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 <em>P<\/em>1\r\n\r\n0\r\n\r\nChecks turn\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Checks turn\r\n\r\nEnters CS\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\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\u00a0 waits\r\n\r\nExits CS\r\n\r\nChanges turn\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Enters CS\r\n\r\n&nbsp;\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">Checks turn<\/span>\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">Waits \u2026\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 \u00a0\u2026<\/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\">According to the mutual exclusion condition, when process <em>P<\/em>0 is in its critical-section, process <em>P<\/em>1 should wait and vice versa. Here, the value of <em>turn <\/em>is initialized to 0. \u00a0Both processes <em>P<\/em>0 and <em>P<\/em>1 \u00a0want to enter critical-section. Hence, they are in the entry sections of\u00a0<span style=\"font-size: 1em;text-align: initial\">their respective codes. Both the processes check the value of <\/span><em style=\"font-size: 1em;text-align: initial\">turn<\/em><span style=\"font-size: 1em;text-align: initial\">. Since the value of <\/span><em style=\"font-size: 1em;text-align: initial\">turn <\/em><span style=\"font-size: 1em;text-align: initial\">is 0, <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">0 comes of the loop (the condition <\/span><strong style=\"font-size: 1em;text-align: initial\">turn!=i <\/strong><span style=\"font-size: 1em;text-align: initial\">becomes false). <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">0 enters its critical-section. At the same time, process <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1 finds the value of <\/span><em style=\"font-size: 1em;text-align: initial\">turn <\/em><span style=\"font-size: 1em;text-align: initial\">to be 0 and waits (<\/span><strong style=\"font-size: 1em;text-align: initial\">turn != i <\/strong><span style=\"font-size: 1em;text-align: initial\">is true for <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1). When process <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">0 comes out of its critical-section, it changes the value of <\/span><em style=\"font-size: 1em;text-align: initial\">turn <\/em><span style=\"font-size: 1em;text-align: initial\">to 1 (<\/span><strong style=\"font-size: 1em;text-align: initial\">turn = j<\/strong><span style=\"font-size: 1em;text-align: initial\">). Now process <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1 comes out of the while loop and enters its critical-section. Thus, it seen that, when process <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">0 is in its critical-section, process <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1 waits, and when process <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1 is in its critical-section, process <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">0 \u00a0waits. Thus, mutual exclusion is satisfied.<\/span><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify\">\r\n\r\n&nbsp;\r\n\r\nAlgorithm 1 \u2013 Progress\r\n\r\n&nbsp;\r\n\r\n<em>P<\/em>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 turn\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<em>P<\/em><sub>1<\/sub>\r\n\r\n0\r\n\r\nChecks turn\r\n\r\nEnters CS\r\n\r\nExits CS\r\n\r\nChanges turn\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 1\r\n\r\n&nbsp;\r\n\r\nP0 wants to enter CS\r\n\r\nChecks turn\r\n\r\nWaits \u2026\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">According to the progress requirement, if there is no process executing in its critical- section and if there are some processes that are wanting to enter their critical-sections, then the selection of the process that will enter the critical-section next cannot be postponed indefinitely. In the example shown above, process <em>P<\/em>0 comes out of its critical-section and changes the value of <em>turn <\/em>to 1. Process <em>P<\/em>1 does not want to enter its critical-section. Now, process <em>P<\/em>0 wants to enter its critical-section again. But <em>P<\/em>0 cannot enter its critical-section because the value of <em>turn <\/em>is\u00a0 1. Unless\u00a0 process <em>P<\/em>1 enters its critical-section and exits its critical-section and then changes the value of turn to 0, <em>P<\/em>0 cannot enter its critical-section. Thus, it is seen that there needs to be a strict alternation between the processes <em>P<\/em>0 and <em>P<\/em>1. After <em>P<\/em>0 comes out of its critical-section, <em>P<\/em>1 should enter its critical-section. Only after that, <em>P<\/em>0 can enter its critical-section the second time. Thus, it is seen that the progress requirement is not satisfied by this algorithm. Since this algorithm does not satisfy all the requirements for a solution to the critical-section problem, it is not a proper solution.<\/p>\r\n&nbsp;\r\n\r\n<strong>10.4\u00a0 Summary\u00a0<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This module discussed the need for process synchronization when cooperating processes share data. The portion of code in each process that\u00a0 involves the access of shared data is called the critical-section of code. The access of the critical-section of each process should be synchronized. Any solution to this critical-section problem should satisfy the mutual exclusion, progress and the bounded waiting requirements. This module discussed one such solution to the critical-section problem where only two processes are involved. We saw that this solution satisfies the mutual exclusion requirement but not the progress requirement.<\/p>\r\n&nbsp;\r\n\r\n<strong>References\u00a0<\/strong>\r\n\r\n&nbsp;\r\n\r\n1.\u00a0 Abraham Silberschatz, Peter B. Galvin, Greg Gagne, \u201cOperating System Concepts\u201d,\u00a0<span style=\"text-align: justify;font-size: 1em\">Ninth Edition, John Wiley &amp; Sons Inc., 2012.<\/span>\r\n\r\n<\/div>\r\n<ol style=\"text-align: justify\" start=\"2\">\r\n \t<li>William Stallings, \u00a0\u201cOperating \u00a0Systems: \u00a0Internals \u00a0and \u00a0Design \u00a0Principles\u201d, \u00a0Seventh Edition, Pearson, 2012.<\/li>\r\n<\/ol>","rendered":"<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>10.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 share data either directly or through files. When two or more processes access shared data concurrently, data inconsistency may result. Maintaining data consistency requires mechanisms that will ensure the orderly execution of cooperating processes. The section of code in each process in which the process accesses shared data is called the critical-section of the process. During the execution of a process, the process may access many such shared data or the same shared data a number of times. Thus, a process may execute critical-sections of code multiple times during its execution. \u00a0This module introduces the critical-section problem and discusses solutions to the critical-section problem.<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p><strong>10.2\u00a0 Bounded-buffer problem\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To understand the critical-section problem, we will revisit the bounded-buffer problem we learnt in an earlier module. The shared-memory solution to the bounded-buffer problem, discussed in the earlier module, allows at most N \u2013 1 items in the buffer at the same time. The solution is as follows:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Shared data:<\/strong><\/p>\n<p>#define BUFFER_SIZE 10<\/p>\n<p>typedef struct {<\/p>\n<p>. . .<\/p>\n<p>} item;<\/p>\n<p>item buffer[BUFFER_SIZE];<\/p>\n<p>int in = 0;<\/p>\n<p>int out = 0;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">There is a buffer of size BUFFER_SIZE (10), which is used by the producer and the consumer processes to place and remove items respectively. There are two variables <em>in <\/em>and <em>out<\/em>, which point to the locations\u00a0 in the buffer where the producer places\u00a0 items and the consumer consumes items respectively. Initially, <em>in <\/em>and <em>out <\/em>point to the same location in the buffer.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>C<\/strong><strong>od<\/strong><strong>e for producer process: <\/strong><\/p>\n<p>item next Produced;<\/p>\n<p>while (1) {<\/p>\n<p style=\"padding-left: 60px\">while (((in + 1) % BUFFER_SIZE) == out) ; \/* do nothing *\/<\/p>\n<p style=\"padding-left: 60px\">buffer[in] = nextProduced;<\/p>\n<p style=\"padding-left: 60px\">in = (in + 1) % BUFFER_SIZE;<\/p>\n<p>}<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">When (<\/span><em style=\"text-align: initial;font-size: 1em\">in <\/em><span style=\"text-align: initial;font-size: 1em\">+ 1) % BUFFER_SIZE == <\/span><em style=\"text-align: initial;font-size: 1em\">out<\/em><span style=\"text-align: initial;font-size: 1em\">, buffer is full. The producer waits if the buffer is full. Else, the producer places the item in the location pointed to by the <\/span><em style=\"text-align: initial;font-size: 1em\">in <\/em><span style=\"text-align: initial;font-size: 1em\">variable and increments the <\/span><em style=\"text-align: initial;font-size: 1em\">in <\/em><span style=\"text-align: initial;font-size: 1em\">variable.<\/span><\/p>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p>Code for consumer process:Item nextConsumed;<\/p>\n<p>while (1) {<\/p>\n<p style=\"padding-left: 60px\">while (in == out); \/* do nothing *\/<\/p>\n<p style=\"padding-left: 60px\">nextConsumed = buffer[out];<\/p>\n<p style=\"padding-left: 60px\">out = (out + 1) % BUFFER_SIZE;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The buffer is empty when <em>in <\/em>== <em>out<\/em>. The consumer waits when the buffer is empty. If the buffer is not empty, the consumer consumes the item pointed to by the <em>out <\/em>variable and increments the <em>out <\/em>variable.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Suppose the consumer had not consumed any data. Then, the initial location will be pointed to by the <em>out <\/em>pointer. When the <em>in <\/em>pointer is pointing to the N \u2013 1th location, the condition \u201c(<em>in <\/em>+ 1) % BUFFER_SIZE == <em>out<\/em>\u201d will be true and the producer will wait. Thus, this solution uses only N \u2013 1 locations of the buffer.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A solution, where all N buffers are used is not simple. A solution is possible when we modify the producer-consumer code by adding a variable <em>counter<\/em>, initialized to 0 and incrementing it each time a new item is added to the buffer. The code for the solution to the bounded buffer problem using the <em>counter <\/em>variable is given below:<\/p>\n<p>&nbsp;<\/p>\n<p>#define BUFFER_SIZE 10<\/p>\n<p>typedef struct {\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 . . .<\/p>\n<p>} item;<\/p>\n<p>item buffer[BUFFER_SIZE];<\/p>\n<p>int in = 0;<\/p>\n<p>int out = 0;<\/p>\n<p>int counter = 0;<\/p>\n<p><strong>\/*Producer process<\/strong><\/p>\n<p style=\"padding-left: 30px\">item nextProduced;<\/p>\n<p style=\"padding-left: 30px\">while (1) {<\/p>\n<p style=\"padding-left: 60px\">while (counter == BUFFER_SIZE); \u00a0\/* do nothing *\/<\/p>\n<p style=\"padding-left: 60px\">buffer[in] = nextProduced;<\/p>\n<p style=\"padding-left: 60px\">in = (in + 1) % BUFFER_SIZE;<\/p>\n<p style=\"padding-left: 60px\">counter++;<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p><strong>\/*Consumer process<\/strong><\/p>\n<p style=\"padding-left: 30px\">item nextConsumed; while (1) {<\/p>\n<p style=\"padding-left: 60px\">while (counter == 0); \u00a0\/* do nothing *\/<\/p>\n<p style=\"padding-left: 60px\">nextConsumed = buffer[out];<\/p>\n<p style=\"padding-left: 60px\">out = (out + 1) % BUFFER_SIZE;<\/p>\n<p style=\"padding-left: 60px\">counter&#8211;;<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">In this solution, the variable <\/span><em style=\"text-align: initial;font-size: 1em\">counter is <\/em><span style=\"text-align: initial;font-size: 1em\">shared between the producer and the consumer. Whenever the producer places an item in the buffer, it increases the value of the variable. When the consumer consumes an item from the buffer, it decreases the value of the <\/span><em style=\"text-align: initial;font-size: 1em\">counter <\/em><span style=\"text-align: initial;font-size: 1em\">variable. Thus, the value of the variable <\/span><em style=\"text-align: initial;font-size: 1em\">counter <\/em><span style=\"text-align: initial;font-size: 1em\">maintains the count of the items present in the buffer. Now, we will see how the increment and decrement operations performed on the variable <\/span><em style=\"text-align: initial;font-size: 1em\">counter <\/em><span style=\"text-align: initial;font-size: 1em\">can lead to inconsistencies.<\/span><\/p>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The statements \u201c<strong>counter++;\u201d <\/strong>and <strong>\u201ccounter&#8211;;\u201d <\/strong>must be performed <em>atomically<\/em>. Atomic operation means an operation that completes in its entirety without interruption. The reason for this is explained below:<\/p>\n<p>&nbsp;<\/p>\n<p>The statement \u201c<strong>counter++<\/strong>\u201d may be implemented in machine language as:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>register1 = counter <\/strong><\/p>\n<p><strong>register1 = register1 + 1 <\/strong><\/p>\n<p><strong>counter = register1\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Thus, it is seen that the single statement <strong>counter++ <\/strong>in the producer\u2019s code is not executed as a single instruction. Rather, it is executed in three different instructions. Similarly, the single statement <strong>counter&#8211; <\/strong>in the consumer\u2019s code is implemented in three different instructions.<\/p>\n<p>&nbsp;<\/p>\n<p>The statement \u201c<strong>counter&#8211;<\/strong>&#8221; may be implemented as:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>register2 = counter <\/strong><\/p>\n<p><strong>register2 = register2 \u2013 1 <\/strong><\/p>\n<p><strong>counter = register2\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">If both the producer and consumer attempt to update the buffer concurrently, the assembly language statements may get interleaved. Interleaving depends upon how\u00a0 the producer and consumer processes are scheduled.<\/p>\n<p>&nbsp;<\/p>\n<p>Assume <strong>counter <\/strong>is initially 5.<\/p>\n<p>One such interleaving of statements is:<\/p>\n<p>&nbsp;<\/p>\n<p>producer: <strong>register1 = counter <\/strong>(<em>register1 = 5<\/em>)<\/p>\n<p>producer: <strong>register1 = register1 + 1 <\/strong>(<em>register1 = 6<\/em>)<\/p>\n<p>consumer: <strong>register2 = counter <\/strong>(<em>register2 = 5<\/em>)<\/p>\n<p>consumer: <strong>register2 = register2 \u2013 1 <\/strong>(<em>register2 = 4<\/em>)<\/p>\n<p>producer: <strong>counter = register1 <\/strong>(<em>counter = 6<\/em>)<\/p>\n<p>consumer: <strong>counter = register2 <\/strong>(<em>counter = 4<\/em>)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Thus, the value of <strong>counter <\/strong>may be either 4 or 6, where the correct result should be 5.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This situation is called a race condition, that is, the situation where several processes access and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last. To prevent race conditions, concurrent processes must be synchronized. Thus, by understanding the above-mentioned race condition, the critical- section problem can be understood.<\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p><strong>10.3\u00a0 The Critical-Section Problem\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">There are <em>n <\/em>processes that compete to use some shared data. Each process has a code segment, called <em>critical-section<\/em>, in which the shared data is accessed. Hence, the problem is to ensure that, when one process is executing in its critical-section, no other process is allowed to execute in its critical-section. In the above example, the critical-section of code for the producer is <em>counter++ <\/em>and the critical-section of code for the consumer is <em>counter&#8211;<\/em>. It should be ensured that when the producer is executing <em>counter<\/em>++, the consumer should not be allowed to access <em>counter<\/em>&#8211;.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>10.3.1\u00a0 Solution to the Critical-Section Problem\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Any solution to the critical-section problem should satisfy the following conditions:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Mutual Exclusion<\/strong>: If process <em>P<\/em><em>i <\/em>is executing in its critical-section, then no other processes can be executing in their critical-sections.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Progress<\/strong>: If no process is executing in its critical-section and there exist some processes that wish to enter their critical-section, then the selection of the processes that will enter the critical-section next cannot be postponed indefinitely.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Bounded Waiting<\/strong>: After a process has made a request to enter its critical-section and before that request is granted, a bound must exist on the number of times that \u00a0other processes are allowed to enter their critical-sections.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Many algorithms were designed by researchers to solve the critical-section problem. The initial attempt to solve the critical-section problem provided a two-process solution.<\/p>\n<p>&nbsp;<\/p>\n<p>The general structure of any process <em>P<\/em>i (the other process being <em>P<\/em>j) is given below:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>d<\/strong><strong>o <\/strong>{<\/p>\n<p style=\"padding-left: 30px\"><em>en<\/em><em>t<\/em><em>ry section<\/em><\/p>\n<p style=\"padding-left: 60px\">critical-section<\/p>\n<p style=\"padding-left: 30px\"><em>e<\/em><em>x<\/em><em>i<\/em><em>t section<\/em><\/p>\n<p style=\"padding-left: 60px\">remainder section<\/p>\n<p>} <strong>while (1)<\/strong>;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Before entering the critical-section, each process executes a piece of code called the <em>entry section <\/em>and after coming out of the critical-section, the process executes a piece of code called the <em>exit section<\/em>. The entry and the exit sections of code help in providing synchronization between the two processes. In the solutions provided, the processes share some common variables to synchronize their actions. One of the solutions to the critical- section problem, providing a two-process solution is given in Algorithm 1. The two processes are <em>P<\/em>0 and <em>P<\/em>1.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>10.3.2\u00a0 Algorithm 1<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0 Shared variables:<\/p>\n<p style=\"padding-left: 30px\">\u2013\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>in<\/strong><strong>t turn<\/strong>; (initially <strong>turn = 0)<\/strong><\/p>\n<p style=\"padding-left: 30px\">\u2013\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>turn &#8211; i <\/strong>\u00de <em>P<\/em><em>i <\/em>can enter its critical-section<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022\u00a0 Process <\/span><em style=\"text-align: initial;font-size: 1em\">P<\/em><em style=\"text-align: initial;font-size: 1em\">i<\/em><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">do { while (turn != i) ;<\/span><\/p>\n<p style=\"text-align: justify;padding-left: 60px\"><span style=\"text-align: initial;font-size: 1em\">critical-section<\/span><\/p>\n<p style=\"text-align: justify;padding-left: 30px\"><span style=\"text-align: initial;font-size: 1em\">turn = j;<\/span><\/p>\n<p style=\"text-align: justify;padding-left: 60px\"><span style=\"text-align: initial;font-size: 1em\">remainder section<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">} <\/span><strong style=\"text-align: initial;font-size: 1em\">while (1)<\/strong><span style=\"text-align: initial;font-size: 1em\">;<\/span><\/p>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p>The variable <em>turn <\/em>is common between the two processes. The value of the variable\u00a0<em>turn <\/em>can be initialized to either 0 or 1. In this example, the value of <em>turn <\/em>is initialised to 0.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this algorithm, the entry section is\u00a0 the code \u201c<strong>while (turn != i)<\/strong>;\u201d. Any process before entering the critical-section, executes the entry section. The process checks the value of <em>turn<\/em>. If <em>P<\/em>0 finds that the value of <em>turn <\/em>is 0, it enters critical-section. If <em>P<\/em>0 finds that the value of <em>turn <\/em>is 1, it waits till the value of <em>turn <\/em>becomes 1. Similar is the case with process <em>P<\/em>1. Here, we need to note the semicolon (;) at the end of the while statement.<\/p>\n<p>&nbsp;<\/p>\n<p>\u201cwhile (turn != i) ;\u201d<\/p>\n<p>is the same as<\/p>\n<p>while(turn!=i)<\/p>\n<p>{<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As long as the condition (turn !=i) in the while loop is true, the process continues to loop in while. Only when the condition becomes false, the process comes out of the while loop.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Therefore, when <em>P<\/em>0 executes the entry section, if the value of <em>turn <\/em>is 0, the condition t<strong>u<\/strong><strong>rn!=0 <\/strong>becomes false and therefore, <em>P<\/em>0 comes out of the while loop and enters critical section. If the value of <em>turn <\/em>is 1, the condition t<strong>urn!=0 <\/strong>is true and therefore, <em>P<\/em>0 continues to execute in the while loop till the value of <em>turn <\/em>is changed to 0.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">When a process comes out of the critical-section, it executes the exit section. In this algorithm, the exit section is the code \u201c<strong>turn = j<\/strong>;\u201d. The process changes the value of <em>turn<\/em>. That is, when process <em>P<\/em>0 is in the critical-section, the value of <em>turn <\/em>would have been 0. When it comes out of the critical-section, it changes the value of <em>turn <\/em>to 1. Similarly, when process <em>P<\/em>1 comes out of the critical-section, it changes the value of <em>turn <\/em>to 0.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This\u00a0 \u00a0algorithm\u00a0 \u00a0satisfies\u00a0 \u00a0the \u00a0mutual \u00a0exclusion \u00a0condition, \u00a0but \u00a0not \u00a0the \u00a0progress condition. This is explained below:<\/p>\n<p>&nbsp;<\/p>\n<p>Algorithm 1 \u2013 Mutual Exclusion<\/p>\n<p>&nbsp;<\/p>\n<p><em>P<\/em>0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 turn\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 <em>P<\/em>1<\/p>\n<p>0<\/p>\n<p>Checks turn\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Checks turn<\/p>\n<p>Enters CS\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\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\u00a0 waits<\/p>\n<p>Exits CS<\/p>\n<p>Changes turn\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Enters CS<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">Checks turn<\/span><\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">Waits \u2026\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 \u00a0\u2026<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">According to the mutual exclusion condition, when process <em>P<\/em>0 is in its critical-section, process <em>P<\/em>1 should wait and vice versa. Here, the value of <em>turn <\/em>is initialized to 0. \u00a0Both processes <em>P<\/em>0 and <em>P<\/em>1 \u00a0want to enter critical-section. Hence, they are in the entry sections of\u00a0<span style=\"font-size: 1em;text-align: initial\">their respective codes. Both the processes check the value of <\/span><em style=\"font-size: 1em;text-align: initial\">turn<\/em><span style=\"font-size: 1em;text-align: initial\">. Since the value of <\/span><em style=\"font-size: 1em;text-align: initial\">turn <\/em><span style=\"font-size: 1em;text-align: initial\">is 0, <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">0 comes of the loop (the condition <\/span><strong style=\"font-size: 1em;text-align: initial\">turn!=i <\/strong><span style=\"font-size: 1em;text-align: initial\">becomes false). <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">0 enters its critical-section. At the same time, process <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1 finds the value of <\/span><em style=\"font-size: 1em;text-align: initial\">turn <\/em><span style=\"font-size: 1em;text-align: initial\">to be 0 and waits (<\/span><strong style=\"font-size: 1em;text-align: initial\">turn != i <\/strong><span style=\"font-size: 1em;text-align: initial\">is true for <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1). When process <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">0 comes out of its critical-section, it changes the value of <\/span><em style=\"font-size: 1em;text-align: initial\">turn <\/em><span style=\"font-size: 1em;text-align: initial\">to 1 (<\/span><strong style=\"font-size: 1em;text-align: initial\">turn = j<\/strong><span style=\"font-size: 1em;text-align: initial\">). Now process <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1 comes out of the while loop and enters its critical-section. Thus, it seen that, when process <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">0 is in its critical-section, process <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1 waits, and when process <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">1 is in its critical-section, process <\/span><em style=\"font-size: 1em;text-align: initial\">P<\/em><span style=\"font-size: 1em;text-align: initial\">0 \u00a0waits. Thus, mutual exclusion is satisfied.<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify\">\n<p>&nbsp;<\/p>\n<p>Algorithm 1 \u2013 Progress<\/p>\n<p>&nbsp;<\/p>\n<p><em>P<\/em>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 turn\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<em>P<\/em><sub>1<\/sub><\/p>\n<p>0<\/p>\n<p>Checks turn<\/p>\n<p>Enters CS<\/p>\n<p>Exits CS<\/p>\n<p>Changes turn\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 1<\/p>\n<p>&nbsp;<\/p>\n<p>P0 wants to enter CS<\/p>\n<p>Checks turn<\/p>\n<p>Waits \u2026<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">According to the progress requirement, if there is no process executing in its critical- section and if there are some processes that are wanting to enter their critical-sections, then the selection of the process that will enter the critical-section next cannot be postponed indefinitely. In the example shown above, process <em>P<\/em>0 comes out of its critical-section and changes the value of <em>turn <\/em>to 1. Process <em>P<\/em>1 does not want to enter its critical-section. Now, process <em>P<\/em>0 wants to enter its critical-section again. But <em>P<\/em>0 cannot enter its critical-section because the value of <em>turn <\/em>is\u00a0 1. Unless\u00a0 process <em>P<\/em>1 enters its critical-section and exits its critical-section and then changes the value of turn to 0, <em>P<\/em>0 cannot enter its critical-section. Thus, it is seen that there needs to be a strict alternation between the processes <em>P<\/em>0 and <em>P<\/em>1. After <em>P<\/em>0 comes out of its critical-section, <em>P<\/em>1 should enter its critical-section. Only after that, <em>P<\/em>0 can enter its critical-section the second time. Thus, it is seen that the progress requirement is not satisfied by this algorithm. Since this algorithm does not satisfy all the requirements for a solution to the critical-section problem, it is not a proper solution.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>10.4\u00a0 Summary\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This module discussed the need for process synchronization when cooperating processes share data. The portion of code in each process that\u00a0 involves the access of shared data is called the critical-section of code. The access of the critical-section of each process should be synchronized. Any solution to this critical-section problem should satisfy the mutual exclusion, progress and the bounded waiting requirements. This module discussed one such solution to the critical-section problem where only two processes are involved. We saw that this solution satisfies the mutual exclusion requirement but not the progress requirement.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>References\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0 Abraham Silberschatz, Peter B. Galvin, Greg Gagne, \u201cOperating System Concepts\u201d,\u00a0<span style=\"text-align: justify;font-size: 1em\">Ninth Edition, John Wiley &amp; Sons Inc., 2012.<\/span><\/p>\n<\/div>\n<ol style=\"text-align: justify\" start=\"2\">\n<li>William Stallings, \u00a0\u201cOperating \u00a0Systems: \u00a0Internals \u00a0and \u00a0Design \u00a0Principles\u201d, \u00a0Seventh Edition, Pearson, 2012.<\/li>\n<\/ol>\n","protected":false},"author":4,"menu_order":7,"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-90","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\/90","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\/90\/revisions"}],"predecessor-version":[{"id":412,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapters\/90\/revisions\/412"}],"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\/90\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/media?parent=90"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/pressbooks\/v2\/chapter-type?post=90"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/contributor?post=90"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp3\/wp-json\/wp\/v2\/license?post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}