chapter7

Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld

Chapter 7

Multiple resources

The dinning philosophers problem

Chapter 7

Version: June 2014

Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

1

Synchronization Algorithms and Concurrent Programming

ISBN: 0131972596, 1 st 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:

 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 2014

Gadi Taubenfeld, All Rights Reserved

Chapter 7

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

Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

2

Chapter 7

Multiple Resources

2.1

Deadlocks

2.2

Deadlock Prevention

2.3

Deadlock Avoidance

2.4

The Dining Philosophers

2.5

Hold and Wait Strategy

2.5

Wait and Release Strategy

2.6

Randomized algorithms

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

3

Deadlocks

Section 7.1

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

4

Deadlocks

A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause.

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

5

Multiple resources

How to avoid deadlock?

account A account B

Transferring money between two bank accounts deadlock semaphores A and B, initialized to 1

P

0 down(A); down(B);

P

1 down(B) down(A)

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

6

Multiple resources

How to avoid deadlock?

Bridge crossing

On the bridge traffic only in one direction.

The resources are the two entrances.

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

7

Chapter 7

Two Simple Questions

Question: A system has 2 processes and 3 identical resources. Each process needs a maximum of 2 resources. Is deadlock possible?

Question: Consider a system with X identical resources. The system has 15 processes each needing a maximum of 15 resources. What is the smallest value for X which makes the system deadlock-free (without the need to use a deadlock avoidance algorithm)?

15 × 14+1 = 211

No

Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

8

Chapter 7

Question

Question: Two processes, P1 and P2 each need to hold five records 1,2,3,4 and 5 in a database to complete. If

P1 asks for them in the order 1,2,3,4,5 and P2 asks them in the same order, deadlock is not possible.

However, if P2 asks for them in the order 5,4,3,2,1 then deadlock is possible. With five resources, there are 5! or 120 possible combinations each process can request the resources. Hence there are 5!

× 5! different algorithms. What is the exact number of algorithms

(out of 5!

× 5!) that is guaranteed to be deadlock free?

5!(4!

× 4!) = (5!

× 5!)/5

Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

9

Chapter 7

Strategies for dealing with Deadlocks

Just ignore the problem altogether

UNIX and Windows take this approach.

Detection and recovery

Allow the system to enter a deadlock state and then recover.

Avoidance

By careful resource allocation, ensure that the system will never enter a deadlock state.

Prevention

The programmer should write programs that never deadlock. This is achieved by negating one of the four necessary conditions for deadlock to occur

(mentioned in the next slide.)

Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

10

Deadlock Prevention

Section 7.2

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

11

Chapter 7

Deadlock Prevention

Attacking one of the following conditions for deadlock

Mutual exclusion condition

 one process at a time can use the resource.

Hold and wait condition

 a process can request (and wait for) a resource while holding another resource.

No preemption condition

A resource can be released only voluntarily by the process holding it.

Circular wait condition

 must be a cycle involving several processes, each waiting for a resource held by the next one.

Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

12

Attacking the mutual exclusion condition

Some devices (such as printer) can be spooled

 only the printer daemon uses printer resource, thus deadlock for printer eliminated

Not all devices can be

 spooled

 attack is not useful in general

Attacking the no preemption condition

Many resources (such as printer) should not be preempted

 can not take the printer from a process that has not finished printing yet

 attack is not useful in general

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

13

Attacking the Hold and Wait Condition

Processes may request all the resources they need in advance.

Problems

May not know all required resources in advance.

Inefficient : ties up resources other processes could be using.

Starvation is possible.

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

14

Two-Phase Locking

(Notice similarity to requesting all resources at once)

Phase one

The process tries to lock all the resources it

 currently needs, one at a time if needed record is not avaliable, release and start over

Phase two: when phase one succeeds,

 performing updates releasing locks

“livelock” is possible.

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

15

The time-stamping ordering technique

Time stamps:

Before a process starts locking a unique new timestamp is associated with that process.

If a process has been assigned timestamp T and later a new process has assigned timestamp T j then T i

<T j

.

We associate with each resource a timestamp value, which is the timestamp of the process that is currently holding that resource.

i

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

16

The time-stamping ordering technique

Phase one: the process tries to lock all the resources it currently needs, one at a time.

If a needed resource is not available and the timestamp value is smaller than that of the process,

 release all the resources,

 waits until the resource with the smaller timestamp is released,

 and starts over.

Otherwise, if the timestamp of the resource is not smaller,

 waits until the resource is released and locks it.

Phase two: when phase one succeeds,

 performing updates; releasing locks.

Chapter 7

Prevents deadlock and starvation.

Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

17

Attacking the Circular Wait Condition

Impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration.

1 2 3 time

4 5 6 7

Chapter 7 account A account B

Solves transferring money between two bank accounts

18

