Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress...

21
Deadlock INF2140 Modeling and programming parallel systems Lecture 7 March 06, 2013 INF2140 Modeling and programming parallel systems Lecture 7 Deadlock

Transcript of Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress...

Page 1: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Deadlock

INF2140 Modeling and programming parallel systemsLecture 7

March 06, 2013

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 2: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Deadlock

Concepts

System deadlock: no further progressFour necessary & sufficient conditions

ModelsDeadlock - no eligible actions

PracticeBlocked threads

Aim: deadlock avoidance - to designsystems where deadlock cannot occur

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 3: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Deadlock: Four Necessary and Sufficient Conditions

Serially reusable resources:the processes involved share resources which they use undermutual exclusionIncremental acquisition:processes hold on to resources already allocated to them whilewaiting to acquire additional resourcesNo pre-emption:once acquired by a process, resources cannot be pre-empted(forcibly withdrawn) but are only released voluntarilyWait-for cycle:a circular chain (or cycle) of processes exists such that eachprocess holds a resource which its successor in the cycle iswaiting to acquire

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 4: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Wait-for Cycle

Resources:A,B,C ,D,E

A

B

C D

E

Has A awaits B

Has B awaits C

Has C awaits D Has D awaits E

Has E awaits A

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 5: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Deadlock Analysis: Primitive Processes

A deadlocked state is one with no outgoing transitionsSTOP process in FSP

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

Animation to producea trace.

Analysis using LTSA:Shortest trace to STOP

Trace to DEADLOCK:northnorth

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 6: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Deadlock Analysis: Parallel Composition

In composed systems, deadlock may arise from theparallel composition of interacting processes.

printer: RESOURCE get put

SYS

scanner: RESOURCE get put

p:P printer

scanner

q:Q printer

scanner

Deadlock trace?How to avoid deadlock?

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

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 7: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Deadlock Analysis - Avoidance

1 Acquire resources in the same order?2 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?

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 8: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Dining Philosophers

(Dijkstra, 1965)

Five philosophers sit around acircular table.Each philosopher spends his lifealternately thinking and eating.In the centre of the table is alarge bowl of spaghetti.A philosopher needs two forksto eat a helping of spaghetti. 0

1

2 3

4 0

1

2

3

4

One fork is placed between each pair of philosophers and they agreethat each will only use the fork to his immediate right and left.

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 9: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Dining Philosophers - Model Structure Diagram

Each FORK is a sharedresource with actionsget and put.

When hungry, eachPHIL must first gethis right and left forksbefore he can starteating.

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 10: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Dining Philosophers - Model

FORK = (get -> put -> FORK).

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

Table of philosophers:

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

Can this system deadlock?

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 11: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

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 where all thephilosophers become hungry at thesame time, sit down at the tableand each philosopher picks up thefork to his right.

The system can make no furtherprogress since each philosopher iswaiting for a fork held by hisneighbor, i.e., a wait-for cycle exists!

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 12: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Dining Philosophers

Deadlock is easily detectedin our model using LTSA

How easy is it to detect apotential deadlock in animplementation?

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 13: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Dining Philosophers - Implementation in Java

Philosophers:active entities—implementas threadsForks: sharedpassive entities—implementas monitorsDisplay

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 14: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Dining Philosophers - Fork monitor

taken:encodes thestate of thefork

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); }

}

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 15: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Dining Philosophers - Philosopher implementation

class Philosopher extends Thread {... // Follows from the model (we omit sitting downpublic void run() { // and leaving the table)

try {while (true) { // thinking

view.setPhil(identity ,view.THINKING );sleep(controller.sleepTime ()); // hungryview.setPhil(identity ,view.HUNGRY );right.get (); // get right chopstickview.setPhil(identity ,view.GOTRIGHT );sleep (500);left.get(); // eatingview.setPhil(identity ,view.EATING );sleep(controller.eatTime ());right.put (); // return right chopstickleft.put(); // return left chopstick

}} catch (java.lang.InterruptedException e){}}}

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 16: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Dining Philosophers - implementation in Java

Code to create the philosopherthreads and fork monitors:

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 ();

}

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 17: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Dining Philosophers

To ensure deadlockoccurs eventually, theslider control may bemoved to the left. Thisreduces the time eachphilosopher spendsthinking and eating.

This “speedup” increasesthe probability ofdeadlock occurring.

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 18: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Deadlock-free Philosophers

Deadlock can be avoided by ensuring that await-for cycle cannot exist. How?

Introduce an asymmetry into ourdefinition of philosophers.

Use the identity i of aphilosopher such that

even numbered philosophersget their left forks firstodd numbered philosophersget their right forks first

Other strategies?

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

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 19: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Maze Example - Shortest Path to “Deadlock”

We can exploit the shortest path trace produced by the deadlockdetection mechanism of LTSA to find the shortest path out of amaze to the STOP process!

We first model the MAZE.

Each position is modelled bythe moves that it permits.The MAZE parameter gives thestarting position.

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

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 20: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Maze Example - Shortest Path to “Deadlock”

||GETOUT = MAZE(7). Shortest path escape tracefrom position 7 ?

Trace to DEADLOCK:eastnorthnorthwestwestnorth

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock

Page 21: Deadlock - Universitetet i oslo€¦ · Deadlock Concepts Systemdeadlock: nofurtherprogress Fournecessary&sufficientconditions Models Deadlock-noeligibleactions Practice Blockedthreads

Summary

ConceptsDeadlock: no further progressFour necessary and sufficient conditions:

Serially reusable resourcesIncremental acquisitionNo preemptionWait-for cycle

ModelsNo eligible actions (analysis gives shortest path trace)

PracticeBlocked threads

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

INF2140 Modeling and programming parallel systems Lecture 7

Deadlock