Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made...

28
COMP 150-CCP COMP 150-CCP Concurrent Programming Concurrent Programming Lecture 12: Deadlock Dr. Richard S. Hall [email protected] Concurrent programming – February 28, 2008

Transcript of Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made...

Page 1: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

COMP 150-CCPCOMP 150-CCPConcurrent ProgrammingConcurrent Programming

Lecture 12:Deadlock

Dr. Richard S. Hall [email protected]

Concurrent programming – February 28, 2008

Page 2: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

ScenarioScenario

Process 1 gets the lock for object A and wants to lock object B

A

C

B

Process 2 gets the lock for object C and

wants the lock for object A

Process 3 gets the lock for object B and

wants the lock for object C

What happens next?

Page 3: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Deadlock ConceptDeadlock Concept

When all processes in a system are waiting to acquire a shared resource (i.e., all of the processes are blocked), then the system is deadlocked.

● When a system is deadlocked, it is not possible to execute any actions or make any progress

Each process is waiting for a resource to be released, but no process can make progress to release a held resource

● Deadlock is a serious issue in concurrent systems● The goal is to design systems that are free from

deadlock

Page 4: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Possibility of DeadlockPossibility of Deadlock

● Serially reusable resources The processes involved share resources which they

use under mutual exclusion● Incremental acquisition

Processes hold on to resources already allocated to them while waiting to acquire additional resources

● No preemption Once acquired by a process, resources cannot be

preempted (forcibly withdrawn) but are only released voluntarily

Page 5: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Existence of DeadlockExistence of Deadlock

● Serially reusable resources● Incremental acquisition● No preemption● Wait-for cycle

A circular chain (or cycle) of processes exists such that each process holds a resource which itssuccessor in the cycle is waiting to acquire

● Referred to as the “four necessary and sufficient conditions for deadlock”

Page 6: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Dealing With DeadlockDealing With Deadlock

● Ignore the problem altogether● Prevention● Avoidance● Detection and recovery

Page 7: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Deadlock Prevention (1/2)Deadlock Prevention (1/2)

● Eliminate one of the four necessary conditions

● Serially reusable resources Not possible to eliminate this, since access to some

resources requires mutual exclusion

● Incremental acquisition Require that all resources are requested at one time

▴ Process may block for a long time waiting for all resources▴ Resources may not be used for a long time▴ Not always possible to know all resources in advance

Page 8: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Deadlock Prevention (2/2)Deadlock Prevention (2/2)

● No preemption Process must release resource and request again May preempt a process to require it releases its

resources

● Wait-for cycle Define a linear ordering of resource types

Page 9: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Deadlock AvoidanceDeadlock Avoidance

● A decision is made dynamically whether the current resource allocation request will, if granted, potentially lead to a deadlock

● Requires knowledge of future thread resource requests

Do not grant an incremental resource request to a thread if this allocation might lead to deadlock

▴ e.g., Banker's algorithm▴ Only helps us when allocating with resources, will not help

when deadlock occurs due to trying to acquire synchronized locks

Page 10: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Deadlock Detection & RecoveryDeadlock Detection & Recovery

● Let deadlock occur● Try to detect when a deadlock occurs● When a deadlock is detected, take some action to

recover from it, e.g., Abort/stop all deadlocked threads

▴ Cannot really do this in Java▴ Even if you could, you would likely leave the system in an

inconsistent state unless you had some sort of rollback mechanism

Try interrupting threads▴ Threads would have to understand that an interrupt meant to

“let go”▴ Would not help if they were blocked trying to acquire a

synchronized lock

Page 11: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Primitive Deadlock AnalysisPrimitive Deadlock Analysis

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

In an LTS graph deadlock is easily visible as a state with no outgoing arcs.

In FSP we can achieve deadlock using the STOP process:

Using the LTSA, we can find the deadlock through safety analysis:Trace to DEADLOCK:

northnorth

Page 12: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Parallel Process Deadlock AnalysisParallel Process Deadlock Analysis

In real systems, deadlock may arise from the parallel composition of interacting processes.

printer:RESOURCE

getput

SYS

scanner:RESOURCEgetput

p:P

printer

scanner

q:Q

printer

scanner

RESOURCE = (lock->unlock->RESOURCE).P = (printer.lock->scanner.lock ->copy->scanner.unlock ->printer.unlock->P).Q = (scanner.lock->printer.lock ->copy->printer.unlock ->scanner.unlock->Q).||SYS = (p:P||q:Q ||{p,q}::printer:RESOURCE ||{p,q}::scanner:RESOURCE ).

Deadlock trace?

Avoidance?

Page 13: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Deadlock PreventionDeadlock Prevention

One potential technique for preventing deadlock:

If processes share different classes of resources, such as printers and scanners, a general purpose deadlock preventing strategy is to order the resource classes so every process acquires them in the same order.

RESOURCE = (lock->unlock->RESOURCE).P = (scanner.lock->printer.lock ->copy->printer.unlock ->scanner.unlock->P).Q = (scanner.lock->printer.lock ->copy->printer.unlock ->scanner.unlock->Q).||SYS = (p:P||q:Q ||{p,q}::printer:RESOURCE ||{p,q}::scanner:RESOURCE).

Page 14: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Deadlock PreventionDeadlock Prevention

Another potential technique for preventing deadlock:

It is also possible to use time-out values to prevent deadlock.

