Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

Synchronization Algorithms

and Concurrent Programming


Gadi Taubenfeld
Chapter 10
Timing-based Algorithms

Version: June 2014


Chapter 10 Synchronization Algorithms and Concurrent Programming 1
Gadi Taubenfeld © 2014
Synchronization Algorithms
and Concurrent Programming
ISBN: 0131972596, 1st edition

A note on the use of these ppt slides:


I am making these slides freely available to all (faculty, students, readers).
They are in PowerPoint form so you can add, modify, and delete slides and slide
content to suit your needs. They obviously represent a lot of work on my part.
In return for use, I only ask the following:

q That you mention their source, after all, I would like people to use my book!
q That you note that they are adapted from (or perhaps identical to)
my slides, and note my copyright of this material.

Thanks and enjoy!


Gadi Taubenfeld
All material copyright 2014
Gadi Taubenfeld, All Rights Reserved

To get the most updated version of these slides go to:


http://www.faculty.idc.ac.il/gadi/book.htm
Chapter 10 Synchronization Algorithms and Concurrent Programming 2
Gadi Taubenfeld © 2014
Chapter 10
Timing-based Algorithms

10.1 Timing-based Algorithms


10.2 Mutual Exclusion with Known Delays
10.3 Fast Mutual Exclusion with Known Delays
10.4 Consensus with Known Delays
10.5 Fast Consensus with Known Delays
10.6 Fast Consensus with Unknown Delays
10.7 Fast Mutual Exclusion with Unknown Delays

We will also discuss the (recent) subject of “Computing


in the Presence of Timing Failures” which does not
appear in the 1st edition of the book.

Chapter 10 Synchronization Algorithms and Concurrent Programming 3


Gadi Taubenfeld © 2014
Shared Memory Models

P ... P P ... P P ... P

C C M M

M M

Simple Coherent Distributed


Shared Caching Shared
Memory Memory

q Communication via shared objects, plus


q Timing assumptions.

Chapter 10 Synchronization Algorithms and Concurrent Programming 4


Gadi Taubenfeld © 2014
Time
models of computation

Synchronous Partially Asynchronous


synchronous

• extremely powerful • less powerful


• unrealistic • realistic

Chapter 10 Synchronization Algorithms and Concurrent Programming 5


Gadi Taubenfeld © 2014
Modeling Timing-based Systems

q Asynchronous: no bound on how much time it takes to


access a shared register.
q Known Delays : Such a bound exists and is known.
q Unknown Delays: Such a bound exists but is not known.

D – the time it takes the slowest process to complete a


single access to a shared register.
delay(d) – delay for d time units.

Chapter 10 Synchronization Algorithms and Concurrent Programming 6


Gadi Taubenfeld © 2014
Mutual Exclusion with Known Delays
Section 10.2

Chapter 10 Synchronization Algorithms and Concurrent Programming 7


Gadi Taubenfeld © 2014
Mutual Exclusion

remainder code

entry code

critical section

exit code

Chapter 10 Synchronization Algorithms and Concurrent Programming 8


Gadi Taubenfeld © 2014
Mutual Exclusion

Implement a mutual exclusion algorithm for


unknown number of processes with constant
number of atomic registers and constant
time complexity.

Impossible for an asynchronous model,


possible for timing-based model.

Chapter 10 Synchronization Algorithms and Concurrent Programming 9


Gadi Taubenfeld © 2014
Notation

await (condition) == while (not condition) do skip od

Chapter 10 Synchronization Algorithms and Concurrent Programming 10


Gadi Taubenfeld © 2014
Fischer’s algorithm
process pi program

x 0

1 repeat
2 await(x = 0)
3 x=i
4 delay(D)
5 until x = i
6 critical section
7 x=0
// not fast

await (condition) == while (not condition) do skip od

Chapter 10 Synchronization Algorithms and Concurrent Programming 11


Gadi Taubenfeld © 2014
Fischer’s algorithm
process pi program

x 0

