Concurrency Mutual Exclusion and Synchronization Chapter 5 Part

  • Slides: 36
Download presentation
Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) • tally Bounds for

Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2) • tally Bounds for N Processes • Dekker’s Algorithm (HW Q 2) • Mutual Exclusion • Dead Lock • Starvation • Peterson’s Algorithm (HW Q 1) • Mutual Exclusion • Dead Lock • Starvation • Mutual Exclusion - Interrupt Disabling • Mutual Exclusion Machine Instructions (HW Q 3)

tally Bounds for N Processes (1) z Consider the following program (next slide) z

tally Bounds for N Processes (1) z Consider the following program (next slide) z 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. z 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; var tally: integer; * tally (2) * procedure total; var

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

Possible Solutions - tally Bounds for N Processes (3) z Upper bound on the

Possible Solutions - tally Bounds for N Processes (3) z Upper bound on the final value of the shared variable tally output by this concurrent program should be n*50. z Lower bound of tally - lack of mutual exclusion: n ? ? ? - lack of mutual exclusion: 3 ? ? ? - lack of mutual exclusion: 2 ? ? ?

Process P 1 (Both share tally: =0) Process P 2 for count: =1 to

Process P 1 (Both share tally: =0) Process P 2 for count: =1 to n do tally: =tally+1 n=1, P 1 did tally+1 P 1 losing processor did not store this value tally: =0 for count: =1 to n do tally: =tally+1 P 2 did for loop to n=49 tally: =49 P 2 losing processor P 1 regains control processor replacing tally (49) with 1 tally: =1 *Solution of tally (4)* P 1 losing processor again

tally: =1 *Solution of tally (5)* P 1 losing processor again P 2 regains

tally: =1 *Solution of tally (5)* P 1 losing processor again P 2 regains control Processor load tally (1) to register But no time to do tally+1 P 2 losing control processor P 1 regains control processor do for loop from n: =2 to 50 tally: =50 P 3, P 4, …, Pn do for loop (n-2) times and tally: = (n-1)*50 P 2 is reactivated using register value (1) and do tally: =tally(register value)+1 tally: =2

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

Dekker’s Algorithm z Mutual Exclusion ? - Mutual exclusion is enforced in the Dekker’s Algorithm. z Dead Lock? - No dead lock is happened in the Dekker’s Algorithm. z Starvation? - No starvation in the Dekker’s Algorithm.

var flag: array[ 0. . 1]; * a shared global variable* turn: 0. .

var flag: array[ 0. . 1]; * a shared global variable* turn: 0. . 1; * a referee’s message* procedure P 0; 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 P 0’s turn flag[0]: =false; to check P 1’s igloo while turn: =1 do{nothing}; avoid mutual courtesy flag[0]: =true end; <critical section> *flag[1]=false, P 0 goes critical part* turn: =1; *P 1’s turn to check P 0’s igloo * flag[0]: =false; *P 1 can go critical section * <remainder> forever end;

procedure P 1; begin repeat flag[1]: = true; *attempt 3 mutual exclusion * while

procedure P 1; 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 P 1’s turn flag[1]: =false; to check P 0’s igloo while turn: =0 do{nothing}; avoid mutual courtesy flag[1]: =true end; <critical section> *flag[0]=false, P 1 goes critical part* turn: =0; *P 0’s turn to check P 1’s igloo * flag[1]: =false; *P 0 can go critical section * <remainder> forever end;

Mutual Exclusion forced in Dekker’s Algorithm (1) - Question Prove that mutual exclusion is

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

No Dead Lock in Dekker’s Algorithm - Question (HW Q 2) (1) Prove that

No Dead Lock in Dekker’s Algorithm - Question (HW Q 2) (1) 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 (2 a) turn=0 and flag[0] = false, and (2 b) turn=0 and flag[0] = true.

No Starvation in Dekker’s Algorithm (1) - Program var turn: integer; . . .