Deadlock Avoidance

Section 7.3

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

19

safe unsafe deadlock

Deadlock Avoidance

Safe and Unsafe States time

Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

20

Basic Facts

If a system is in safe state  no deadlock.

If a system is in unsafe state  deadlock now or in the future.

Deadlock Avoidance  ensure that a system will never enter an unsafe state.

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

21

Example: Prove that the state below is safe

P1 1 9

P2 4 5

P3 2 8 available : 2

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

22

Proof

P1 1 9

P2 4 5

P3 2 8 available : 2

P1 9 9

P2 0 --

P3 0 -available : 0

Chapter 7

P1 1 9

P2 5 5

P3 2 8 available : 1

P1 1 9

P2 0 -

P3 2 8 available : 6

P1 1 9

P2 0 -

P3 8 8 available : 0

P1 0 --

P2 0 --

P3 0 -available : 9 time

Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

P1 1 9

P2 0 -

P3 0 available : 8

23

Example: safe and unsafe

P1 1 9

P2 4 5

P3 2 8 available : 2

P1 2 9

P2 4 5

P3 2 8 available : 1

Chapter 7

If process P1 requests one (out of the 2 avaliable), resources the Banker will not allocated it .

unsafe state

Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

24

The Banker’s Algorithm

When there is a request for an available resource, the banker must decide if immediate allocation leaves the system in a safe state.

If the answer is positive, the resource is allocated, otherwise the request is temporarily denied.

A state is safe if there exists a sequence of all processes <P

1

, P

2

, …, P n such that for each P i resources that P i

>

, the can still request can be satisfied by currently available resources + resources held by all the P j

, with j < i.

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

25

The Banker’s Algorithm: commensts

Can handle Multiple instances.

Each process must a priori claim maximum use -- a disadvantage.

When a process requests a resource it may have to wait.

When a process gets all its resources it must return them in a finite amount of time.

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

26

The Dinning Philosophers Problem

Section 7.4

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

27

Dining Philosophers

Philosophers

 think

 take forks eat

 put forks

Eating needs 2 forks

Pick one fork at a time

How to prevent deadlock

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

28

Chapter 7

An incorrect solution

( means “waiting for this forks”)

L

L L

L

L

L

Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

29

An inefficient solution using mutual exclusion

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

30

Proving deadlock-freedom & starvation-freedom

Impose a total ordering of all forks, and require that each philosopher requests resources in an increasing order.

1 2

R

Chapter 7

L R

6

R

R

R

5 4

Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

3

31

The Hold and Wait Strategy

Section 7.5

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

32

Chapter 7

The LR Solution

L

R R

L

L

R

Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

33

Chapter 7

The LR Solution

Proving deadlock-freedom & starvation-freedom

1 4

R

L L

6

R

R

L

3 5

Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

2

34

Concurrency

How many can eat simultaneously?

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

35

At most

half

can eat simultaneously

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

36

Only one third can eat simultaneously

Any algorithm is at most  n/3  -concurrent

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

37

In LR only one forth can eat simultaneously

The LR algorithm is at most  n/4  -concurrent

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

38

L

LR

If all want to eat, there is a case where only  n/4  will be able to eat simultaneously.

( means “waiting for this forks”)

R L

R free

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

39

Robustness

k-robust: if all except k consecutive philosophers fail, then one will not starve.

Any algorithm is at most

n/3  -robust.

The LR algorithm is not 4-robust.

The LR algorithm is 5-robust iff n is even .

There is no 4-robust algorithm using a hold and wait strategy.

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

40

The LLR Solution

Chapter 7

L

R L

L

R

L

Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

41

Chapter 7

LLR

Proving deadlock-freedom & starvation-freedom

3 2

L

R L

0 1

L

R

L

5 6

Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

42

In LLR one third can eat simultaneously

The LLR algorithm is  n/3  -concurrent

A tight bound

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

43

Robustness

k-robust: if all except k consecutive philosophers fail, then one will not starve.

The LLR algorithm is not 4robust.

The LLR algorithm is 5-robust iff n  0 mod 3 .

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

44

The Hold and Release Strategy

Section 7.6

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

45

The

LR

wait/release Algorithm

The LR wait/release algorithm is:

 deadlock-free but not starvation-free.

 n/3  -concurrent.

3-robust iff n is even .

Recall: There is no 4-robust algorithm using a hold and wait strategy.

R

L

L

R

L

R

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

46

Randomized Algorithms

Section 7.7

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

47

Two Randomized Algorithms

The Free Philosophers algorithm is:

 deadlock-free with probability 1, but is not starvation-free and is not 2-concurrent.

3-robust with probability 1 .

The Courteous Philosophers algorithm is:

 starvation-free with probability

1, but is not 2-concurrent and is not (n-1)-robust.

Chapter 7 Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld © 2014

48