/
Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 - PowerPoint Presentation

hondasnoopy
hondasnoopy . @hondasnoopy
Follow
343 views
Uploaded On 2020-10-22

Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 - PPT Presentation

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld Chapter 7 Multiple resources The dinning philosophers problem Version June 2014 Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld 2014 ID: 815758

algorithms concurrent chapter gadi concurrent algorithms gadi chapter synchronization taubenfeld 2014 programming deadlock resources process algorithm resource wait robust

Share:

Link:

Embed:

Download Presentation from below link

Download The PPT/PDF document "Chapter 7 Synchronization Algorithms and..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Synchronization Algorithms and Concurrent Programming

Gadi Taubenfeld

Chapter 7 Multiple resourcesThe dinning philosophers problem

Version:

June 2014

Slide2

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

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

Synchronization Algorithms and Concurrent ProgrammingISBN: 0131972596, 1

st

edition

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

http://www.faculty.idc.ac.il/gadi/book.htm

Slide3

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

2.1

Deadlocks2.2 Deadlock Prevention2.3 Deadlock Avoidance2.4 The Dining Philosophers2.5 Hold and Wait Strategy2.5

Wait and Release Strategy2.6 Randomized algorithms Chapter 7 Multiple Resources

Slide4

Deadlocks

Section 7.1

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Slide5

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

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.

Deadlocks

Slide6

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Multiple resources

How to avoid deadlock?account A

account B

Transferring money between two bank accounts

P

0

P

1

down(A); down(B)

down(B); down(A)

semaphores

A

and

B

, initialized to 1

deadlock

Slide7

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Multiple resources

How to avoid deadlock?

Bridge crossing

On the bridge traffic only in one direction.

The resources are the two entrances.

Slide8

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

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)?

No

15

×

14+1 = 211

Slide9

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

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

Slide10

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Strategies for dealing with Deadlocks

Just ignore the problem altogetherUNIX and Windows take this approach.Detection and recovery

Allow the system to enter a deadlock state and then recover.AvoidanceBy 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.)

Slide11

Deadlock Prevention

Section 7.2

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Slide12

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Mutual exclusion condition

one process at a time can use the resource.Hold and wait conditiona process can request (and wait for) a resource while holding another resource.

No preemption conditionA resource can be released only voluntarily by the process holding it.Circular wait conditionmust be a cycle involving several processes, each waiting for a resource held by the next one.

Deadlock PreventionAttacking one of the following conditions for deadlock

Slide13

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Attacking the mutual exclusion condition

Some devices (such as printer) can be spooledonly the printer daemon uses printer resource, thus deadlock for printer eliminatedNot all devices can be spooled attack is

not useful in generalAttacking the no preemption condition

Many resources (such as printer) should not be preemptedcan not take the printer from a process that has not finished printing yet

attack is not useful in general

Slide14

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Attacking the Hold and Wait Condition

ProblemsMay not know all required resources in advance.Inefficient : ties up resources other processes could be using.Starvation is possible.

Processes may request all the resources they need in advance.

Slide15

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Two-Phase Locking

(Notice similarity to requesting all resources at once)Phase oneThe process tries to lock all the resources it currently needs, one at a timeif needed record is not avaliable, release and start overPhase two: when phase one succeeds,performing updatesreleasing locks

“livelock” is possible.

Slide16

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Time stamps:

Before a process starts locking a unique new timestamp is associated with that process.If a process has been assigned timestamp Ti and later a new process has assigned timestamp Tj then Ti <Tj.

We associate with each resource a timestamp value, which is the timestamp of the process that is currently holding that resource.The time-stamping ordering technique

Slide17

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

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.

The time-stamping ordering techniquePrevents deadlock and starvation.

Slide18

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

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

234

56

7

We will see other interesting usage of this observation

time

account A

account B

Solves transferring money between two bank accounts

Slide19

Deadlock Avoidance

Section 7.3

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Slide20

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Safe and Unsafe States

safe

unsafe

deadlock

time

All terminated!

Deadlock Avoidance

Slide21

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

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.

Slide22

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Maximum

Allocation

available : 2

P1

P2

P3

1

9

4

5

2

8

Example: Prove that the state below is safe

Slide23

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Maximum

Allocation

Proof

time

available : 8

P1

P2

P3

1

9

0

-

0

-

Maximum

Allocation

available : 0

P1

P2

P3

1

9

0

-

8

8

Maximum

Allocation

available : 6

P1

P2

P3

1

9

0

-

2

8

Maximum

Allocation

available : 1

P1

P2

P3

1

9

5

5

2

8

Maximum

Allocation

available : 2

P1

P2

P3

1

9

4

5

2

8

available : 0

P1

P2

P3

9

9

0

--

0

--

available : 9

P1

P2

P3

0

--

0

--

0

--

Slide24

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Example: safe and unsafe

Maximum

Allocation

available : 2

P1

P2

P3

1

9

4

5

2

8

If process

P1

requests

one

(out of the 2 avaliable), resources the Banker will

not

allocated it

.

available : 1

P1

P2

P3

2

9

4

5

2

8

unsafe

state

Slide25

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

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 <P1, P2, …, Pn> such that for each

Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j < i.

Slide26

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

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.The Banker’s Algorithm: commensts

Slide27

The Dinning Philosophers Problem

Section 7.4

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Slide28

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Dining Philosophers

Philosophersthinktake forkseatput forksEating needs 2 forksPick one fork at a time How to prevent deadlock

Slide29

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

(

means “waiting for this forks”)

An incorrect solutionL

L

L

L

L

L

Slide30

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

An inefficient solution

using mutual exclusion

Slide31

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Proving deadlock-freedom & starvation-freedom

1

2

3

4

5

6

R

R

R

R

R

L

Impose a total ordering of all forks, and require that

each philosopher requests resources in an increasing order.

Slide32

The Hold and Wait Strategy

Section 7.5

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Slide33

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

R

L

L

R

R

L

The LR Solution

Slide34

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

1

4

2

5

3

6

R

L

R

L

R

L

The LR Solution

Proving deadlock-freedom & starvation-freedom

Slide35

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Concurrency

How many can eat simultaneously?

Slide36

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

At most

half can eat simultaneously

Slide37

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Only one third can eat simultaneously

Any algorithm is at most

n

/3

-concurrent

Slide38

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

In LR only one forth can eat simultaneously

The LR algorithm is at most

n

/4

-concurrent

Slide39

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

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

R

free

R

L

L

LR

(

means “waiting for this forks”)

Slide40

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

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.

Slide41

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

L

L

R

L

R

L

The LLR Solution

Slide42

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

LLR

3

2

1

6

5

0

L

L

R

L

L

R

Proving deadlock-freedom & starvation-freedom

Slide43

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

In LLR one third can eat simultaneously

A tight bound

The LLR algorithm is

n

/3

-concurrent

Slide44

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Robustness

k-robust: if all except k consecutive

philosophers fail, then one will not

starve.

The LLR algorithm is not 4-robust.

The LLR algorithm is 5-robust iff n

0 mod 3

.

Slide45

The Hold and Release Strategy

Section 7.6

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Slide46

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

The

LR wait/release Algorithm

RL

LR

R

L

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.

Slide47

Randomized Algorithms

Section 7.7

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Slide48

Chapter 7

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

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.