csci 5828: foundations of software engineering · csci 5828: foundations of software engineering...

22
1 ©Magee/Kramer 2 nd Edition CSCI 5828: Foundations of Software Engineering Lecture 18: Deadlock Slides created by Magee and Kramer for the Concurrency textbook

Upload: hathien

Post on 27-May-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

1©Magee/Kramer 2nd Edition

CSCI 5828: Foundationsof Software Engineering

Lecture 18: Deadlock

Slides created by Mageeand Kramer for the

Concurrency textbook

Page 2: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 2©Magee/Kramer 2nd Edition

Chapter 6

Deadlock

Page 3: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 3©Magee/Kramer 2nd Edition

Deadlock

Concepts: system deadlock: no further progressfour necessary & sufficient conditions

Models: deadlock - no eligible actions

Practice: blocked threads

Aim: deadlock avoidance - to designsystems where deadlock cannot occur.

Page 4: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 4©Magee/Kramer 2nd Edition

Deadlock: four necessary and sufficient conditions

♦ Serially reusable resources:the processes involved shared resources which they use under mutualexclusion.

♦ Incremental acquisition:processes hold on to resources already allocated to them while waitingto acquire additional resources.

♦ No pre-emption:once acquired by a process, resources cannot be pre-empted (forciblywithdrawn) but are only released voluntarily.

♦ Wait-for cycle:a circular chain (or cycle) of processes exists such that each processholds a resource which its successor in the cycle is waiting to acquire.

Page 5: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 5©Magee/Kramer 2nd Edition

Wait-for cycle

A

B

CD

E

Has A awaits B

Has B awaits C

Has C awaits DHas D awaits E

Has E awaits A

Page 6: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 6©Magee/Kramer 2nd Edition

6.1 Deadlock analysis - primitive processes

♦ deadlocked state is one with no outgoing transitions

♦ in FSP: STOP process

MOVE = (north->(south->MOVE|north->STOP)).

Trace to DEADLOCK:northnorth

♦ animation to produce a trace.

♦analysis using LTSA:

(shortest trace to STOP)

MOVEnorth north

south

0 1 2

Page 7: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 7©Magee/Kramer 2nd Edition

deadlock analysis - parallel composition

♦ in systems, deadlock may arise from theparallel composition of interacting processes.

RESOURCE = (get->put->RESOURCE).P = (printer.get->scanner.get

->copy ->printer.put->scanner.put

->P).Q = (scanner.get->printer.get

->copy ->scanner.put->printer.put

->Q).||SYS = (p:P||q:Q

||{p,q}::printer:RESOURCE ||{p,q}::scanner:RESOURCE ).

printer:RESOURCEgetput

SYS

scanner:RESOURCEgetput

p:Pprinter

scanner

q:Qprinter

scanner

Deadlock Trace?

Avoidance?

Page 8: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 8©Magee/Kramer 2nd Edition

deadlock analysis - avoidance

♦ acquire resources in the same order?

♦ Timeout:P = (printer.get-> GETSCANNER),GETSCANNER = (scanner.get->copy->printer.put ->scanner.put->P |timeout -> printer.put->P ).Q = (scanner.get-> GETPRINTER),GETPRINTER = (printer.get->copy->printer.put ->scanner.put->Q |timeout -> scanner.put->Q ).

Deadlock? Progress?

Page 9: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 9©Magee/Kramer 2nd Edition

6.2 Dining Philosophers

Five philosophers sit around acircular table. Each philosopherspends his life alternatelythinking and eating. In the centreof the table is a large bowl ofspaghetti. A philosopher needstwo forks to eat a helping ofspaghetti.

0

1

23

40

1

2

3

4

One fork is placed between eachpair of philosophers and they agreethat each will only use the fork to hisimmediate right and left.

Page 10: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 10©Magee/Kramer 2nd Edition

Dining Philosophers - model structure diagram

phil[4]:PHIL

phil[1]:PHIL

phil[3]:PHIL

phil[0]:PHIL

phil[2]:PHIL

FORK FORK

FORK

FORK FORK

lef tright

right

right

right

lef t

lef t

right

lef t

lef t

Each FORK is ashared resourcewith actions getand put.

When hungry,each PHIL mustfirst get hisright and leftforks before hecan start eating.

Page 11: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 11©Magee/Kramer 2nd Edition

Dining Philosophers - model

FORK = (get -> put -> FORK).PHIL = (sitdown ->right.get->left.get

->eat ->right.put->left.put ->arise->PHIL).

||DINERS(N=5)= forall [i:0..N-1] (phil[i]:PHIL || {phil[i].left,phil[((i-1)+N)%N].right}::FORK

).

Table of philosophers:

Can this system deadlock?

Page 12: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 12©Magee/Kramer 2nd Edition

Dining Philosophers - model analysis

