69
1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld Chapter 1 Introduction Version: June 2014

1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

Embed Size (px)

Citation preview

Page 1: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

1Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld

Chapter 1 Introduction

Version: June 2014

Page 2: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

2Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

A note on the use of these power-point 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:

That you mention their source, after all, I would like people to use my

book!

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 2014Gadi Taubenfeld, All Rights Reserved

A note on the use of these power-point 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:

That you mention their source, after all, I would like people to use my

book!

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 2014Gadi Taubenfeld, All Rights Reserved

Synchronization Algorithms and Concurrent Programming

ISBN: 0131972596, 1st edition

To get the most updated version of these slides go to: http://www.faculty.idc.ac.il/gadi/book.htm

Page 3: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

3Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Synchronization

Type of synchronization Contention Coordination

Why synchronization is Difficult? Easy between humans Communication between

computers is restricted• reading and writing of notes• sending and receiving

messages

Type of synchronization Contention Coordination

Why synchronization is Difficult? Easy between humans Communication between

computers is restricted• reading and writing of notes• sending and receiving

messages

Page 4: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

4

Too Much Milk

Section 1.2.2

Chapter 1

Page 5: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

5Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

The Too-Much-Milk Problem

Time Alice Bob

5:00 Arrive Home

5:05 Look in fridge; no milk

5:10 Leave for grocery

5:15 Arrive home

5:20 Arrive at grocery Look in fridge; no milk

5:25 Buy milk Leave for grocery

5:30 Arrive home; put milk in fridge

3:40 Buy milk

3:45 Arrive home; too much milk!

Page 6: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

6Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Solving the Too-Much-Milk Problem

Correctness properties Mutual Exclusion:

Only one person buys milk “at a time”

Progress: Someone always buys milk if it is needed

Communication primitives

Leave a note (set a flag)

Remove a note (reset a flag)

Read a note (test the flag)

Page 7: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

7Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Important Distinction

Safety Property Nothing bad happens ever Example: mutual exclusion

Liveness Property Something good happens

eventually Example: progress

Safety Property Nothing bad happens ever Example: mutual exclusion

Liveness Property Something good happens

eventually Example: progress

Page 8: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

8Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Solution 1

Does it work?

Aliceif (no note) { if (no milk) { leave Note buy milk remove note }}

Bobif (no note) { if (no milk) { leave Note buy milk remove note }}

No, may end up with too much milk.

C mutual exclusionprogress

Page 9: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

9Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Solution 2

Aliceleave note Aif (no note B) { if (no milk) {buy milk}}remove note A

Bobleave note Bif (no note A) { if (no milk) {buy milk}}remove note B

Using labeled notes

Does it work? No, may end up with no milk.

mutual exclusionC progress

Page 10: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

10Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Solution 3

Does it work?

Aliceleave note Awhile (note B) {skip}if (no milk) {buy milk}

remove note A

Bobleave note Bif (no note A) { if (no milk) {buy milk}} remove note B

No ! a timing assumption is needed

mutual exclusionC progress

Bob’s code is as before

Page 11: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

11Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Is solution #3 a good solution? No

A timing assumption is needed! Unfair to one of the processes It is asymmetric and complicated Alice is busy waiting – consuming CPU

cycles without accomplishing useful work

A timing assumption is needed! Unfair to one of the processes It is asymmetric and complicated Alice is busy waiting – consuming CPU

cycles without accomplishing useful work

Page 12: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

12Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Solution 3+

Does it work?

Aliceleave note Awhile (note B) {skip}if (no milk) {buy milk}

remove note A

Bobrepeat leave note B if (note A) { remove note B; while (note A) {skip} }until (note B) if (no milk) {buy milk}remove note B

Yes, BUT … mutual exclusionprogress

Alice code is as before

BUT, Bob may forever get stuck in the repeat-until

loop !!!

Additional requirement [informal definition]

Thirst-freedom: Nobody ever gets stuck forever.(Both Alice and Bob eventually complete their programs.)

Page 13: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

13Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Solution 4Using 4 labeled

notes

A1 A2 B2 B1

the fridge’s door

Page 14: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

14Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Solution 4Notations

color:there is a note A1 A2 B2

Alice’s turn

B2dotted line:there is no note

solid line without color:we do not care if there is a note

who will buy milk ?

XX

Page 15: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

15Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

A1 A2 B2

Alice’s turn

A1 A2 B2 B1

Alice’s turn

A2 B2 B1

Bob’s turn

A1 A2 B2 B1

Alice’s turn

A1 A2 B2 B1

Bob’s turn

A1 A2 B2 B1

Bob’s turn

Solution 4

B2

B2

X X

X X

Page 16: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

16Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Aliceleave A1if B2 {leave A2} else {remove A2}while B1 and ((A2 and B2) or (no A2 and no B2)) {skip} if (no milk) {buy milk} remove A1

Bobleave B1if (no A2) {leave B2} else {remove B2}while A1 and ((A2 and no B2) or (no A2 and B2)) {skip} if (no milk) {buy milk} remove note B1

Solution 4

A correct, fair, symmetric algorithm! mutual exclusion progress both Alice and Bob eventually

complete their programs

Page 17: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

17Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

A variant of Solution 4The order of the first two statements is replaced

Is it correct ?

No, may end up with two milk.C mutual exclusionprogress

Aliceif B2 {leave A2} else {remove A2}leave A1while B1 and ((A2 and B2) or (no A2 and no B2)) {skip} if (no milk) {buy milk} remove A1

Bobif (no A2) {leave B2} else {remove B2}leave B1while A1 and ((A2 and no B2) or (no A2 and B2)) {skip} if (no milk) {buy milk} remove note B1

MILKMILK

A1 B2 B1

Page 18: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

18Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Question

Alice and Bob have a daughter C.Make it work for all the three of

them.

In Chapter 2, we solve a generalization of the too-much-milk problem, called the mutual exclusion problem, for any number of participants (not just two or three).

Page 19: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

19Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Question

Process Afor i = 1 to 5 { x:=x+1 }

Process Bfor i = 1 to 5 { x:=x+1 }

x is an atomic register, initially 0

Q: What are all the possible values for x after both processes terminate?

A: 2 through 10 inclusive.

00

Page 20: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

20Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

The smallest value is 2

00

A

0 1

B

0 1

112233441122334455

1 2

22

Page 21: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

21Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Question

Process Ax:=x+1;x:=x+1

Process Bx:=x+1;x:=x+1

Process Cx:=10

x is an atomic register, initially 0

Q: What are all the possible values for x after the three processes terminate?

A: 2,3,4,10,11,12,13,14.

00

Page 22: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

22

The Coordinated Attack Problem

Section 1.2.3

Chapter 1

Page 23: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

23Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

The coordinated attack problem• Blue army wins if both blue camps attack

simultaneously• Design an algorithm to ensure that both blue camps attack

simultaneously

Enemy

1 2

The problem is due to Jim Gray (1977)

Page 24: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

24Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

The coordinated attack problem• Communication is done by sending messengers across

the valley• Messengers may be lost or captured by the enemy

Enemy

1 2

Page 25: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

25Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

The coordinated attack problem

Theorem: There is no algorithm that solves the problem !

• Assume to the contrary that such an algorithm exits.• Let P be an algorithm that solves with fewest # of

messages, when no message is lost. • P should work even when the last messenger is

captured.• So P should work even if the last messenger is never

sent.• But this is a new algorithm with one less message.• A contradiction.

Proof:

The (asynchronous) model is too weak.

Page 27: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

27Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Synchronization

Contention Too-much-milk (1) mutual exclusion

(2,3,4) Concurrent Data

Structures (4) l-exclusion (6) multiple resources:

dinning philosophers (7)

readers & writer (8) group-exclusions (8) ...

Contention Too-much-milk (1) mutual exclusion

(2,3,4) Concurrent Data

Structures (4) l-exclusion (6) multiple resources:

dinning philosophers (7)

readers & writer (8) group-exclusions (8) ...

Coordination The coordinated attack

(1) barrier synchronization

(5) Concurrent Data

Structures (4) producer-consumer (8) Choice Coordination (8) Consensus (data

coordination) (9) ....

Coordination The coordinated attack

(1) barrier synchronization

(5) Concurrent Data

Structures (4) producer-consumer (8) Choice Coordination (8) Consensus (data

coordination) (9) ....

(The numbers refer to chapters in the book.)

Page 28: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

28Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

The first problem we cover in detail. A generalization of the too-much-milk problem.

The mutual exclusion problemChapters 2+3

Page 29: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

29Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Blocking and Non-blocking SynchronizationConcurrent data structures

P1 P2 P3

data structure

counter, stack, queue, link list

Chapter 4

Page 30: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

30Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Blocking

P1 P2 P3

data structure

Chapter 4

Page 31: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

31Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Non-blocking

P1 P2 P3

data structure

Chapter 4

Page 32: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

32Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Coarse-Grained Locking

Easier to program, but not scalable.

Page 33: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

33Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Fine-Grained Locking

Efficient and scalable, but too complicated (deadlocks, etc.).

Page 34: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

34Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Barr

ier

P1P1

P2P2

P3P3

P4P4

Barr

ier

P1P1

P2P2

P3P3

P4P4

P1P1

P2P2

P3P3

P4P4

time

Barr

ier

Barrier SynchronizationChapter 5

four processes approach the barrier

all except P4 arrive

Once all arrive, they continue

Page 35: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

35Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Example: Prefix Sum

abegin b c d e f

aend a+b a+b+c a+b+c+d a+b+c+d+e

a+b+c+d+e+f

time

Page 36: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

36Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Example: Sequential Prefix Sum

abegin b c d e f

a a+b c d e f

a a+b a+b+c a+b+c+d e f

aend a+b a+b+c a+b+c+d a+b+c+d+e

a+b+c+d+e+f

a a+b a+b+c d e f

a a+b a+b+c a+b+c+da+b+c+d+e f

time

Page 37: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

37Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Example: Parallel Prefix Sum

abegin b c d e f

a a+b b+c c+d d+e e+f

a a+b a+b+c a+b+c+db+c+d+ec+d+e+f

aend a+b a+b+c a+b+c+d a+b+c+d+e

a+b+c+d+e+f

time

Page 38: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

38Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Example: Parallel Prefix Sum

abegin b c d e f

a a+b b+c c+d d+e e+f

a a+b a+b+c a+b+c+db+c+d+ec+d+e+f

aend a+b a+b+c a+b+c+d a+b+c+d+e

a+b+c+d+e+f

barrier

barriertime

Page 39: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

39Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Consensus

1

1

0

1

1

1

1

1

0

1

1

1

0

1

requirements: validity agreement termination

Chapter 9

Page 40: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

40Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Impossibility of Consensus with One Faulty Process

What can be done? Strong primitives. Timing assumptions. Randomizations.

Theorem: There is no algorithm that solve the consensus problem using atomic read/write registers in the presence of a single faulty process.We give a detailed proof of this important result in Chapter 9 of the book.

Page 41: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

41

Models of Computation

Section 1.4

Chapter 1

Page 42: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

42Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Communicationmodels of computation

Concurrent programming Shared memory safe registers atomic registers (read/write) test&set swap compare-and-swap load-link/store-conditional read-modify-write semaphore monitor

Concurrent programming Shared memory safe registers atomic registers (read/write) test&set swap compare-and-swap load-link/store-conditional read-modify-write semaphore monitor

What is the relative power of these communication primitives?Answer: see Chapter 9.

Page 43: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

43Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Communicationmodels of computation

Message passing send/receive multi-cast broadcast network topology

Message passing send/receive multi-cast broadcast network topology

Distributed message passing systems are not covered in the book

Page 44: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

44Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Timemodels of computation

Synchronous(CRCW, EREW)

