Download presentation

Presentation is loading. Please wait.

Published byCarl Mosley Modified over 2 years ago

1
**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 - Interrupt Disabling Mutual Exclusion Machine Instructions (HW Q3)

2
**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.

3
const n = 50; * tally (2) * var tally: integer; procedure total; var count: integer; begin for count :=1 to n do tally := tally + 1 end; begin (*main program *) tally := 0; parbegin total; total; …… total; * n number of total * parend; write (tally) end.

4
**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 ???

5
**Process P1 (Both share tally:=0) Process P2**

for count:=1 to n for count:=1 to n do tally:=tally do tally:=tally+1 n=1, P1 did tally+1 P1 losing processor did not store this value tally:= P2 did for loop to n=49 tally:=49 P2 losing processor P1 regains control processor replacing tally (49) with 1 tally:= *Solution of tally (4)* P1 losing processor again

6
**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 processor P1 regains control processor do for loop from n:=2 to 50 tally:=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

7
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.

8
**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> forever

9
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> forever

10
**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

11
**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.

12
**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; P2 parend end.

13
**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> forever end; No Starvation in Dekker’s Algorithm (2) – P0 Algorithm

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

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> forever end; No Starvation in Dekker’s Algorithm (3) – P1 Algorithm

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

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> forever end; No Starvation in Dekker’s Algorithm (4) – P2 Algorithm

16
**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 *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

17
**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

18
**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; P1 parend end. * Main program is exactly the same as the Dekker’s algorithm

19
**Peterson’s Algorithm - P0 (2)**

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> forever end;

20
**Peterson’s Algorithm - P1 (3)**

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> forever end;

21
**Peterson’s Algorithm - Mutual Exclusion (4)**

procedure P0; procedure P1; begin begin repeat repeat flag[0]:= true; flag[1]:= true; turn:= 1; turn:= 0; while flag[1] and turn = 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.

22
**Peterson’s Algorithm - Dead Lock (5)**

procedure P0; procedure P1; begin begin repeat repeat flag[0]:= true; flag[1]:= true; turn:= 1; turn:= 0; while flag[1] and turn = 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.

23
**Peterson’s Algorithm - three exhaustive cases (6)**

P1 has no interest in its critical section This case is impossible, because it implies flag[1] = false P1 is waiting for its critical section This 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 it This cannot happen, because P1 is obliged to give P0 an opportunity be setting turn to 0 before each attempt to enter its critical section.

24
**Peterson’s Algorithm - N Processes Algorithm (7)**

Procedure i begin repeat 1 for j=1 to N * 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 N 5 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 false end

25
**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; ……; Pn parend end.

26
**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.

27
**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.

28
**Question 1 -Exercise/Home Work (2)**

procedure P (id: integer); begin repeat blocked[id] :=true; while turn != id do while blocked[1-id] do; turn :=id end; <critical section> blocked[id] :=false; < remainder > until false

29
**Mutual Exclusion - Interrupt Disabling (1)**

In an uniprocessor machine: repeat < disable interrupts >; < critical section >; <enable interrupts>; < remainder>; forever

30
**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 degraded Disabling interrupts on more than two processors will not guarantee mutual exclusion

31
**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.

32
**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

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

Function testset (var i: integer): boolean begin if i = 0 then * if i is 0, i is replaced by 1 * begin * and return true to testset * i := 1; testset := true end else testset := false * if i is 1, * end * return false to testset *

34
**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.

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

procedure P(i: integer); begin repeat repeat {nothing} until testset(bolt); * if bolt = 0, * <critical section>; * testset = true, P(i) goes to * * critical section * bolt:=0; * P(i) reset bolt:= * <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 *

36
**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? Deadlock If 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.

Similar presentations

OK

Ch. 7 Process Synchronization (1/2) 2015-04-17. 7.I Background F Producer - Consumer process : Compiler, Assembler, Loader, · · · · · · F Bounded buffer.

Ch. 7 Process Synchronization (1/2) 2015-04-17. 7.I Background F Producer - Consumer process : Compiler, Assembler, Loader, · · · · · · F Bounded buffer.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on pheromonal security system Ppt on network theory and analysis Ppt on credit default swaps 2016 Ppt on polynomials in maths what does commutative mean Ppt on construction of building Ppt on review writing services Ppt on obesity in india Ppt on noise in frequency modulation Ppt on online shopping project in java Full ppt on electron beam machining video