Top Banner
Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock Starvation Peterson’s Algorithm (HW Q1) Mutual Exclusion Dead Lock Starvation Mutual Exclusion - Interrupt Disabling Mutual Exclusion Machine
36

Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Dec 14, 2015

Download

Documents

Carl Mosley
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2)

• tally Bounds for N Processes

• Dekker’s Algorithm (HW Q2)• Mutual Exclusion

• Dead Lock

• Starvation

• Peterson’s Algorithm (HW Q1)• Mutual Exclusion

• Dead Lock

• Starvation

• Mutual Exclusion - Interrupt Disabling • Mutual Exclusion Machine Instructions (HW Q3)

Page 2: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

tally Bounds for N Processes (1)

Consider the following program (next slide)Assume processes can execute at any relative

speed and that a value can only be incremented after it has been loaded into a register by a separate machine instruction.

Suppose that n number of these processes are permitted to execute in parallel. Determine the proper lower bound and upper bound on the final value of the shared variable tally output by this concurrent program.

Page 3: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

const n = 50; * tally (2) *var tally: integer;

procedure total;var count: integer;begin

for count :=1 to n do tally := tally + 1end;

begin (*main program *)tally := 0;parbegin

total; total; …… total; * n number of total *parend;write (tally)

end.

Page 4: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Possible Solutions - tally Bounds for N Processes (3)

Upper bound on the final value of the shared variable tally output by this concurrent program should be n*50.

Lower bound of tally - lack of mutual exclusion: n ??? - lack of mutual exclusion: 3 ??? - lack of mutual exclusion: 2 ???

Page 5: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Process P1 (Both share tally:=0) Process P2 for count:=1 to n for count:=1 to n do tally:=tally+1 do tally:=tally+1

n=1, P1 did tally+1P1 losing processordid not store this valuetally:=0 P2 did for loop to

n=49 tally:=49

P2 losing processorP1 regains control processor replacing tally (49) with 1 tally:=1 *Solution of tally (4)*P1 losing processor again

Page 6: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

tally:=1 *Solution of tally (5)*P1 losing processor again P2 regains control Processor load tally (1) to register But no time to do tally+1 P2 losing control processorP1 regains control processordo for loop from n:=2 to 50tally:=50 P3, P4, …, Pn do for loop(n-2) times and tally:= (n-1)*50

P2 is reactivated using register value (1) and

do tally:=tally(register value)+1

tally:=2

Page 7: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Dekker’s Algorithm

Mutual Exclusion ? - Mutual exclusion is enforced in the Dekker’s

Algorithm.Dead Lock?

- No dead lock is happened in the Dekker’s Algorithm.

Starvation?- No starvation in the Dekker’s Algorithm.

Page 8: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

var flag:array[ 0.. 1]; * a shared global variable* turn: 0..1; * a referee’s message*procedure P0;begin

repeat flag[0]:= true; *attempt 3 mutual exclusion *

while flag[1] do if turn =1 then

begin *attempt 4 avoiding dead lock*if turn=0 P0’s turn flag[0]:=false;

to check P1’s igloo while turn:=1 do{nothing}; avoid mutual courtesy flag[0]:=true

end; <critical section> *flag[1]=false, P0 goes critical part* turn:=1; *P1’s turn to check P0’s igloo * flag[0]:=false; *P1 can go critical section * <remainder>

foreverend;

Page 9: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

procedure P1;begin

repeat flag[1]:= true; *attempt 3 mutual exclusion *

while flag[0] do if turn =0 then

begin *attempt 4 avoiding dead lock*if turn=1 P1’s turn flag[1]:=false;

to check P0’s igloo while turn:=0 do{nothing}; avoid mutual courtesy flag[1]:=true

end; <critical section> *flag[0]=false, P1 goes critical part* turn:=0; *P0’s turn to check P1’s igloo * flag[1]:=false; *P0 can go critical section * <remainder>

foreverend;

Page 10: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Mutual Exclusion forced in Dekker’s Algorithm (1) - Question

Prove that mutual exclusion is enforced in the Dekker’s algorithm.