AsynchronousPartiallysynchronous

What is the relative power of these timing models?

Concurrent/Distributed

Parallel

This book

• less powerful• realistic

• extremely powerful• unrealistic

Page 45: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

45Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

. . .

M

. . .

C

. . .

M

C

P P P P P P

Coherent Caching

Distributed Shared Memory

Simple Shared Memory

Shared Memory Models models of computation

M M

Page 46: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

46Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

How many can fail ? constant,

n/3, ...

Wait-freedom, Non-blocking, Lock-freedom

what is it ?

Self-stabilization what is it ?

How many can fail ? constant,

n/3, ...

Wait-freedom, Non-blocking, Lock-freedom

what is it ?

Self-stabilization what is it ?

Fault-tolerancemodels of computation

What can fail? processes shared memory links

Type of faults crash omission Byzantine

What can fail? processes shared memory links

Type of faults crash omission Byzantine

Page 47: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

47Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

# of Processes: known, unknown, infinite

Processes & Concurrencymodels of computation

Page 48: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

48Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Processes: known, unknown, infinite Concurrency: finite, bounded, unbounded

Processes & Concurrencymodels of computation

Page 49: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

49Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Processes: known, unknown, infinite Concurrency: finite, bounded, unbounded Faults: known, unknown

Processes & Concurrencymodels of computation

Page 50: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

50Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Processes: known, unknown, infinite Concurrency: finite, bounded, unbounded Faults: known, unknown Participation: required / not required

Processes & Concurrencymodels of computation

Page 51: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

51Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Model Summary

Multiple processes (or threads) Shared memory Communication via shared objects Unpredictable asynchronous delays

Cover all the classical synchronization problems

Focus more on principles, less on performance

Look for simple elegant short algorithms

Multiple processes (or threads) Shared memory Communication via shared objects Unpredictable asynchronous delays

Cover all the classical synchronization problems

Focus more on principles, less on performance

Look for simple elegant short algorithms

Page 52: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

52

Processes, Threads and Scheduling

Section 1.5

Chapter 1

Page 53: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

53Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Terminology

Hardware Processors

Software Threads, processes

Sometimes OK to confuse them, sometimes not.

Scheduler

Hardware Processors

Software Threads, processes

Sometimes OK to confuse them, sometimes not.

Scheduler

Page 54: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

54Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Processes and Threads A process is a program in execution

Three processes each with one or more threads

KernelKernelspace

Userspace

Process 1

Threads

Process 1 Process 1

Kernel

Threads

Process

One process with three threads

In this book it is always ok to confuse between processes and threads.

Page 55: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

55Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Processes and Threads A process is a program in execution

Shared by all threads in a process Address space, global variables, open files,

... Private to each thread

Program counter, registers, stack. User-level threads

Thread management is done by the application.

Kernel-level threads Threads are managed by the kernel (but

run in user space).

Page 56: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

56Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

NewNew RunningRunning terminatedterminatedReadyReady

BlockedBlocked

admit

event occurs

CPU scheduler picks another process

exit

waits for I/O or other event

CPU scheduler picks this process

Five states a process can be in

Page 57: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

57Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Processes and Scheduling

0 1 n -1 n

Scheduler

Non-preemptive the scheduler can not interrupt a running process.

Preemptive the scheduler can interrupt a running process.

Page 58: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

58Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Scheduling Algorithms

First-come-first-served schedules processes in the order they become

ready. Shortest-job-first

schedule the process with the shortest time. Priority scheduling

schedule the process with the highest priority. Round robin –

each process is scheduled for a small number of time units (milliseconds).

Many other possibilities exist.

First-come-first-served schedules processes in the order they become

ready. Shortest-job-first

schedule the process with the shortest time. Priority scheduling

schedule the process with the highest priority. Round robin –

each process is scheduled for a small number of time units (milliseconds).

Many other possibilities exist.

Page 59: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

59Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

SchedulingThe Priority Inversion Problem

Process P1 with priority 5 enters its CS, and is preempted before releasing it.

