Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 3 The Critical Section Problem. 2 Producer - Consumer Problem Buffer is shared (ie., it is a shared variable) Buffer is shared (ie., it is a.

Similar presentations


Presentation on theme: "1 Chapter 3 The Critical Section Problem. 2 Producer - Consumer Problem Buffer is shared (ie., it is a shared variable) Buffer is shared (ie., it is a."— Presentation transcript:

1 1 Chapter 3 The Critical Section Problem

2 2 Producer - Consumer Problem Buffer is shared (ie., it is a shared variable) Buffer is shared (ie., it is a shared variable) Producer Process Consumer Process Produce Put in bufferConsume Get from buffer BUFFER

3 3 Progress in time….. Both processes are started at the same time and consumer uses some old value initially Both processes are started at the same time and consumer uses some old value initially 3 instead of 2! Producer Consumer 12c2 p1p4p3p24321 t Buffer c1

4 4 A Race Condition Because of the timing and which process starts first Because of the timing and which process starts first There is a chance that different executions may end up with different results There is a chance that different executions may end up with different results That is, because of interleaving, we can not predict what will happen next when processes are executing That is, because of interleaving, we can not predict what will happen next when processes are executing

5 5 Why Processes Need to Communicate? To synchronize their executions To synchronize their executions To exchange data and information To exchange data and information

6 6 Critical Sections Critical Section Critical Section A section of code in which a process accesses and modifies shared variables A section of code in which a process accesses and modifies shared variables Mutual Exclusion Mutual Exclusion A method of prevention to ensure that one (or a specified number) of process(es) is/are in a critical section A method of prevention to ensure that one (or a specified number) of process(es) is/are in a critical section

7 7 Mutual Exclusion Problem Program producerconsumer; Procedure producer; beginrepeatproduce;putinbuffer; until false; End; Procedure consumer; Beginrepeat getfrombuffer; consume; until false; End; Begin (* main program *) cobegin producer; consumer coend End. critical section

8 8 Critical Section A critical section in the procedure of a process is the section in which the process refers and changes common variables (which are shared by other processes) A critical section in the procedure of a process is the section in which the process refers and changes common variables (which are shared by other processes) There may be several critical sections operating on the same or different common variables in a process There may be several critical sections operating on the same or different common variables in a process

9 9 Rules to Form Critical Sections No two processes may be simultaneously inside their CS - (Rule of Mutual Exclusion) No two processes may be simultaneously inside their CS - (Rule of Mutual Exclusion) No assumptions are made about relative process speeds or number of CPUs - (Race Conditions) No assumptions are made about relative process speeds or number of CPUs - (Race Conditions) No process can remain inside a CS indefiniely (CS must be small in code and it’s execution must take minimal time) - (Rule of Deadlock and Starvation) No process can remain inside a CS indefiniely (CS must be small in code and it’s execution must take minimal time) - (Rule of Deadlock and Starvation) A process outside a CS should not block other processes - (Rule of Deadlock and Starvation) A process outside a CS should not block other processes - (Rule of Deadlock and Starvation) No process should wait forever before entering its CS (When a process leaves a CS, the process manager must activate a waiting process to enter its CS. Processes waiting to enter a CS usually wait in a queue maintained in a FIFO manner or by a priority rule) - (Rule of Starvation) No process should wait forever before entering its CS (When a process leaves a CS, the process manager must activate a waiting process to enter its CS. Processes waiting to enter a CS usually wait in a queue maintained in a FIFO manner or by a priority rule) - (Rule of Starvation)

10 10 Correctness Specification Correctness specifications required for a solution are : Correctness specifications required for a solution are : Mutual exclusion : Statements from the critical sections of two or more processes must not be interleaved Mutual exclusion : Statements from the critical sections of two or more processes must not be interleaved Freedom from Deadlock : If some processes are trying to enter their critical sections, then one of them must eventually succeed Freedom from Deadlock : If some processes are trying to enter their critical sections, then one of them must eventually succeed Freedom from Starvation : If any process tries to enter its critical section, then that process must eventually succeed Freedom from Starvation : If any process tries to enter its critical section, then that process must eventually succeed

11 11 Synchronization A synchronization mechanism (synchronization primatives) must be provided to handle the critical section problem A synchronization mechanism (synchronization primatives) must be provided to handle the critical section problem This synchronization mechanism consists of additional statements that are placed before and after a critical section (preprotocol and postprotocol) This synchronization mechanism consists of additional statements that are placed before and after a critical section (preprotocol and postprotocol)

12 12 First Attempt (Critical Section Problem for two processes) Program mutualexclusion; Var turn : integer; Procedure P; beginrepeat non-critical section; repeat (* do nothing *) until turn = 1; (* busy wait while P2 is in CS*) critical section; turn := 2; until false; End; Procedure Q; Beginrepeat non-critical section; repeat (* do nothing *) until turn = 2;(* busy wait while P1 is in CS *) critical section; turn := 1; until false; End; Begin (* main program *) turn := 1; (* we start with P1 *) cobegin P; Q coend End.

13 13 Comments Do (* nothing *) is implemented by timeslicing (process loops in the while instruction ) Do (* nothing *) is implemented by timeslicing (process loops in the while instruction ) Solution satisfies mutual exclusion, since only one process can be in CS Solution satisfies mutual exclusion, since only one process can be in CS Deadlock is not possible. Both processes can never be stuck at the while statement Deadlock is not possible. Both processes can never be stuck at the while statement Process starvation is possible, if a process spends a lot of time in its non-critical section. Here we assume that the other process is busy waiting to enter the critical section. If processes spend finite times in non-critical sections then there will be no starvation Process starvation is possible, if a process spends a lot of time in its non-critical section. Here we assume that the other process is busy waiting to enter the critical section. If processes spend finite times in non-critical sections then there will be no starvation

14 14 Drawback The processes are not loosely connected. That is, the right to enter a CS is being explicitly passed from one process to another The processes are not loosely connected. That is, the right to enter a CS is being explicitly passed from one process to another Suppose process P is waiting to enter the CS. If something happens to process Q while inside the non-critical section, process P is hopelessly deadlocked after the next pass Suppose process P is waiting to enter the CS. If something happens to process Q while inside the non-critical section, process P is hopelessly deadlocked after the next pass

15 15 First Attempt Using Book’s Convention Await is blocking wait not a busy wait loop Await is blocking wait not a busy wait loop

16 16 Proving Correctness with State Diagrams In the first attempt, states are triples of the form (p i,q j,turn) In the first attempt, states are triples of the form (p i,q j,turn) Variables used in the non-critical and critical sections are assumed to be distinct from the variables used in the protocols so that they have no effect at the correctness of the solution. So, they are left out of the states Variables used in the non-critical and critical sections are assumed to be distinct from the variables used in the protocols so that they have no effect at the correctness of the solution. So, they are left out of the states The mutual exclusion correctness property holds if the set of all accessible states does not contain a state of the form (p3,q3,turn) for some value of turn, because p3 and q3 are the labels of the critical sections The mutual exclusion correctness property holds if the set of all accessible states does not contain a state of the form (p3,q3,turn) for some value of turn, because p3 and q3 are the labels of the critical sections

17 17 State Diagrams How many states can be in a state diagram? How many states can be in a state diagram? Suppose the algorithm has N processes with n i statements in process i, and M variables where variable j has m j possible values. Suppose the algorithm has N processes with n i statements in process i, and M variables where variable j has m j possible values. The number of possible states is The number of possible states is n 1 x... x n N x m 1 x... x m M For the first attempt, the number of states is 4x4x2=32 since we have 4 statements per process and turn can be 1 or 2 For the first attempt, the number of states is 4x4x2=32 since we have 4 statements per process and turn can be 1 or 2

18 18 State Diagram of the First Attempt The initial state is (p1,q1,1) The initial state is (p1,q1,1) Lefthand side is P executing, righthand side is Q executing Lefthand side is P executing, righthand side is Q executing The incremental construction terminates after 16 of the 32 possible states have been constructed The incremental construction terminates after 16 of the 32 possible states have been constructed We do not have states (p3,q3,1) or (p3,q3,2) so mutual exclusion property for correctness holds for the first attempt We do not have states (p3,q3,1) or (p3,q3,2) so mutual exclusion property for correctness holds for the first attempt

19 19 Abbreviating the State Diagram Statements executed in the non-critical and critical sections are irrelevant to the correctness of the synchronization algorithm. So, they are eliminated Statements executed in the non-critical and critical sections are irrelevant to the correctness of the synchronization algorithm. So, they are eliminated In fact, you can think of p as In fact, you can think of p as p1: non-critical section; await turn = 1 p1: non-critical section; await turn = 1 p2: critical section; turn <- 2 p2: critical section; turn <- 2 Now we have 4 states to consider Now we have 4 states to consider

20 20 Correctness of the First Attempt Mutual exclusion property holds (see the state diagram) Mutual exclusion property holds (see the state diagram) Is the algorithm deadlock free? Is the algorithm deadlock free? Which means : If some processes are trying to enter their critical sections, then one of them must eventually succeed Which means : If some processes are trying to enter their critical sections, then one of them must eventually succeed

21 21 Is it Deadlock Free? A process is trying to enter its CS if it is executing p1 or q1 A process is trying to enter its CS if it is executing p1 or q1 Consider the initial state. Turn is 1, which means p1 passes through and q1 loops on Consider the initial state. Turn is 1, which means p1 passes through and q1 loops on Next state is the lower right. Here p2 executes CS and eventually sets turn to 2. While it is in the CS, Q is executing q1 Next state is the lower right. Here p2 executes CS and eventually sets turn to 2. While it is in the CS, Q is executing q1 Next state is upper left in which Q enters its CS. Next state is upper left in which Q enters its CS. Turn value decides which process should enter the CS Turn value decides which process should enter the CS Both processes eventually enter their CS in a mutually exclusive manner. Hence, the property of freedom from deadlock is satisfied Both processes eventually enter their CS in a mutually exclusive manner. Hence, the property of freedom from deadlock is satisfied

22 22 Is it Starvation Free? Consider a section of the original state diagram. NCS stands for a non-critical section Consider a section of the original state diagram. NCS stands for a non-critical section The state on the lower left is the one in which p2 is waiting to enter its CS and q1 is in the NCS The state on the lower left is the one in which p2 is waiting to enter its CS and q1 is in the NCS If process Q decides to stay in its NCS (or broken down), P can not enter its CS so starve If process Q decides to stay in its NCS (or broken down), P can not enter its CS so starve Hence, freedom from starvation property does not hold for this solution Hence, freedom from starvation property does not hold for this solution Second attempt tries to solve it Second attempt tries to solve it

23 23 Second Attempt Each process has its own flag which is true when process is in CS (p3, q3 and p5, q5) Each process has its own flag which is true when process is in CS (p3, q3 and p5, q5) The other process waits until this process leaves the CS (p2, q2) The other process waits until this process leaves the CS (p2, q2) If a process halts in its NCS then the other proceeds independently. If a process halts in its NCS then the other proceeds independently.

24 24 Second Attempt (Abbreviated)

25 25 State Diagram for Abbreviated Algorithm The last state (p3,q3, true,true) means that both processes enter CS at the same time The last state (p3,q3, true,true) means that both processes enter CS at the same time Mutual exclusion property does not hold Mutual exclusion property does not hold

26 26 A Scenario Showing that Mutual Exclusion Does Not Hold

27 27 Second Attempt (for jBACI) Program secondattempt; Var c1, c2 : integer; Procedure p; Beginrepeat while c2 = 0 do; c1 := 0; CS; c1 := 1; NCS; until false; End; Procedure q; Beginrepeat while c1 = 0 do; c2 := 0; CS; c2 := 1; NCS; until false; End; Begin (* main program *) c1:= 1; c2:= 1; (* Both processes are not in their CS *) cobegin p; q coend End. loop on while p2 is in CS process is entering the CS process leaves the CS if p2 breaks down here, p1 can still execute since it has its own flag c1

28 28 Comments & Correctness Each process periodically checks the other’s common variable cx. If the other process is not in CS (ie., cx = 1); the other process enters its CS, sets its flag c to 0 to indicate that it will enter its CS Each process periodically checks the other’s common variable cx. If the other process is not in CS (ie., cx = 1); the other process enters its CS, sets its flag c to 0 to indicate that it will enter its CS This program may not work correctly if events are as shown in the table This program may not work correctly if events are as shown in the table We have no mutual exclusion so deadlock! We have no mutual exclusion so deadlock! c1c2 initially11 P1 checks c2 11 P2 checks c1 11 P1 sets c1 01 P2 sets c2 00 P1 enters CS 00 P2 enters CS 00 Both processes are in CS 00

29 29 Third Attempt This algorithm is a modification of the second attempt This algorithm is a modification of the second attempt Here, “await” statements become a part of the CS Here, “await” statements become a part of the CS Algorithm satisfies mutual exclusion (prove it yourself!) Algorithm satisfies mutual exclusion (prove it yourself!)

30 30 Scenario for a Deadlock Both processes are locked at p3 and q3 Both processes are locked at p3 and q3 The situation is a deadlock case but we may call it a livelock since processes are actively executing statements but nothing useful is done The situation is a deadlock case but we may call it a livelock since processes are actively executing statements but nothing useful is done

31 31 Third Attempt (In jBACI) Program thirdattempt; Var c1, c2 : integer; Procedure p1; Beginrepeat c1 := 0; while c2 = 0 do; CS; c1 := 1; NCS; until false; until false;End; Procedure p2; Beginrepeat c2 := 0; while c1 = 0 do; CS; c2 := 1; NCS; until false; until false;End; Begin (* main program *) c1:= 1; c2:= 1; (* Both processes are not in their CS *) cobegin p1; p2, coend End. process is entering its CS loop on while p2 is in CS process leaves the CS

32 32 Comments & Correctness The third solution is a modification of the 2’nd attempt. Here, the variable cx (request to enter CS) is set before checking the other process whether it is in CS or not The third solution is a modification of the 2’nd attempt. Here, the variable cx (request to enter CS) is set before checking the other process whether it is in CS or not The solution is not correct as shown in the events of the table The solution is not correct as shown in the events of the table c1c2 initially11 P1 sets c1 01 P2 sets c2 00 P1 enters CS 00 P2 enters CS 00 Both processes are in CS 00

33 33 Dekker’s Algorithm

34 34 Dekker’s Algorithm (in jBACI) Program Dekker; Var turn: integer; wantp, wantq:boolean; Procedure p; Beginrepeat wantp := true; while wantq do if turn = 2 then begin wantp:= false; repeat until turn = 1; wantp:=true end; CS; turn := 2; wantp:= false; NCS; until false; until false;End; Procedure q; Beginrepeat wantq2 := true; while wantp do if turn = 1 then begin wantq:= false; repeat until turn = 2; wantq:=false end; CS; turn := 1; wantq := false; NCS; until false; until false;End; Begin (* main program *) turn:= 1; wantp:= false; wantq:= false; cobegin p; q coend End.

35 35 Explanation of the Algorithm Procedure p; Beginrepeat wantp := true;(* 1 *) while wantq do if turn = 2 then begin wantp:= false; repeat until turn = 1; wantp:=true end; (* 2 *) CS; turn := 2;(* 3 *) wantp := false;(* 4 *) NCS; until false; until false;End; 1. Process makes a request to enter CS 2. If the other process had made a request to enter CS before and if it is its turn then Take back the request to enter CS by setting the “want” variable to false” Take back the request to enter CS by setting the “want” variable to false” Wait for the other process to exit CS and change the turn variable Wait for the other process to exit CS and change the turn variable Make a new request to enter CS and then enter CS Make a new request to enter CS and then enter CS 3. Flip the turn variable so that now the other process can enter CS 4. Set “want” variable to false to indicate that the process is now out of CS

36 36 Comments Explicit control of transfer by a turn variable. Hence; turn = 1 means P’s turn, turn = 2 Q’s turn Explicit control of transfer by a turn variable. Hence; turn = 1 means P’s turn, turn = 2 Q’s turn If a process is blocked, the other process can still go If a process is blocked, the other process can still go Dekker’s algorithm is correct (prove it!) Dekker’s algorithm is correct (prove it!) It satisfies the mutual exclusion property It satisfies the mutual exclusion property It is free from deadlock and starvation It is free from deadlock and starvation


Download ppt "1 Chapter 3 The Critical Section Problem. 2 Producer - Consumer Problem Buffer is shared (ie., it is a shared variable) Buffer is shared (ie., it is a."

Similar presentations


Ads by Google