Presentation is loading. Please wait.

Presentation is loading. Please wait.

Synchronization Mutual Exclusion Solutions using shared variables + problems such as deadlocks, starvation, progressiveness, busy wait Prof. R K Joshi.

Similar presentations


Presentation on theme: "Synchronization Mutual Exclusion Solutions using shared variables + problems such as deadlocks, starvation, progressiveness, busy wait Prof. R K Joshi."— Presentation transcript:

1 Synchronization Mutual Exclusion Solutions using shared variables + problems such as deadlocks, starvation, progressiveness, busy wait Prof. R K Joshi CSE, IIT Bombay

2 We know about deadlocks already R2 R1 P1 P2 wait hold

3 The problem of mutual exclusion (exclusiveness) V p1 p2 Read Write Read Write

4 Attempt 1 use v as simple flag 0:free, 1:available P1 If (v==0) v=1 Use the resource V=0 Else repeat P2 If (v==0) v=1 Use the resource V=0 Else repeat Problem: both see v=0 Both go ahead, no mutual exclusion: correctness problem Due to context switch immediately after if (v==0) condition check Due to round robin scheduling or some other scheduling concern

5 Fix 1 use v as turn indicator 0:p1's turn, 1:p2's turn initial value: turn=0 or 1 P0 If (turn==0) Use the resource turn=1 Else repeat P1 If (turn==1) Use the resource turn=0 Else repeat Problem: if it's someone else's turn, and the process is not picking it up, then there is no progress for the interested process...

6 Attempt 2 v,w = 0 P1 V=1 while (true) { If (w=0) Use the resource else v=0 } P2 W=1 While (true) If (v==0) Use resource Else w=0 } Problem: P1 goes in, P2 cannot, P1 runs and then sets v=1 when it repeats P2 checks for v again, but fails, while P1 goes ahead and this can continue Starvation problem also correctness problem!

7 Attempt 3 v,w = 0 P1 while (true) { v=1 If (w=0) Use the resource else v=0 } P2 While (true) w=1 If (v==0) Use resource Else w=0 } Problem: P1 shows interest, P2 shows it too P1 withdraws, P2 also does the same They repeat the show interest-withdraw cycle for ever Livelock! Starvation problem: find it. correctness problem solved!

8 Attempt 4 claim: no starvation willing[0]=willing[1] = 0 P0 while (true) { willing[0]=1 If (!willing[1]) Use the resource willing[0]=0 } P1 While (true) willing[1]=1 If (!willing[0]) Use resource willing[1]=0 } P0 is willing, p1 is yet to set it's willingness, p0 goes in P1 comes in, sees p0 willing, it withdraws on the last line Now p0 quickly completes and and takes the resource once more This can repeat forever

9 Attempt 5 claim: no starvation, use a delay willing[0]=willing[1] = 0 P0 while (true) { willing[0]=1 If (!willing[1]) Use the resource Willing [0]=0 delay else Willing[0]=0 } P1 While (true) willing[1]=1 If (!willing[0]) Use resource Willing[1]=0 delay else willing[1]=0 } Doesnot solve the problem, however we have tried a new idea: that of delays

10 Attempt 56 claim: no starvation, use a delay willing[0]=willing[1] = 0 P0 while (true) { willing[0]=1 If (!willing[1]) Use the resource Willing [0]=0 while(willing[1]); else Willing[0]=0 } P1 While (true) willing[1]=1 If (!willing[0]) Use resource Willing[1]=0 While willing[0]; else willing[1]=0 } Doesnot solve the problem, however we have tried a new idea: that of delays

11 Attempt 6 claim: no starvation, use a condition willing[0]=willing[1] = 0 P0 while (true) { willing[0]=1 If (!willing[1]) Use the resource Willing [0]=0 while(willing[1]); else Willing[0]=0 } P1 While (true) willing[1]=1 If (!willing[0]) Use resource Willing[1]=0 While (willing[0]) ; else willing[1]=0 } Next idea: instead of delay, use a condition variable----> does not solve the problem

12 A suggestion: let's start all over again.. Let's put up the 'taking turns' solution after restructruring it a bit.. observe the separation of critical section entry and exit code..

13 Fix 1 use a turn indicator 0:p1's turn, 1:p2's turn initial value: turn=0 or 1 P0 while (turn==1); Use the resource turn=1 P1 while (turn==0); Use the resource turn=0 Problem: if it's someone else's turn, and the process is not picking it up, then there is no progress for the interested process...

14 Fix 7 use a turn indicator 0:p1's turn, 1:p2's turn initial value: turn=0 or 1 willing[0], willing[1]=0 P0 willing[0]=1 while (willing[1]); Use the resource willing[0]=0 P1 willing[1]=1 while (willing[0]); Use the resource willing[1]=0 Problem: if it's someone else's turn, and the process is not picking it up, then there is no progress for the interested process...

15 Fix 8: willing[0], willing[1]=0 P0 while (true) { willing[0]=1 while (willing[1]) { Willing[0]=0; Delay Willing[0]=1; } Use the resource willing[0]=0; } P1 while (true) { willing[1]=1 while (willing[0]) { Willing[1]=0; delay Willing[1]=1; } Use the resource willing[1]=0 }

16 Time for a solution?

17 Dekker's solution turn=willing[0]=willing[1]=0 While (true) { willing [0] = 1; while (willing[1]) { if (turn==1) willing[0]=0; while(turn==1); willing[0]=1; } CS willing[0]=0; turn=1; } While (true) { willing [1] = 1; while (willing[0]) { if (turn==0) willing[1]=0; while(turn==0); willing[1]=1; } CS willing[1]=0; turn=0;

18 Peterson's solution turn=willing[0]=willing[1]=0 While (true) { willing [0] = 1; turn=1; while (willing[1]&&turn==1); CS willing[0]=0; } While (true) { willing [1] = 1; turn=0; while (willing[0]&&turn==0); CS willing[1]=0; }


Download ppt "Synchronization Mutual Exclusion Solutions using shared variables + problems such as deadlocks, starvation, progressiveness, busy wait Prof. R K Joshi."

Similar presentations


Ads by Google