Hint: Show that when Pi enters its critical section, the following expression is true:

flag[i] and (not flag[1-i])

It means that we should prove following expressions:

(flag[1] and (not flag[0])) = true (flag[0] and (not flag[1])) = true

Page 11: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

No Dead Lock in Dekker’s Algorithm (1) - Question (HW Q2)

Prove that a process requiring access to its critical section will not be delayed indefinitely in the Dekker’s algorithm.

Hint: Consider the following cases: (1) A single process is attempting to enter

the critical section; (2) both processes are attempting to enter

the critical section, and (2a) turn=0 and flag[0] = false, and (2b) turn=0 and flag[0] = true.

Page 12: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

No Starvation in Dekker’s Algorithm (1) - Program

var turn: integer; * turn:=0, 1, or 2 *......Begin

flag[0]:=false;flag[1]:=false;flag[2]:=false;turn:=1;parbegin

P0; P1; P2parend

end.

Page 13: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

var flag:array[ 0.. 1]; * a shared global variable* turn: integer; * a referee’s message, turn:=0, 1, or 2*procedure P0;begin

repeat flag[0]:= true; *attempt 3 mutual exclusion *

while (flag[1]or flag[2]) do if ( turn =1 or turn=2) then

begin *attempt 4 avoiding dead lock*

if turn=0 P0’s turn flag[0]:=false;to check P1 & P2’s igloo while turn:=1 or 2 do{nothing}; avoid mutual courtesy flag[0]:=true

end; *flag[2]=false and <critical section> *flag[1]=false, P0 goes critical part* turn:=1; *P1’s turn to check P0’s igloo, or turn:=2* flag[0]:=false; *P1, P2 can go critical section * <remainder>

foreverend; No Starvation in Dekker’s Algorithm (2) – P0 Algorithm

Page 14: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

procedure P1;begin

repeat flag[1]:= true; *attempt 3 mutual exclusion *

while (flag[0]or flag[2]) do if (turn =0 or turn=2)then

begin *attempt 4 avoiding dead lock*

if turn=1 P1’s turn flag[1]:=false;to check P0 &P2’s igloo while turn:=2 or 0 do{nothing}; avoid mutual courtesy flag[1]:=true

end; *flag[2]=false and <critical section> *flag[0]=false, P1 goes critical part* turn:=2; *P2’s turn to check P1’s igloo, or turn:=0)* flag[1]:=false; *P0, P2 can go critical section * <remainder>

foreverend; No Starvation in Dekker’s Algorithm (3) – P1 Algorithm

Page 15: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

procedure P2;begin

repeat flag[2]:= true; *attempt 3 mutual exclusion *

while (flag[0]or flag[1]) do if (turn =0 or turn=1)then

begin *attempt 4 avoiding dead lock*

if turn=2 P2’s turn flag[2]:=false;to check P0 & P1’s igloo while turn:=0 or 1 do{nothing}; avoid mutual courtesy flag[2]:=true

end; *flag[1]=false and <critical section> *flag[0]=false, P2 goes critical part* turn:=0; *P0’s turn to check P1’s igloo,or turn:=1)* flag[2]:=false; *P0 orP1 can go critical section

* <remainder>

foreverend; No Starvation in Dekker’s Algorithm (4) – P2 Algorithm

Page 16: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

var flag:array[ 0.. 1]; /* a shared global variable */ turn: integer; /* a referee’s message, turn:=0, 1, or 2*/procedure P0; procedure P1;begin begin repeat repeat flag[0]:= true; flag[1]:= true;

while (flag[1]or flag[2]) do while (flag[0]or flag[2]) do if ( turn =1 or turn=2) then if ( turn =0 or turn=2) then

begin begin *avoiding dead lock* *avoiding dead lock*

end; end; <critical section> <critical section> turn:=1; turn:=2; /*P2 ->turn:=0*/ flag[0]:=false; flag[1]:=false; <remainder> <remainder> forever forever

end; end;

No Starvation in Dekker’s Algorithm (5) – P0 and P1 Algorithms

