L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la...

21
L8: Deadlocks Este texto se distribuye bajo los t´ erminos de la Creative Commons License esar S´ anchez Grado en Ingenier´ ıa Inform´ atica Grado en Matem´ aticas e Inform´ atica Universidad Polit´ ecnica de Madrid Fri, 27 Feb 2015 Under construction! Do not print

Transcript of L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la...

Page 1: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

1

L8: Deadlocks

Este texto se distribuye bajo los terminos de la Creative Commons License

Cesar Sanchez

Grado en Ingenierıa InformaticaGrado en Matematicas e Informatica

Universidad Politecnica de Madrid

Fri, 27 Feb 2015

Underco

nstruct

ion! Donot prin

t

Page 2: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

2

Mapa Conceptual

Concurrency = Simultaneous + Nondeterminism + Interaction

Interaction = Communication | Synchronization

Synchronization = Mutual Exclusion | ConditionalSynchronization

atomic race conditioninterleaving busy-waitmutual exclusion critical sectiondeadlock livelockliveness

Terminology:

Page 3: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

3

Deadlock

Deadlock:

a deadlock is a situation in which two or more competing actionsare each waiting for the other to finish, and thus neither everdoes.

Page 4: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

3

Deadlock

Deadlock:

a deadlock is a situation in which two or more competing actionsare each waiting for the other to finish, and thus neither everdoes.

“When two trains approach each other at a crossing, both shallcome to a full stop and neither shall start up again until the otherhas gone.”

Statute passed by the Kansas State Legislature,early in the 20th century.

Page 5: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

4

Livelock

Livelock:

A livelock is similar to a deadlock, except that the states of theprocesses involved in the livelock constantly change with regardto one another, none progressing.

Page 6: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

4

Livelock

Livelock:

A livelock is similar to a deadlock, except that the states of theprocesses involved in the livelock constantly change with regardto one another, none progressing.

Page 7: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

5

Conditions for Deadlock (necessary and sufficient)

1. Mutual Exclusion

The resource is only accessed by one thread at a time.

2. Hold and wait

The resource is kept until the job is finished.

3. No preemption

A resource is not stolen.

4. Circular wait

There is a circular chain of processes waiting for resources heldby the next process in the chain.

Page 8: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

6

Resource allocation graph

R1

R2

P

Q

Page 9: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

6

Resource allocation graph

R1

R2

P

Q

Page 10: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

6

Resource allocation graph

R1

R2

P

Q

Page 11: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

6

Resource allocation graph

R1

R2

P

Q

Page 12: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

6

Resource allocation graph

R1

R2

P

Q

Page 13: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

7

Resource allocation graph (Deadlock Example)

R1

R2

P

Q

Page 14: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

7

Resource allocation graph (Deadlock Example)

R1

R2

P

Q

Page 15: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

7

Resource allocation graph (Deadlock Example)

R1

R2

P

Q

Page 16: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

7

Resource allocation graph (Deadlock Example)

R1

R2

P

Q

Page 17: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

7

Resource allocation graph (Deadlock Example)

R1

R2

P

Q

Page 18: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

7

Resource allocation graph (Deadlock Example)

R1

R2

P

Q

Page 19: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

7

Resource allocation graph (Deadlock Example)

R1

R2

P

Q

Page 20: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

8

Deadlock.java

public void run () {

for (int n = 1; n <= Dining.ROUNDS; ++ n) {

//think();

left.take();

System.out.println(me+" takes fork "+left.me);

right.take();

System.out.println(me+" takes fork "+right.me);

eat();

right.put_back();

left.put_back();

}

System.out.println(me+" leaves");

}

Page 21: L8: Deadlocks - Academia Cartagena99L8: Deadlocks Este texto se distribuye bajo los t erminos de la Creative Commons License C esar S anchez Grado en Ingenier a Inform atica Grado

9

Starvation

Starvation:

Starvation describes a situation where a thread is unable to gainregular access to shared resources and is unable to make progress.This happens when shared resources are made unavailable for longperiods by “greedy” threads.