1 chapter 3 the critical section problem. 2 producer - consumer problem buffer is shared (ie., it is...

36
1 Chapter 3 Chapter 3 The Critical The Critical Section Problem Section Problem

Upload: annabella-carter

Post on 16-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

1

Chapter 3Chapter 3

The Critical Section The Critical Section ProblemProblem

Page 2: 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

2

Producer - Consumer Producer - Consumer ProblemProblem

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 buffer Consume

Get from buffer

BUFFER

Page 3: 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

3

Progress in time…..Progress in time…..

Both processes are started at the same time Both processes are started at the same time and consumer uses some old value initiallyand consumer uses some old value initially

3 instead of 2!

Producer

Consumer1 2 c2

p1 p4p3p2 4321

t

Buffer

c1

Page 4: 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

4

A Race ConditionA Race Condition

Because of the timing and which Because of the timing and which process starts firstprocess starts first

There is a chance that There is a chance that different different executions may end up with different executions may end up with different resultsresults

That is, because of interleaving, we can That is, because of interleaving, we can not predict what will happen next when not predict what will happen next when processes are executingprocesses are executing

Page 5: 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

5

Why Processes Need to Why Processes Need to Communicate?Communicate?

To synchronize their executionsTo synchronize their executions

To exchange data and informationTo exchange data and information

Page 6: 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

6

Critical SectionsCritical Sections Critical SectionCritical Section

A section of code in which A section of code in which aa process process accesses and modifies accesses and modifies shared variablesshared variables

Mutual ExclusionMutual Exclusion A method of preventA method of preventionion toto ensure ensure that that

one (or a specified number) of one (or a specified number) of processprocess((eses)) is/is/are in a critical sectionare in a critical section

Page 7: 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

7

Mutual Exclusion Mutual Exclusion ProblemProblemProgram producerconsumer;Program producerconsumer;

Procedure producer;Procedure producer;beginbegin

repeat repeat produce;produce;

putinbuffer; putinbuffer; until false;until false;

End;End;Procedure consumer;Procedure consumer;BeginBegin

repeatrepeat

getfrombuffer;getfrombuffer;consume;consume;

until false;until false;End;End;Begin (* main program *)Begin (* main program *)

cobegin producer; cobegin producer; consumer coendconsumer coend

End.End.

critical section

critical section

Page 8: 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

8

Critical SectionCritical Section

A A critical sectioncritical section in the procedure of a in the procedure of a process is the section in which the process is the section in which the process refers and changes common process refers and changes common variables (which are shared by other variables (which are shared by other processes)processes)

There may be several critical sections There may be several critical sections operating on the same or different operating on the same or different common variables in a processcommon variables in a process

Page 9: 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

9

Rules to Form Critical Rules to Form Critical SectionsSections No two processes may be simultaneously inside No two processes may be simultaneously inside

their CS their CS --(Rule of Mutual Exclusion)(Rule of Mutual Exclusion) No assumptions are made about relative process No assumptions are made about relative process

speeds or number of CPUs speeds or number of CPUs - - (Race Conditions)(Race Conditions) No process can remain inside a CS indefiniely No process can remain inside a CS indefiniely

(CS must be small in code and it’s execution (CS must be small in code and it’s execution must take minimal time) - must take minimal time) - (Rule of Deadlock and (Rule of Deadlock and Starvation)Starvation)

A process outside a CS should not block other A process outside a CS should not block other processes processes - - (Rule of Deadlock and Starvation)(Rule of Deadlock and Starvation)

No process should wait forever before entering No process should wait forever before entering its CSits CS (When a process leaves a CS, the process (When a process leaves a CS, the process manager must activate a waiting process to enter manager must activate a waiting process to enter its CS. Processes waiting to enter a CS usually its CS. Processes waiting to enter a CS usually wait in a queue maintained in a FIFO manner or wait in a queue maintained in a FIFO manner or by a priority rule) - by a priority rule) - (Rule of Starvation)(Rule of Starvation)

Page 10: 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

10

Correctness SpecificationCorrectness Specification

Correctness specifications required for a solution Correctness specifications required for a solution are :are : Mutual exclusionMutual exclusion : Statements from the critical : Statements from the critical

sections of two or more processes must not be sections of two or more processes must not be interleaved interleaved

Freedom from DeadlockFreedom from Deadlock :: If some processes are If some processes are trying to enter their critical sections, then one of them trying to enter their critical sections, then one of them must eventually succeedmust eventually succeed

Freedom from StarvationFreedom from Starvation : : If any process tries to If any process tries to enter its critical section, then that process must enter its critical section, then that process must eventually succeedeventually succeed

Page 11: 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

11

SynchronizationSynchronization A synchronization mechanism (synchronization A synchronization mechanism (synchronization

primatives) must be provided to handle the critical primatives) must be provided to handle the critical section problemsection problem

This synchronization mechanism consists of additional This synchronization mechanism consists of additional statements that are placed before and after a critical statements that are placed before and after a critical section (preprotocol and postprotocol)section (preprotocol and postprotocol)

Page 12: 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

12

First AttemptFirst Attempt(Critical Section Problem for two (Critical Section Problem for two

processes)processes)Program mutualexclusion;Program mutualexclusion;Var turn : integer;Var turn : integer;Procedure P;Procedure P;beginbegin

repeat repeat non-critical section;non-critical section;repeat (* do nothing *) until turn = 1; repeat (* do nothing *) until turn = 1; (* busy wait (* busy wait

while P2 is in CS*)while P2 is in CS*)critical section; critical section; turn := 2;turn := 2;

until false;until false;End;End;Procedure Q;Procedure Q;BeginBegin

repeatrepeatnon-critical section;non-critical section;repeat (* do nothing *) until turn = 2;repeat (* do nothing *) until turn = 2; (* busy wait while P1 is in (* busy wait while P1 is in

CS *)CS *)critical section;critical section;turn := 1;turn := 1;

until false;until false;End;End;Begin (* main program *)Begin (* main program *)

turn := 1; (* we start with P1 *)turn := 1; (* we start with P1 *)cobegin P; Q coendcobegin P; Q coend

End.End.

Page 13: 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

13

CommentsComments Do (* nothing *)Do (* nothing *) is implemented by timeslicing is implemented by timeslicing

(process loops in the while instruction )(process loops in the while instruction ) Solution satisfies Solution satisfies mutual exclusionmutual exclusion, since only , since only

one process can be in CSone process can be in CS Deadlock is not possibleDeadlock is not possible. Both processes can . Both processes can

never be stuck at the while statementnever be stuck at the while statement Process Process starvationstarvation is possible, if a process spends is possible, if a process spends

a lot of time in its non-critical section. Here we a lot of time in its non-critical section. Here we assume that the other process is busy waiting to assume that the other process is busy waiting to enter the critical section. If processes spend enter the critical section. If processes spend finite times in non-critical sections then there will finite times in non-critical sections then there will be no starvation be no starvation

Page 14: 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

14

DrawbackDrawback

The processes are not loosely The processes are not loosely connected. That is, the right to enter a connected. That is, the right to enter a CS is being explicitly passed from one CS is being explicitly passed from one process to anotherprocess to another

Suppose process P is waiting to enter Suppose process P is waiting to enter the CS. If something happens to process the CS. If something happens to process Q while inside the non-critical section, Q while inside the non-critical section, process P is hopelessly deadlocked after process P is hopelessly deadlocked after the next passthe next pass

Page 15: 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

15

First Attempt Using Book’s First Attempt Using Book’s ConventionConvention

Await is blocking wait not a busy wait loop Await is blocking wait not a busy wait loop

Page 16: 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

16

Proving Correctness with Proving Correctness with State Diagrams State Diagrams

In the first attempt, states are triples of the form In the first attempt, states are triples of the form (p(pii,q,qjj,turn),turn)

Variables used in the non-critical and critical sections Variables used in the non-critical and critical sections are assumed to be distinct from the variables used in are assumed to be distinct from the variables used in the protocols so that they have no effect at the the protocols so that they have no effect at the correctness of the solution. So, they are left out of the correctness of the solution. So, they are left out of the statesstates

The mutual exclusion correctness property holds if the The mutual exclusion correctness property holds if the set of all accessible states does not contain a state of set of all accessible states does not contain a state of the form (p3,q3,turn) for some value of the form (p3,q3,turn) for some value of turnturn, because , because p3 and q3 are the labels of the critical sectionsp3 and q3 are the labels of the critical sections

Page 17: 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

17

State DiagramsState Diagrams

How many states can be in a state diagram?How many states can be in a state diagram? Suppose the algorithm has Suppose the algorithm has NN processes with processes with nnii

statements in process statements in process ii, and , and MM variables where variables where variable variable jj has m has mj j possible values.possible values.

The number of possible states is The number of possible states is

nn11 x ... x x ... x nnNN x x mm11 x ... x x ... x mmMM

For the first attempt, the number of states is For the first attempt, the number of states is 4x4x2=32 since we have 4 statements per 4x4x2=32 since we have 4 statements per process and turn can be 1 or 2process and turn can be 1 or 2

Page 18: 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

18

State Diagram of the First State Diagram of the First AttemptAttempt

The initial state is The initial state is (p1,q1,1)(p1,q1,1)

Lefthand side is P Lefthand side is P executing, righthand executing, righthand side is Q executingside is Q executing

The incremental The incremental construction terminates construction terminates after 16 of the 32 after 16 of the 32 possible states have possible states have been constructedbeen constructed

We do not have states We do not have states (p3,q3,1) or (p3,q3,2) (p3,q3,1) or (p3,q3,2) so mutual exclusion so mutual exclusion property for property for correctness holds for correctness holds for the first attemptthe first attempt

Page 19: 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

19

Abbreviating the State Abbreviating the State DiagramDiagram

Statements executed in the non-critical and Statements executed in the non-critical and critical sections are irrelevant to the critical sections are irrelevant to the correctness of the synchronization algorithm. correctness of the synchronization algorithm. So, they are eliminatedSo, they are eliminated

In fact, you can think of p asIn fact, you can think of p as p1: non-critical section; await turn = 1p1: non-critical section; await turn = 1 p2: critical section; turn <- 2p2: critical section; turn <- 2

Now we have 4 states to considerNow we have 4 states to consider

Page 20: 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

20

Correctness of the First Correctness of the First AttemptAttempt

Mutual exclusion property holds (see Mutual exclusion property holds (see the state diagram)the state diagram)

Is the algorithm deadlock free?Is the algorithm deadlock free? Which means : If some processes are Which means : If some processes are

trying to enter their critical sections, trying to enter their critical sections, then one of them must eventually then one of them must eventually succeedsucceed

Page 21: 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

21

Is it Deadlock Free?Is it Deadlock Free? A process is trying to enter its A process is trying to enter its

CS if it is executing p1 or q1CS if it is executing p1 or q1 Consider the initial state. Consider the initial state. TurnTurn

is 1, which means p1 passes is 1, which means p1 passes through and q1 loops onthrough and q1 loops on

Next state is the lower right. Next state is the lower right. Here p2 executes CS and Here p2 executes CS and eventually sets eventually sets turnturn to 2. to 2. While it is in the CS, Q is While it is in the CS, Q is executing q1executing q1

Next state is upper left in Next state is upper left in which Q enters its CS. which Q enters its CS.

TurnTurn value decides which value decides which process should enter the CSprocess should enter the CS

Both processes eventually Both processes eventually enter their CS in a mutually enter their CS in a mutually exclusive manner. Hence, the exclusive manner. Hence, the property of freedom from property of freedom from deadlock is satisfieddeadlock is satisfied

Page 22: 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

22

Is it Starvation Free?Is it Starvation Free?

Consider a section of Consider a section of the original state the original state diagram. NCS stands for diagram. NCS stands for a non-critical sectiona non-critical section

The state on the lower The state on the lower left is the one in which left is the one in which p2 is waiting to enter its p2 is waiting to enter its CS and q1 is in the NCSCS and q1 is in the NCS

If process Q decides to If process Q decides to stay in its NCS (or stay in its NCS (or broken down), P can not broken down), P can not enter its CS so starveenter its CS so starve

Hence, freedom from Hence, freedom from starvation property does starvation property does not hold for this solutionnot hold for this solution

Second attempt tries to Second attempt tries to solve itsolve it

Page 23: 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

23

Second AttemptSecond Attempt

Each process has its own flag which is true when process is in Each process has its own flag which is true when process is in CS (p3, q3 and p5, q5)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 If a process halts in its NCS then the other proceeds

independentlyindependently. .

Page 24: 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

24

Second Attempt Second Attempt (Abbreviated)(Abbreviated)

Page 25: 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

25

State Diagram for Abbreviated State Diagram for Abbreviated AlgorithmAlgorithm

The last state (p3,q3, true,true) means that both processes enter CS at the same The last state (p3,q3, true,true) means that both processes enter CS at the same timetime

Mutual exclusion property does not holdMutual exclusion property does not hold

Page 26: 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

26

A Scenario Showing that A Scenario Showing that Mutual Exclusion Does Not Mutual Exclusion Does Not

HoldHold

Page 27: 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

27

Second Attempt (for Second Attempt (for jBACI)jBACI)Program secondattempt;Program secondattempt;

Var c1, c2 : integer;Var c1, c2 : integer;Procedure p;Procedure p;BeginBegin

repeatrepeatwhile c2 = 0 do;while c2 = 0 do;c1 := 0;c1 := 0;CS;CS;c1 := 1;c1 := 1;NCS;NCS;

until false;until false;End;End;Procedure q;Procedure q;BeginBegin

repeatrepeatwhile c1 = 0 do;while c1 = 0 do;c2 := 0;c2 := 0;CS;CS;c2 := 1;c2 := 1;NCS;NCS;

until false;until false;End;End;Begin (* main program *)Begin (* main program *)

c1:= 1; c2:= 1; (* Both processes are not c1:= 1; c2:= 1; (* Both processes are not in their CS *)in their CS *)cobegin p; q coendcobegin p; q coend

End.End.

loop on while p2 is in CSloop on while p2 is in CS processprocess is entering the CS is entering the CS

process leaves the CSprocess leaves the CS

if p2 breaks down here, if p2 breaks down here, p1 can still execute since it has its own flag c1p1 can still execute since it has its own flag c1

Page 28: 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

28

Comments & CorrectnessComments & Correctness Each process periodically Each process periodically

checks the other’s checks the other’s common variable cx. If common variable cx. If the other process is not the other process is not in CS (ie., cx = 1); the in CS (ie., cx = 1); the other process enters its other process enters its CS, sets its flag c to 0 to CS, sets its flag c to 0 to indicate that it will enter indicate that it will enter its CSits CS

This program may not This program may not work correctly if events work correctly if events are as shown in the tableare as shown in the table

We have no mutual We have no mutual exclusion so deadlock!exclusion so deadlock!

c1c1 c2c2

initiallyinitially 11 11

P1 checks c2P1 checks c2 11 11

P2 checks c1P2 checks c1 11 11

P1 sets c1P1 sets c1 00 11

P2 sets c2P2 sets c2 00 00

P1 enters CSP1 enters CS 00 00

P2 enters CSP2 enters CS 00 00

Both processes are in Both processes are in CSCS

00 00

Page 29: 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

29

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

Page 30: 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

30

Scenario for a DeadlockScenario for a Deadlock

Both processes are locked at p3 and q3Both processes are locked at p3 and q3 The situation is a deadlock case but we may call it a The situation is a deadlock case but we may call it a livelock livelock since since

processes are actively executing statements but nothing useful is done processes are actively executing statements but nothing useful is done

Page 31: 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

31

Third Attempt (In jBACI)Third Attempt (In jBACI)Program thirdattempt;Program thirdattempt;Var c1, c2 : integer;Var c1, c2 : integer;Procedure p1;Procedure p1;BeginBegin

repeatrepeatc1 := 0;c1 := 0;

while c2 = 0 do;while c2 = 0 do;

CS;CS;c1 := 1;c1 := 1;NCS;NCS;

until false;until false;End;End;Procedure p2;Procedure p2;BeginBegin

repeatrepeatc2 := 0;c2 := 0;while c1 = 0 do;while c1 = 0 do;CS;CS;c2 := 1;c2 := 1;NCS;NCS;

until false;until false;End;End;Begin (* main program *)Begin (* main program *)

c1:= 1; c2:= 1; (* Both processes are not c1:= 1; c2:= 1; (* Both processes are not in their CS *)in their CS *)cobegin p1; p2, coendcobegin p1; p2, coend

End.End.

process is entering its CSprocess is entering its CSloop on while p2 is in CSloop on while p2 is in CS

process leaves the CSprocess leaves the CS

Page 32: 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

32

Comments & CorrectnessComments & Correctness

The third solution is a The third solution is a modification of the modification of the 2’nd attempt. Here, the 2’nd attempt. Here, the variable cx (request to variable cx (request to enter CS) is set before enter CS) is set before checking the other checking the other process whether it is in process whether it is in CS or notCS or not

The solution is not The solution is not correct as shown in the correct as shown in the events of the tableevents of the table

c1c1 c2c2

initiallyinitially 11 11

P1 sets c1P1 sets c1 00 11

P2 sets c2P2 sets c2 00 00

P1 enters CSP1 enters CS 00 00

P2 enters CSP2 enters CS 00 00

Both processes are in Both processes are in CSCS

00 00

Page 33: 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

33

Dekker’s AlgorithmDekker’s Algorithm

Page 34: 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

34

Dekker’s Algorithm (in Dekker’s Algorithm (in jBACI)jBACI)Program Dekker;Program Dekker;

Var Var turnturn : integer;: integer;wantp, wantqwantp, wantq :boolean;:boolean;

Procedure p;Procedure p;BeginBegin

repeatrepeatwantp := true;wantp := true;while wantq do if turn = 2 then begin wantp:= false; repeat until turn = 1; while wantq do if turn = 2 then begin wantp:= false; repeat until turn = 1;

wantp:=true end;wantp:=true end;CS;CS;turn := 2;turn := 2;wantp:= false;wantp:= false;NCS;NCS;

until false;until false;End;End;Procedure q;Procedure q;BeginBegin

repeatrepeatwantq2 := true;wantq2 := true;while wantp do if turn = 1 then begin wantq:= false; repeat until turn = 2; while wantp do if turn = 1 then begin wantq:= false; repeat until turn = 2;

wantq:=false end;wantq:=false end;CS;CS;turn := 1;turn := 1;wantq := false;wantq := false;NCS;NCS;

until false;until false;End;End;Begin (* main program *)Begin (* main program *)

turn:= 1; wantp:= false; wantq:= false;turn:= 1; wantp:= false; wantq:= false;cobegin p; q coendcobegin p; q coend

End.End.

Page 35: 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

35

Explanation of the Explanation of the AlgorithmAlgorithm

Procedure p;Procedure p;

BeginBegin

repeatrepeat

wantp := true;wantp := true; (* 1 *)(* 1 *)

while wantq do if turn = 2 then begin wantp:= false; repeat until turn = 1; while wantq do if turn = 2 then begin wantp:= false; repeat until turn = 1; wantp:=true end;wantp:=true end; (* 2 *)(* 2 *)

CS;CS;

turn := 2;turn := 2; (* 3 *)(* 3 *)

wantp := false;wantp := false; (* 4 *)(* 4 *)

NCS;NCS;

until false;until false;

End;End;

1.1. Process makes a request to enter CSProcess makes a request to enter CS

2.2. If the other process had made a request to enter CS before and if If the other process had made a request to enter CS before and if it is its turn then it is its turn then

Take back the request to enter CS by setting the “want” Take back the request to enter CS by setting the “want” variable to false”variable to false”

Wait for the other process to exit CS and change the turn Wait for the other process to exit CS and change the turn variablevariable

Make a new request to enter CS and then enter CSMake a new request to enter CS and then enter CS

3.3. Flip the turn variable so that now the other process can enter CSFlip the turn variable so that now the other process can enter CS

4.4. Set “want” variable to false to indicate that the process is now out Set “want” variable to false to indicate that the process is now out of CSof CS

Page 36: 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

36

CommentsComments

Explicit control of transfer by a Explicit control of transfer by a turnturn variable. Hence; turn = 1 means P’s variable. Hence; turn = 1 means P’s turn, turn = 2 Q’s turnturn, turn = 2 Q’s turn

If a process is blocked, the other If a process is blocked, the other process can still goprocess can still go

Dekker’s algorithm is correct (prove it!)Dekker’s algorithm is correct (prove it!) It satisfies the mutual exclusion propertyIt satisfies the mutual exclusion property It is free from deadlock and starvationIt is free from deadlock and starvation