Page 17: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Peterson’s Algorithm (0)

2 Processed Peterson’s Algorithm

- Mutual exclusion is enforced in the Peterson’s Algorithm.

- No dead lock is happened in the Peterson’s Algorithm.

- No starvation in the Peterson’s Algorithm.

N Processed Peterson’s Algorithm

- Mutual exclusion is enforced in the Peterson’s Algorithm.

- No dead lock is happened in the Peterson’s Algorithm.

- No starvation in the Peterson’s Algorithm.

Page 18: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Peterson’s Algorithm - main (1)

var flag:array[0..1] of boolean turn: 0 ..1;

......Begin

flag[0]:=false;flag[1]:=false;turn:=1;parbegin

P0; P1parend

end.

* Main program is exactly the same as the Dekker’s algorithm

Page 19: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

procedure P0;begin

repeat flag[0]:= true; *attempt 3 mutual exclusion

* turn:= 1; while flag[1] and turn =1 *attempt 4 mutual courtesy

* do{nothing};

<critical section> * flag[1]=false, or turn = 0 * * P0 goes critical part *

flag[0]:=false; *P1 can go critical section * <remainder>

foreverend;

Peterson’s Algorithm - P0 (2)

Page 20: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

procedure P1;begin

repeat flag[1]:= true; *attempt 3 mutual

exclusion * turn:= 0; while flag[0] and turn =0

do{nothing}; <critical section> * flag[0]=false, or turn = 1 *

* P1 goes critical part * flag[1]:=false; *P0 can go critical section * <remainder>

foreverend;

Peterson’s Algorithm - P1 (3)

Page 21: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

procedure P0; procedure P1;begin begin

repeat repeat flag[0]:= true; flag[1]:= true; turn:= 1; turn:= 0; while flag[1] and turn =1 while flag[0] and turn =0

do{nothing}; do{nothing}; <critical section> <critical section> flag[0]:=false; flag[1]:=false; <remainder> <remainder> forever forever

end; end;

Let turn:=0; Once P0 has set flag[0] to true, P0 can enter its critical section and P1 can

not enter its critical section. If P1 already is in its critical section, then flag[1]=true and flag[0]=false,

and P0 is blocked from entering its critical section.

Peterson’s Algorithm - Mutual Exclusion (4)

Page 22: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

procedure P0; procedure P1;begin begin

repeat repeat flag[0]:= true; flag[1]:= true; turn:= 1; turn:= 0; while flag[1] and turn =1 while flag[0] and turn =0

do{nothing}; do{nothing}; <critical section> <critical section> flag[0]:=false; flag[1]:=false; <remainder> <remainder> forever forever

end; end; P0 is blocked in its while loop. This means that flag[1] is true

and turn=1. Because turn = 1, P1 is not blocked its while loop. After critical

section, P1 would set flag[1]:=false. When flag[1]=false, P0 will go to critical section. P0 will not be

blocked for ever.

Peterson’s Algorithm - Dead Lock (5)

Page 23: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Peterson’s Algorithm - three exhaustive cases (6)

P1 has no interest in its critical sectionThis case is impossible, because it implies flag[1] = false

P1 is waiting for its critical sectionThis case is also impossible, because if turn=1, P1 is able to enter its critical section

P1 is using its critical section repeatedly and therefore monopolizing access to itThis cannot happen, because P1 is obliged to give P0 an opportunity be setting turn to 0 before each attempt to enter its critical section.

Page 24: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Peterson’s Algorithm - N Processes Algorithm (7)Procedure ibegin repeat1 for j=1 to N-1 * j is the “stage” of the algorithm * do { * at which process i executes *2 q[i] = j; *q[i] indicates the stage of each process *3 turn[j] = i; *turn[i] resolves simultaneity conflicts *4 L: for k=1 to i-1, i+1 to N5 if ( (q[k] >= j ) and (turn [j] = i)) goto L; }6 q[i] = N; *a process enters critical phase, it passes to stage N* critical section of process i;7 q[i] = 0; remainder section of process i; until falseend

