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
Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2)
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.
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.
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 ???
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
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
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.
var flag:array[ 0.. 1]; * a shared global variable* turn: 0..1; * a referee’s message*procedure P0;begin
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
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; 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)
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.
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
Peterson’s Algorithm - N Processes Algorithm (8)
var q:array[1..n] of integer; turn: array[1..n-1] of integer;
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
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.
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
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 *
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.
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*
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.