Process P2 with priority 10 is scheduled but can not enter its CS and busy-waits.

Process P1 wants to release its critical section (to enable P2 to enter) but never gets scheduled.

Process P1 with priority 5 enters its CS, and is preempted before releasing it.

Process P2 with priority 10 is scheduled but can not enter its CS and busy-waits.

Process P1 wants to release its critical section (to enable P2 to enter) but never gets scheduled.

In the sequel, we will assume that processes have the same priority.

Page 60: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

60Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Problems Models

read/write

Algorithms Upper and lower bounds Impossibility results Simulations & Implementations

Problems & Models

barrier test&set

swap

LL/SC

readers&writers

consensus

mutex

Page 61: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

61

The Mutual Exclusion Problem

The first problem we cover in detail A generalization of the too-much-milk problem

Chapter 1

Page 62: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

62Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

remainder coderemainder code

entry codeentry code

critical sectioncritical section

exit codeexit code

The problem is to design the entry and exit code in a way that guarantees that the mutual exclusion and deadlock-freedom properties are satisfied.

The mutual exclusion problem

Page 63: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

63Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

The mutual exclusion problem

Mutual Exclusion: No two processes are in their critical section at the same time.

Deadlock-freedom: If a process is trying to enter its critical section, then some process, not necessarily the same one, eventually enters its critical section.

Starvation-freedom: If a process is trying to enter its critical section, then this process must eventually enter its critical section.

Mutual Exclusion: No two processes are in their critical section at the same time.

Deadlock-freedom: If a process is trying to enter its critical section, then some process, not necessarily the same one, eventually enters its critical section.

Starvation-freedom: If a process is trying to enter its critical section, then this process must eventually enter its critical section.

entry code

exit code

criticalsection

remainder

Page 64: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

64Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Question: true or false ?

entry code of A

exit code of A

criticalsection

remainder

entry code of B

exit code of B

Algorithm C

entry code

exit code

criticalsection

remainder

Algorithm A Algorithm B

Page 65: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

65Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Question: true or false ?

A and B are deadlock-free C is deadlock-free.

A and B are starvation-free C is starvation-free.

A or B satisfies mutual exclusion C satisfies mutual exclusion.

A is deadlock-free and B is starvation-free C is starvation-free.

A is starvation-free and B is deadlock-free C is starvation-free.

A and B are deadlock-free C is deadlock-free.

A and B are starvation-free C is starvation-free.

A or B satisfies mutual exclusion C satisfies mutual exclusion.

A is deadlock-free and B is starvation-free C is starvation-free.

A is starvation-free and B is deadlock-free C is starvation-free.

entry code of A

exit code of A

criticalsection

remainder

entry code of B

exit code of B

Algorithm C

Page 66: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

66Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Properties & complexity Time complexity

Fast Adaptive

Fairness FIFO, ...

Fault-tolerance Local-spinning Space complexity Communication primitives

Time complexity Fast Adaptive

Fairness FIFO, ...

Fault-tolerance Local-spinning Space complexity Communication primitives

Page 67: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

67Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Complexity Measures

Counting steps process step complexity process time complexity contention-free time

complexity Counting time units

system response time process response time

Counting communications distributed shared memory coherent caching

Space complexity

Counting steps process step complexity process time complexity contention-free time

complexity Counting time units

system response time process response time

Counting communications distributed shared memory coherent caching

Space complexity

Page 68: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

68Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

History of the mutex problem

1965 Defined by Dijkstra First solution by Dekker General solution by Dijkstra

Fischer, Knuth, Lynch, Rabin, Rivest, ...

1974 Lamport’s bakery algorithm 1981 Peterson’s solution There are hundreds of published

solutions Lots of related problems

E. W. Dijkstrawww.cs.utexas.edu/users/EWD/(Photo by Hamilton Richards 2002)

Page 69: 1 Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization

69Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 1

Next Chapter

In Chapter 2 we will start looking at basic solutions to the mutual exclusion problem using atomic registers.

-- Gadi