No Starvation in Dekker’s Algorithm (1) - Program var turn: integer; . . . Begin flag[0]: =false; flag[1]: =false; flag[2]: =false; turn: =1; parbegin P 0; P 1; P 2 parend end. * turn: =0, 1, or 2 *

var flag: array[ 0. . 1]; * a shared global variable* turn: integer; *

var flag: array[ 0. . 1]; * a shared global variable* turn: integer; * a referee’s message, turn: =0, 1, or 2* procedure P 0; 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 P 0’s turn flag[0]: =false; to check P 1 & P 2’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, P 0 goes critical part* turn: =1; *P 1’s turn to check P 0’s igloo, or turn: =2* flag[0]: =false; *P 1, P 2 can go critical section * <remainder> forever No Starvation in Dekker’s Algorithm (2) – P 0 Algorithm end;

procedure P 1; begin repeat flag[1]: = true; *attempt 3 mutual exclusion * while

procedure P 1; 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 P 1’s turn flag[1]: =false; to check P 0 &P 2’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, P 1 goes critical part* turn: =2; *P 2’s turn to check P 1’s igloo, or turn: =0)* flag[1]: =false; *P 0, P 2 can go critical section * <remainder> forever No Starvation in Dekker’s Algorithm (3) – P 1 Algorithm end;

procedure P 2; begin repeat flag[2]: = true; *attempt 3 mutual exclusion * while

procedure P 2; 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 P 2’s turn flag[2]: =false; to check P 0 & P 1’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, P 2 goes critical part* turn: =0; *P 0’s turn to check P 1’s igloo, or turn: =1)* flag[2]: =false; *P 0 or. P 1 can go critical section * <remainder> forever No Starvation in Dekker’s Algorithm (4) – P 2 Algorithm end;

var flag: array[ 0. . 1]; /* a shared global variable */ turn: integer;

var flag: array[ 0. . 1]; /* a shared global variable */ turn: integer; /* a referee’s message, turn: =0, 1, or 2*/ procedure P 0; procedure P 1; begin 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 *avoiding dead lock* end; <critical section> turn: =1; turn: =2; /*P 2 ->turn: =0*/ flag[0]: =false; flag[1]: =false; <remainder> forever end; No Starvation in Dekker’s Algorithm (5) – P 0 and P 1 Algorithms

Peterson’s Algorithm (0) z 2 Processed Peterson’s Algorithm - Mutual exclusion is enforced in

Peterson’s Algorithm (0) z 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. z 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.

Peterson’s Algorithm - main (1) var flag: array[0. . 1] of boolean turn: 0.

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

Peterson’s Algorithm - P 0 (2) procedure P 0; begin repeat flag[0]: = true;

Peterson’s Algorithm - P 0 (2) procedure P 0; 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 * * P 0 goes critical part * flag[0]: =false; *P 1 can go critical section * <remainder> forever end;

Peterson’s Algorithm - P 1 (3) procedure P 1; begin repeat flag[1]: = true;

Peterson’s Algorithm - P 1 (3) procedure P 1; 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 * * P 1 goes critical part * flag[1]: =false; *P 0 can go critical section * <remainder> forever end;

Peterson’s Algorithm - Mutual Exclusion (4) procedure P 0; procedure P 1; begin repeat

Peterson’s Algorithm - Mutual Exclusion (4) procedure P 0; procedure P 1; begin 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}; <critical section> flag[0]: =false; flag[1]: =false; <remainder> forever end; z Let turn: =0; z Once P 0 has set flag[0] to true, P 0 can enter its critical section and P 1 can not enter its critical section. z If P 1 already is in its critical section, then flag[1]=true and flag[0]=false, and P 0 is blocked from entering its critical section.

Peterson’s Algorithm - Dead Lock (5) procedure P 0; procedure P 1; begin repeat