1 repeat
Can we replace
2 await(x = 0)
the order of
3 x=i these two lines?
4 delay(D)
5 until x = i
6 critical section
7 x=0
// not fast

Chapter 10 Synchronization Algorithms and Concurrent Programming 12


Gadi Taubenfeld © 2014
Fast Mutual Exclusion with Known Delays
Section 10.3

Chapter 10 Synchronization Algorithms and Concurrent Programming 13


Gadi Taubenfeld © 2014
A fast algorithm
process pi program

x y 0 z 0

1 start: x=i;
2 await (y = 0)
3 y=i
4 if x ¹ i then delay(2D)
5 if y ¹ i then goto start fi
6 await (z = 0)
7 else z = 1 fi
8 critical section
9 z=0;
10 if y = i then y = 0 fi

Chapter 10 Synchronization Algorithms and Concurrent Programming 14


Gadi Taubenfeld © 2014
A fast algorithm
process pi program

x y 0 z 0

1 start: x=i; Why delay(2D) and


2 await (y = 0) not delay(3D) ?
3 y=i
4 if x ¹ i then delay(2D)
5 if y ¹ i then goto start fi
6 await (z = 0)
7 else z = 1 fi
8 critical section
9 z=0;
10 if y = i then y = 0 fi

Chapter 10 Synchronization Algorithms and Concurrent Programming 15


Gadi Taubenfeld © 2014
A fast algorithm
process pi program

x y 0 z 0

1 start: x=i;
2 await (y = 0)
3 y=i
4 if x ¹ i then delay(2D)
5 if y ¹ i then goto start fi
6 await (z = 0)
7 else z = 1 fi
8 critical section Can we replace this
9 z=0; statement with: y = 0 ?
10 if y = i then y = 0 fi

Chapter 10 Synchronization Algorithms and Concurrent Programming 16


Gadi Taubenfeld © 2014
A fast algorithm
process pi program

x y 0 z 0

1 start: x=i;
2 await (y = 0)
3 y=i
4 if x ¹ i then delay(2D)
5 if y ¹ i then goto start fi
6 await (z = 0)
7 else z = 1 fi Can we replace the
8 critical section order of the last
9 z=0; two lines?
10 if y = i then y = 0 fi

Chapter 10 Synchronization Algorithms and Concurrent Programming 17


Gadi Taubenfeld © 2014
A fast algorithm
process pi program

x y 0 z 0

1 start: x=i;
2 await (y = 0) Can we replace
the order of
3 y=i
these two lines?
4 if x ¹ i then delay(2D)
5 if y ¹ i then goto start fi
6 await (z = 0)
7 else z = 1 fi
8 critical section
9 z=0;
10 if y = i then y = 0 fi

Chapter 10 Synchronization Algorithms and Concurrent Programming 18


Gadi Taubenfeld © 2014
A fast algorithm
process pi program

x y 0 z 0
Can we replace this statement with:
1 start: x=i; if y ¹ 0 then goto start fi ?
2 await (y = 0)
3 y=i
4 if x ¹ i then delay(2D)
5 if y ¹ i then goto start fi
6 await (z = 0)
7 else z = 1 fi
8 critical section
9 z=0;
10 if y = i then y = 0 fi

Chapter 10 Synchronization Algorithms and Concurrent Programming 19


Gadi Taubenfeld © 2014
A fast algorithm
process pi program

x y 0 z 0

1 start: x=i;
2 await (y = 0)
3 y=i
4 if x ¹ i then delay(2D)
5 if y ¹ i then goto start fi
6 await (z = 0)
7 else z = 1 fi
8 critical section Can we replace this statement
9 z=0; with:
10 if y = i then y = 0 fi if z ¹ 0 then goto start fi ?

Chapter 10 Synchronization Algorithms and Concurrent Programming 20


Gadi Taubenfeld © 2014
Properties

q Mutual exclusion & Deadlock freedom


q In the absence of contention only 8
steps are required
q In the presence of contention, delay
of 2•D used to resolve contention
q space: 3 registers
q work for unknown # of processes

