130 likes | 158 Views
The Synchronization Problem. Synchronization problems occur because multiple processes or threads want to share data; the executions of these processes interleave in arbitrary fashions. A Simple Example. Two processes trying to increment a common variable V (suppose V = 0 initially):.
E N D
The Synchronization Problem • Synchronization problems occur because • multiple processes or threads want to share data; • the executions of these processes interleave in arbitrary fashions. POS-A
A Simple Example • Two processes trying to increment a common variable V (suppose V = 0 initially): load V,Rload V,Radd 1,Radd 1,Rstore R,Vstore R,V load V,Radd 1,Rstore R,Vload V,Radd 1,Rstore R,V Process1:load V,Radd 1,Rstore R,V Process2:load V,Radd 1,Rstore R,V V = 1 V = 2 V POS-A
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. POS-A
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”). POS-A
One Failure Case of Algo. 1 while turn 0 do no-op; CSturn = 1; exit; // remainder sectionwhile turn 1 do no-op; CSturn = 0; remainder section;while turn 1 do no-op; time No more progress! POS-A
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! POS-A
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. POS-A
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. POS-A
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. POS-A
Algorithm 3 – Bounded Waiting • Suppose P0 is executing in the CS, and P1 is waiting (executing the while). • “flag[0]” and “turn = 0” in P1’s while statement are true. • When P0 exits the CS, it sets “flag[0]” to false. • Two possibilities at this point: either P0 continues to execute or P1 enters the CS. • Cont’d … POS-A
Bounded Waiting (cont’d) • Suppose P0 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, P1 is the next one to enter the CS. POS-A
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 … POS-A
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. POS-A