Trace to DEADLOCK:phil.0.sitdownphil.0.right.getphil.1.sitdownphil.1.right.getphil.2.sitdownphil.2.right.getphil.3.sitdownphil.3.right.getphil.4.sitdownphil.4.right.get

This is the situation whereall the philosophers becomehungry at the same time, sitdown at the table and eachphilosopher picks up thefork to his right.

The system can make nofurther progress since eachphilosopher is waiting for afork held by his neighbor i.e.a wait-for cycle exists!

Page 13: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 13©Magee/Kramer 2nd Edition

Dining Philosophers

Deadlock is easilydetected in ourmodel.

How easy is it todetect a potentialdeadlock in animplementation?

Page 14: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 14©Magee/Kramer 2nd Edition

Dining Philosophers - implementation in Java

♦philosophers:active entities- implement asthreads

♦forks: sharedpassive entities- implement asmonitors

♦display

Applet

Diners

Thread

Philosopher1 n

Fork

1

n

PhilCanvas

display

controller

view

display

Page 15: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 15©Magee/Kramer 2nd Edition

Dining Philosophers - Fork monitor

class Fork { private boolean taken=false; private PhilCanvas display; private int identity;

Fork(PhilCanvas disp, int id) { display = disp; identity = id;}

synchronized void put() { taken=false; display.setFork(identity,taken); notify(); }

synchronized void get() throws java.lang.InterruptedException { while (taken) wait(); taken=true; display.setFork(identity,taken); }}

takenencodes thestate of thefork

Page 16: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 16©Magee/Kramer 2nd Edition

Dining Philosophers - Philosopher implementationclass Philosopher extends Thread { ... public void run() { try { while (true) { // thinking view.setPhil(identity,view.THINKING); sleep(controller.sleepTime()); // hungry view.setPhil(identity,view.HUNGRY); right.get(); // gotright chopstick view.setPhil(identity,view.GOTRIGHT); sleep(500); left.get(); // eating view.setPhil(identity,view.EATING); sleep(controller.eatTime()); right.put(); left.put(); } } catch (java.lang.InterruptedException e){} }}

Followsfrom themodel(sittingdown andleaving thetable havebeenomitted).

Page 17: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 17©Magee/Kramer 2nd Edition

Dining Philosophers - implementation in Java

for (int i =0; i<N; ++i) fork[i] = new Fork(display,i);for (int i =0; i<N; ++i){ phil[i] = new Philosopher

(this,i,fork[(i-1+N)%N],fork[i]); phil[i].start();}

Code to create the philosopherthreads and fork monitors:

Page 18: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 18©Magee/Kramer 2nd Edition

Dining Philosophers

To ensure deadlockoccurs eventually,the slider controlmay be moved to theleft. This reducesthe time eachphilosopher spendsthinking and eating.This "speedup"increases theprobability ofdeadlock occurring.

Page 19: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 19©Magee/Kramer 2nd Edition

Deadlock-free Philosophers

Deadlock can be avoided by ensuring that a wait-for cyclecannot exist. How? PHIL(I=0)

= (when (I%2==0) sitdown ->left.get->right.get ->eat

->left.put->right.put->arise->PHIL

|when (I%2==1) sitdown ->right.get->left.get ->eat

->left.put->right.put->arise->PHIL

).

Introduce anasymmetry into ourdefinition ofphilosophers.Use the identity I ofa philosopher to makeeven numberedphilosophers gettheir left forks first,odd their right first.Other strategies?

Page 20: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 20©Magee/Kramer 2nd Edition

Maze example - shortest path to “deadlock”

0 1 2

3 4 5

6 7 8

STOP

north

south

west east

We can exploit the shortest path trace produced by thedeadlock detection mechanism of LTSA to find theshortest path out of a maze to the STOP process!

We first model theMAZE.

Each position ismodelled by themoves that itpermits. The MAZEparameter gives thestarting position.

eg. MAZE(Start=8) = P[Start],P[0] = (north->STOP|east->P[1]),...

Page 21: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 21©Magee/Kramer 2nd Edition

Maze example - shortest path to “deadlock”

||GETOUT = MAZE(7).

0 1 2

3 4 5

6 7 8

STOP

north

south

west east

Shortest pathescape trace fromposition 7 ?

Trace toDEADLOCK:

eastnorthnorthwestwestnorth

Page 22: CSCI 5828: Foundations of Software Engineering · CSCI 5828: Foundations of Software Engineering Lecture 18: ... Deadlock can be avoided by ensuring that a wait-for cycle ... 18-Deadlock.ppt

Concurrency: Deadlock 22©Magee/Kramer 2nd Edition

Summary

Concepts deadlock: no futher progress

four necessary and sufficient conditions: serially reusable resources

incremental acquisition

no preemption

wait-for cycle

Models no eligable actions (analysis gives shortest path trace)

Practice blocked threads

Aim: deadlock avoidance- to design systems wheredeadlock cannot occur.