Title: The Synchronization Problem
1The Synchronization Problem
- Synchronization problems occur because
- multiple processes or threads want to share data
- the executions of these processes interleave in
arbitrary fashions.
2A 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
Process1load V,Radd 1,Rstore R,V
Process2load V,Radd 1,Rstore R,V
V 1
V 2
V
3Critical 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.
42-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).
5One Failure Case of Algo. 1
while turn ? 0 do no-op CSturn 1 exit //
remainder sectionwhile turn ? 1 do
no-op CSturn 0 remainder sectionwhile
turn ? 1 do no-op
time
No more progress!
6One Failure Case of Algo. 2
flag0 trueflag1 truewhile flag0 do
no-op
flag0 trueflag1 truewhile flag1 do
no-op
No progress either case!
7Algorithm 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.
8Proving 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 wont exist.
9Algorithm 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.
10Algorithm 3 Bounded Waiting
- Suppose P0 is executing in the CS, and P1 is
waiting (executing the while). - flag0 and turn 0 in P1s while statement
are true. - When P0 exits the CS, it sets flag0 to false.
- Two possibilities at this point either P0
continues to execute or P1 enters the CS. - Contd
11Bounded Waiting (contd)
- Suppose P0 continues to execute and arrives at
its while statement. - It will be stuck at the while statement because
both flag1 and turn 1 would be true. - Hence, in either case, P1 is the next one to
enter the CS.
12The 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. - Contd
13Bakery Algorithm (contd)
- 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
choosingi variable is used). - If its number is smaller than all existing
numbers, the customer gets the counter.