Chapter 10 Synchronization Algorithms and Concurrent Programming 21


Gadi Taubenfeld © 2014
Consensus with Known Delays
Section 10.4

Chapter 10 Synchronization Algorithms and Concurrent Programming 22


Gadi Taubenfeld © 2014
Consensus

1 0 1 1 0 1 0

1 1 1 1 1 1 1

requirements: Thm:
q validity There is no asynchronous
q agreement solution to the problem
using atomic registers only.
q termination
+ Wait-freedom

Chapter 10 Synchronization Algorithms and Concurrent Programming 23


Gadi Taubenfeld © 2014
Wait-free Consensus
Each process has input Î {0,1} and
has to output a value Î {0,1}, such that:

Agreement: all output the same value.


Validity: output should be the input of some process
termination:
+ wait-free

Recall that there is no solution to the problem using


atomic registers only.

Chapter 10 Synchronization Algorithms and Concurrent Programming 24


Gadi Taubenfeld © 2014
Known delay: Consensus
process pi with input in.i

x ^

1 if x = ^ then x = in.i fi
2 delay(D)
3 decide (x)

Chapter 10 Synchronization Algorithms and Concurrent Programming 25


Gadi Taubenfeld © 2014
Known delay: Fast Consensus
process pi with input in.i

x[0] 0 x[1] 0 y ^

1 x[in.i] = 1
2 if y = ^ then y = in.i fi
3 if x[1– in.i] = 1 then delay(D) fi
4 decide (y)

Chapter 10 Synchronization Algorithms and Concurrent Programming 26


Gadi Taubenfeld © 2014
Known delay: Fast Consensus
process pi with input in.i

x[0] 0 x[1] 0 y ^

Can we replace
1 x[in.i] = 1
the order of
2 if y = ^ then y = in.i fi these two lines?
3 if x[1– in.i] = 1 then delay(D) fi
4 decide (y)

Chapter 10 Synchronization Algorithms and Concurrent Programming 27


Gadi Taubenfeld © 2014
Fast Consensus with Unknown Delays
Section 10.5

Chapter 10 Synchronization Algorithms and Concurrent Programming 28


Gadi Taubenfeld © 2014
Unknown delay: Fast Consensus
process pi with input in.i

x 0 y round v out
0 ^ 1 in.i ^

1 while out = ^ do
2 x[round,v] = 1
3 if y[round] = ^ then y[round] = v fi
4 if x[round, 1-v] = 0 then out = v
5 else delay(round)
6 v = y[round]
7 round = round + 1 fi od
8 output(out).
// delay(r) ~ for j=1 to r do skip od

Chapter 10 Synchronization Algorithms and Concurrent Programming 29


Gadi Taubenfeld © 2014
Unknown delay: Fast Consensus
process pi with input in.i
Can processes decide
on conflicting values if
the delay statement is
deleted?

1 while out = ^ do
2 x[round,v] = 1
3 if y[round] = ^ then y[round] = v fi
4 if x[round, 1-v] = 0 then out = v
5 else delay(round)
6 v = y[round]
7 round = round + 1 fi od
8 output(out).
// delay(r) ~ for j=1 to r do skip od

Chapter 10 Synchronization Algorithms and Concurrent Programming 30


Gadi Taubenfeld © 2014
Unknown delay: Fast Consensus
process pi with input in.i

Can we replace
the order of
1 while out = ^ do these two lines?
2 x[round,v] = 1
3 if y[round] = ^ then y[round] = v fi
4 if x[round, 1-v] = 0 then out = v
5 else delay(round)
6 v = y[round]
7 round = round + 1 fi od
8 output(out).
// delay(r) ~ for j=1 to r do skip od

Chapter 10 Synchronization Algorithms and Concurrent Programming 31


Gadi Taubenfeld © 2014
Unknown delay: Fast Consensus
process pi with input in.i

Can we replace with