Page 25: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Peterson’s Algorithm - N Processes Algorithm (8)

var q:array[1..n] of integer; turn: array[1..n-1] of integer;

......Begin

q[1]:=0; q[2]:=0; ……; q[n]:=0;turn[1]:=0; turn[2]:=0; ……; turn[n-1]:=0;parbegin

P0; P1; P2; ……; Pnparend

end.

Page 26: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Peterson’s Algorithm - N Processes Algorithm (8)

It has been proved that N processes Peterson’s algorithm provides mutual exclusion, deadlock freedom, and no starvation.

Horfi, M. “proof of a Mutual Exclusion Algorithm.” Operating Systems Review, January 1990.

Page 27: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Question 1 -Exercise/Home Work (1) Consider the following program (This and next slides). This is a software solution to the mutual exclusion

problem proposed in [HYMA66]. Find a counterexample that demonstrates that this solution is incorrect

var blocked: array [0 ..1] of boolean; turn: 0: 1;

procedure P (id: integer);.......begin ( * main program *) block[0] := false; blocked[1] := false; turn := 0; parbegin

p(0); P(1)parend

end.

Page 28: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Question 1 -Exercise/Home Work (2)procedure P (id: integer);begin

repeatblocked[id] :=true;while turn != id do

beginwhile blocked[1-id] do;turn :=id

end;<critical section>blocked[id] :=false;< remainder >

until falseend;

Page 29: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Mutual Exclusion - Interrupt Disabling (1)

In an uniprocessor machine:

repeat< disable interrupts >;< critical section >;<enable interrupts>;< remainder>;forever

Page 30: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Mutual Exclusion - Interrupt Disabling (2)Disabling interrupts on one processor

- A process runs until it invokes an operating- system service or until it is interrupted- On one processor, disabling interrupts

guarantees mutual exclusion- Efficiency of execution could be noticeably

degradedDisabling interrupts on more than two

processors will not guarantee mutual exclusion

Page 31: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Mutual Exclusion Machine Instructions (1)

At a hardware level, access to a memory location exclude any other access to that same location.

One machine instruction is used to update a memory location so other instructions cannot interfere.

For mutual exclusion purpose, several machine instructions that carry out two actions of a single memory location with one instruction fetch cycle have been proposed.

Page 32: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Mutual Exclusion Machine Instructions (2)

Mutual exclusion machine instructions can be used for single and multiple processors

Mutual exclusion machine instructions can be used for multiple critical sections

Page 33: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Test and Set Instruction -Mutual Exclusion Machine Instructions (3)

Function testset (var i: integer): booleanbegin

if i = 0 then * if i is 0, i is replaced by 1 * begin * and return true to testset *

i := 1;testset := true

endelse testset := false * if i is 1, *

end. * return false to testset *

Page 34: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Test and Set Instruction -Mutual Exclusion Machine Instructions (4)

Program mutualexclusion;const n=...;(*number of processes *)var bolt: integer;procedure P(i: integer);begin......end;begin (*main program *)

bolt := 0;parbegin

P(1); P(2); ......P(n);parend

end.

Page 35: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Test and Set Instruction -Mutual Exclusion Machine Instructions (5)

procedure P(i: integer);begin

repeatrepeat {nothing} until testset(bolt); * if bolt = 0, *

<critical section>; * testset = true, P(i) goes to *

* critical section *bolt:=0; * P(i) reset bolt:=0 *<remainder> * Only one waiting processes *forever *is granted access to its critical action*

end; *The choice of process depends on *

...... * which process happens to execute *

* the testset instruction next *

Page 36: Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) tally Bounds for N Processes Dekker’s Algorithm (HW Q2) Mutual Exclusion Dead Lock.

Mutual Exclusion Machine Instructions (6)Disadvantages

Busy-waiting consumes processor time Starvation is possible when a process

leaves a critical section and more than one process is waiting. Who is next?

DeadlockIf a low priority process P1 has the critical region and a higher priority process P2 needs, P2 will be denied because of mutual exclusion. However P1 will never be dispatched because it is of lower priority.