P = (printer.lock->GETSCANNER),GETSCANNER = (scanner.lock->copy->scanner.unlock ->printer.unlock->P |timeout->printer.unlock->P ).Q = (scanner.lock-> GETPRINTER),GETPRINTER = (printer.lock->copy->printer.unlock ->scanner.unlock->Q |timeout->scanner.unlock->Q ).

Deadlock? Progress?

Page 15: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Deadlock in JavaDeadlock in Java

Similar example in Java but without using locks:

// Global spaceSemaphore scanner = new Semaphore(1);Semaphore printer = new Semaphore(1);

// Thread 1public void run() { printer.down();

scanner.down();

// do work here… scanner.up(); printer.up();}

// Thread 2public void run() { scanner.down();

printer.down();

// do work here… printer.up(); scanner.up();}

Get printerGet scanner

Try scannerTry printer

Page 16: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Deadlock in JavaDeadlock in Java

● Why does deadlock happen in this example? Because of how semaphores are implemented

▴ The down() method of each semaphore will wait() until the semaphore value is non-zero

▴ Since the two threads acquire the semaphores in the opposite order, it is possible to interleave their instructions such that one thread gets the scanner and one gets the printer and then they both must wait for each other to finish…which will never happen

This scenario can be resolved just like the model, use resource acquisition ordering

▴ It might also be possible to interrupt the threads here, since wait() is interruptible

Page 17: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Dining Philosophers ExampleDining Philosophers Example

0

1

23

40

1

2

3

4

Five philosophers sit around a circular table. Each philosopher spends his life alternately thinking and eating. In the center of the table is a large bowl of spaghetti. A philosopher needs two forks to eat a helping of spaghetti.

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

Page 18: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Dining Philosophers Structure DiagramDining Philosophers Structure Diagram

Each FORK is a shared resource with get and put actions

When hungry, each PHIL must first get his right fork and then his left fork before he can start eating.

Page 19: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Dining Philosopher ModelDining Philosopher Model

FORK = (acquire->release->FORK).PHIL = (hungry->right.acquire->left.acquire

->eat->right.release->left.release->think->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?

Page 20: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Dining Philosopher AnalysisDining Philosopher Analysis

Trace to DEADLOCK:phil.0.hungryphil.0.right.acquirephil.1.hungryphil.1.right.acquirephil.2.hungryphil.2.right.acquirephil.3.hungryphil.3.right.acquirephil.4.hungryphil.4.right.acquire

This is the situation where all the philosophers become hungry at the same time, sit down at the table and each philosopher picks up the fork to his right.

The system can make no further progress since each philosopher is waiting for a fork held by his neighbor, i.e., a wait-for cycle exists!

Page 21: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Dining Philosophers ImplementationDining Philosophers Implementation

Deadlock is easily detected in our model.

How easy is it to detect a potential deadlock in an implementation?

\/ \ \/

Page 22: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Dining Philosophers ImplementationDining Philosophers Implementation

● Philosophers Active entities, implement as threads

● Forks Shared passive entities, implement as monitors

Page 23: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Dining Philosophers ImplementationDining Philosophers Implementation

public class Fork { private boolean taken = false; private int identity;

public Fork(int id) { identity = id; }

public synchronized void acquire() throws InterruptedException { while (taken) { wait(); } taken = true; }

public synchronized void release() { taken = false; notifyAll(); }}

taken encodes the state of the fork

Page 24: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Dining Philosophers ImplementationDining Philosophers Implementation

class Philosopher extends Thread { ... public void run() { try { while (true) { // No forks. setState(THINKING); Thread.sleep(50 * identity); // Get right fork. right.acquire(); setState(GOTRIGHT); Thread.sleep(50 * identity); // Get left fork. left.acquire(); setState(GOTBOTH); Thread.sleep(50 * identity); // Release forks. right.release(); left.release(); } } catch (InterruptedException ex) { } }}

Page 25: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Dining Philosophers ImplementationDining Philosophers Implementation

forks = new Fork[N];phils = new Philosopher[N];for (int i = 0; i < N; i++) { forks[i] = new Fork(i);}for (int i = 0; i < N; i++) { phils[i] = new Philosopher( this, i, forks[(i - 1 + N) % N], forks[i]); new Thread(phils[i]).start();}

Code to create the philosopher threads and forkmonitors in Main:

Page 26: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Deadlock in ImplementationDeadlock in Implementation

Deadlock is possible in the implementation, but it might not be as easy to spot. The program can run for a long time without deadlocking.

\ \ \ \ \

Page 27: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Fixing the ModelFixing the Model

PHIL(I=0) = (when (I%2==0) hungry ->left.acquire->right.acquire ->eat ->left.release->right.release ->think->PHIL |when (I%2==1) hungry ->right.acquire->left.acquire ->eat ->left.release->right.release ->think->PHIL ).

We can fix the implementation by eliminating the wait-for cycle...How?

Introduce an asymmetry into our definition of philosophers.

Use the identity of each philosopher to make even numbered philosophers get their left forks first, odd their right first.

Other strategies?

Page 28: Lecture 12: Deadlock - Department of Computer Science · Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially

Deadlock in Real SystemsDeadlock in Real Systems

● Important deadlock prevention heuristics from this lecture

Ordered acquisition of different resources types Asymmetric acquisition of same resource types

● Both of these heuristics apply to real world systems, but following them alone will not always lead to deadlock free systems

Sometimes wait-for cycles result from unexpected dependencies and circumstances of the system and environment's implementation

▴ Requires testing and diligence