delay (2 x round) ?
1 while out = ^ do
2 x[round,v] = 1
3 if y[round] = ^ then y[round] = v fi
4 if x[round, 1-v] = 0 then out = v
5 else delay(round)
6 v = y[round]
7 round = round + 1 fi od
8 output(out).
// delay(r) ~ for j=1 to r do skip od

Chapter 10 Synchronization Algorithms and Concurrent Programming 32


Gadi Taubenfeld © 2014
A comment about the previous consensus
algorithm for the unknown delay model

delay() number of rounds time


round D O (D^2)
round ! O (log D / log log D) O (D log D / log log D)

Tight lower bound (for problem): W(D log D / log log D)

Chapter 10 Synchronization Algorithms and Concurrent Programming 33


Gadi Taubenfeld © 2014
Conclusions from consensus

Model Time
Asynchronous: impossible
Unknown: q(D log D / log log D)
Known: q(D)

Chapter 10 Synchronization Algorithms and Concurrent Programming 34


Gadi Taubenfeld © 2014
Computing in the Presence of Timing
Failures
Additional materiel not from the book

Based on the paper:


Computing in the presence of timing failures. G. Taubenfeld.
The 26th International Conference on Distributed Computing
Systems, July 2006, Lisboa, Portugal.

The paper can be downloaded from:


http://www.faculty.idc.ac.il/gadi/MyPapers/2006T-timingFailures.pdf

Chapter 10 Synchronization Algorithms and Concurrent Programming 35


Gadi Taubenfeld © 2014
Computing in the Presence of Timing Failures

A timing failure refers to a situation where the timing


constraints of a timing-based system are not met.

In particular, a timing failure occurs when it takes more


than D time units for a process to execute a statement
which involves an access to a shared memory location.

Chapter 10 Synchronization Algorithms and Concurrent Programming 36


Gadi Taubenfeld © 2014
Timing Failures: What can happen?

Synchronous Partially Asynchronous


• extremely powerful synchronous • less powerful
• unrealistic • realistic

• Incorrect behavior
• Satisfies safety but not liveness properties
• Correct but inefficient

• What if the timing failure is transient ?

Chapter 10 Synchronization Algorithms and Concurrent Programming 37


Gadi Taubenfeld © 2014
Goal: Tolerating Timing Failures

Many systems exhibit a significant degree of


synchrony in practice, but few guarantee to do so

q Exploiting synchrony when it is available, but


q In any case guaranteeing correctness regardless
of the timing behavior of the system

A detailed definition later ...

Chapter 10 Synchronization Algorithms and Concurrent Programming 38


Gadi Taubenfeld © 2014
Are Timing-based Algorithms practical ?
q The value of D can be huge
page faults
m
m memory contention
P ... P
m preemption
q Use optimistic evaluation for D

q è timing failures, but its ok! M

Simple
Shared
Memory

Chapter 10 Synchronization Algorithms and Concurrent Programming 39


Gadi Taubenfeld © 2014
Computing in the Presence of Timing Failures
An algorithm is resilient to timing failures, w.r.t. time
complexity T if it satisfies the following requirements,

Stabilization. After a transient timing failure has


terminated, all the properties immediately hold,
assuming no more failures occur in the future.
Furthermore, all the safety properties always hold
(even during timing failures).

Efficiency. In the absence of timing failures the time


complexity of the algorithm should be T.

Convergence. A finite number of time units after all


timing failures stop, the time complexity is again T.

Chapter 10 Synchronization Algorithms and Concurrent Programming 40


Gadi Taubenfeld © 2014
Fast Timing-base Consensus
process pi with input in.i

x[0] 0 x[1] 0 y ^

Can it tolerate
timing failures?
1 x[in.i] = 1
No.
2 if y = ^ then y = in.i fi
3 if x[1– in.i] = 1 then delay(D) fi
4 decide (y)

Chapter 10 Synchronization Algorithms and Concurrent Programming 41


Gadi Taubenfeld © 2014
Consensus in the Presence of Timing Failures
process pi with input in.i

x[0] 0 x[1] 0 y ^