Peterson’s Algorithm - Dead Lock (5) procedure P 0; procedure P 1; begin 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}; <critical section> flag[0]: =false; flag[1]: =false; <remainder> forever end; z P 0 is blocked in its while loop. This means that flag[1] is true and turn=1. z Because turn = 1, P 1 is not blocked its while loop. After critical section, P 1 would set flag[1]: =false. z When flag[1]=false, P 0 will go to critical section. P 0 will not be blocked for ever.

Peterson’s Algorithm - three exhaustive cases (6) z P 1 has no interest in

Peterson’s Algorithm - three exhaustive cases (6) z P 1 has no interest in its critical section This case is impossible, because it implies flag[1] = false z P 1 is waiting for its critical section This case is also impossible, because if turn=1, P 1 is able to enter its critical section z P 1 is using its critical section repeatedly and therefore monopolizing access to it This cannot happen, because P 1 is obliged to give P 0 an opportunity be setting turn to 0 before each attempt to enter its critical section.

Peterson’s Algorithm - N Processes Algorithm (7) Procedure i begin repeat 1 for j=1

Peterson’s Algorithm - N Processes Algorithm (7) Procedure i begin repeat 1 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 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

Peterson’s Algorithm - N Processes Algorithm (8) var q: array[1. . n] of integer;

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 P 0; P 1; P 2; ……; Pn parend end.

Peterson’s Algorithm - N Processes Algorithm (8) z It has been proved that N

Peterson’s Algorithm - N Processes Algorithm (8) z It has been proved that N processes Peterson’s algorithm provides mutual exclusion, deadlock freedom, and no starvation. z Horfi, M. “proof of a Mutual Exclusion Algorithm. ” Operating Systems Review, January 1990.

Question 1 -Exercise/Home Work (1) z Consider the following program (This and next slides).

Question 1 -Exercise/Home Work (1) z Consider the following program (This and next slides). z This is a software solution to the mutual exclusion problem proposed in [HYMA 66]. 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.

Question 1 -Exercise/Home Work (2) procedure P (id: integer); begin repeat blocked[id] : =true;

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

Mutual Exclusion Interrupt Disabling (1) In an uniprocessor machine: repeat < disable interrupts >;

Mutual Exclusion Interrupt Disabling (1) In an uniprocessor machine: repeat < disable interrupts >; < critical section >; <enable interrupts>; < remainder>; forever

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

Mutual Exclusion Interrupt Disabling (2) z Disabling interrupts on one processor - A process runs until it invokes an operatingsystem service or until it is interrupted - On one processor, disabling interrupts guarantees mutual exclusion - Efficiency of execution could be noticeably degraded z Disabling interrupts on more than two processors will not guarantee mutual exclusion

Mutual Exclusion Machine Instructions (1) z At a hardware level, access to a memory

Mutual Exclusion Machine Instructions (1) z At a hardware level, access to a memory location exclude any other access to that same location. z One machine instruction is used to update a memory location so other instructions cannot interfere. z 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) z Mutual exclusion machine instructions can be used for

Mutual Exclusion Machine Instructions (2) z Mutual exclusion machine instructions can be used for single and multiple processors z 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):

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 end. * if i is 1, * * return false to testset *

Test and Set Instruction -Mutual Exclusion Machine Instructions (4) Program mutualexclusion; const n=. .

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 repeat

Test and Set Instruction -Mutual Exclusion Machine Instructions (5) procedure P(i: integer); begin repeat {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 *

Mutual Exclusion Machine Instructions (6) z Disadvantages y Busy-waiting consumes processor time y Starvation

Mutual Exclusion Machine Instructions (6) z Disadvantages y Busy-waiting consumes processor time y Starvation is possible when a process leaves a critical section and more than one process is waiting. Who is next? y Deadlock If a low priority process P 1 has the critical region and a higher priority process P 2 needs, P 2 will be denied because of mutual exclusion. However P 1 will never be dispatched because it is of lower priority.