Presentation is loading. Please wait.

Presentation is loading. Please wait.

28/10/1999POS-A1 The Synchronization Problem Synchronization problems occur because –multiple processes or threads want to share data; –the executions.

Similar presentations


Presentation on theme: "28/10/1999POS-A1 The Synchronization Problem Synchronization problems occur because –multiple processes or threads want to share data; –the executions."— Presentation transcript:

1 28/10/1999POS-A1 The Synchronization Problem Synchronization problems occur because –multiple processes or threads want to share data; –the executions of these processes interleave in arbitrary fashions.

2 28/10/1999POS-A2 A Simple Example Two processes trying to increment a common variable V (suppose V = 0 initially): Process 1 : load V,R add 1,R store R,V Process 2 : load V,R add 1,R store R,V load V,R load V,R add 1,R add 1,R store R,V store R,V V load V,R add 1,R store R,V V = 1V = 2

3 28/10/1999POS-A3 Critical Section It can be easily seen that the “load” of one process must follow the “store” of the other process. The three instructions must be executed atomically, thus forming a critical section (CS). –Ie., when one process is in its critical section, no other process can be in its critical section at the same time.

4 28/10/1999POS-A4 2-Process CS Algorithms 1 & 2 do not satisfy the progress requirement. –To prove an algorithm not satisfying a requirement, we only need to find one case (ie., “there exists”). –To prove an algorithm satisfying a requirement, we have to prove that it does so for all cases (ie., “for all”).

5 28/10/1999POS-A5 One Failure Case of Algo. 1 while turn  0 do no-op; CS turn = 1; exit; // remainder section while turn  1 do no-op; CS turn = 0; remainder section; while turn  1 do no-op; time No more progress!

6 28/10/1999POS-A6 One Failure Case of Algo. 2 flag[0] = true; flag[1] = true; while flag[0] do no-op; flag[0] = true; flag[1] = true; while flag[1] do no-op; No progress either case!

7 28/10/1999POS-A7 Algorithm 3 – Mutual Exclusion “turn”, a shared variable, is either 0 or 1, and hence only one process can pass through the while statement at a time. –This is a direct proof.

8 28/10/1999POS-A8 Proving “For All” Instead of a direct proof, to prove “for all”, we could use proof by contradiction. –Assume there exists a case not satisfying a requirement; show that such a case won’t exist.

9 28/10/1999POS-A9 Algorithm 3 - Progress Proof by contradiction Assume there exists a time when both processes are executing the while statement and cannot get into the CS. –Then both “turn = 0” and “turn = 1” must be true – an impossible case! The assumption cannot be true.

10 28/10/1999POS-A10 Algorithm 3 – Bounded Waiting Suppose P 0 is executing in the CS, and P 1 is waiting (executing the while). –“flag[0]” and “turn = 0” in P 1 ’s while statement are true. –When P 0 exits the CS, it sets “flag[0]” to false. –Two possibilities at this point: either P 0 continues to execute or P 1 enters the CS. »Cont’d …

11 28/10/1999POS-A11 Bounded Waiting (cont’d) –Suppose P 0 continues to execute and arrives at its while statement. It will be stuck at the while statement because both “flag[1]” and “turn = 1” would be true. –Hence, in either case, P 1 is the next one to enter the CS.

12 28/10/1999POS-A12 The Bakery Algorithm – Solving the n-process CS The counter can serve only one customer at a time. The counter is the CS. When a customer enters the bakery, it picks a number which is 1 + the largest number being held by a existing customer. »Cont’d …

13 28/10/1999POS-A13 Bakery Algorithm (cont’d) To get to the counter, the customer compares its number with every one of the existing customers. –It holds a bit when coming to a customer, i, who is in the middle of getting its number (the “choosing[i]” variable is used). –If its number is smaller than all existing numbers, the customer gets the counter.


Download ppt "28/10/1999POS-A1 The Synchronization Problem Synchronization problems occur because –multiple processes or threads want to share data; –the executions."

Similar presentations


Ads by Google