1 x[in.i] = 1
2 if y = ^ then y = in.i fi
round 1
3 if x[1– in.i] = 1 then delay(D) else decide (y) fi
4 in.i = y

1 x[in.i] = 1
2 if y = ^ then y = in.i fi
round 2
3 if x[1– in.i] = 1 then delay(D) else decide (y) fi
4 in.i = y

Chapter 10 Synchronization Algorithms and Concurrent Programming 42


Gadi Taubenfeld © 2014
Consensus in the Presence of Timing Failures
process pi with input in.i

x 0 y round v out
0 ^ 1 in.i ^

1 while out = ^ do
2 x[round,v] = 1
3 if y[round] = ^ then y[round] = v fi
4 if x[round, 1-v] = 0 then out = v
5 else delay(D)
6 v = y[round]
7 round = round + 1 fi od
8 output(out).

Chapter 10 Synchronization Algorithms and Concurrent Programming 43


Gadi Taubenfeld © 2014
Consensus in the Presence of Timing Failures
process pi with input in.i

Can processes decide on


conflicting values
without the delay? NO.
1 while out = ^ do
2 x[round,v] = 1
3 if y[round] = ^ then y[round] = v fi
4 if x[round, 1-v] = 0 then out = v
5 else delay(D)
6 v = y[round]
7 round = round + 1 fi od
8 output(out).

Chapter 10 Synchronization Algorithms and Concurrent Programming 44


Gadi Taubenfeld © 2014
Properties

q In the absence of timing failures, each


process decides after at most c•D time units.
q When there are no more timing failures after
round r, decision is made by round r+1.
q Wait-free: can tolerate any number of
process failures.
q No need to know the number of processes.
q Validity, agreement, termination.

Chapter 10 Synchronization Algorithms and Concurrent Programming 45


Gadi Taubenfeld © 2014
Mutual Exclusion in the Presence of Timing Failures

Fischer’s algorithm Let A be a fast


starvation-free
1 repeat algorithm
2 await(x = 0)
3 x=i 1 entry section of A
4 delay(D) 2 critical section
5 until x = i 3 exit section of A
6 critical section
7 x=0

Chapter 10 Synchronization Algorithms and Concurrent Programming 46


Gadi Taubenfeld © 2014
Mutual Exclusion in the Presence of Timing Failures
process pi with input in.i

1 repeat
2 await(x = 0)
3 x=i
4 delay(D)
5 until x = i
6 entry section of A
7 critical section
8 exit section of A
9 if x = i then x = 0 fi

A is a fast starvation-free algorithm

Chapter 10 Synchronization Algorithms and Concurrent Programming 47


Gadi Taubenfeld © 2014
Time complexity

The longest time interval where


some process is in its entry code
while no process is in its critical time
section.

(assuming there is an upper bound


of D time units for step time in
the entry or exit code and no
lower bound.)

Chapter 10 Synchronization Algorithms and Concurrent Programming 48


Gadi Taubenfeld © 2014
The Algorithm is resilient to Timing Failures

q Stabilization: Satisfies mutual exclusion


& deadlock freedom (before, during &
after timing failures).
q Efficiency: In the absence of timing
failures, time complexity is c•D .
q During timing failures, time complexity
is the complexity of M + c•D
q Convergence: Guaranteed to converge,
after timing failures stop.

Chapter 10 Synchronization Algorithms and Concurrent Programming 49


Gadi Taubenfeld © 2014
Summary
q Weaknesses of the asynchronous model
q Timing-based models (known, unknown)
q Algorithms (consensus, mutex)

Are all the impossibility results for


asynchronous systems important, given that
they do not hold for the known-delay model
and the unknown-delay model ?

Is these timing models a better way out of


the FLP dilemma ? (FLP == the impossibility
result for consensus in the presence of one
faulty process, proved in Section 9.5)

Chapter 10 Synchronization Algorithms and Concurrent Programming 50


Gadi Taubenfeld © 2014
The End

Chapter 10 Synchronization Algorithms and Concurrent Programming 51


Gadi Taubenfeld © 2014

You might also like