ch. 10 avoiding liveness hazards - lunds tekniska...

16
Ch. 10 Avoiding Liveness Hazards JESPER PEDERSEN NOTANDER

Upload: others

Post on 07-Sep-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ch. 10 Avoiding Liveness Hazards - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDA015F/2013/Ch10-presentation.pdf · 2013. 11. 26. · Ch. 10 Avoiding Liveness Hazards JESPER

Ch. 10 Avoiding Liveness Hazards JESPER PEDERSEN NOTANDER

Page 2: Ch. 10 Avoiding Liveness Hazards - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDA015F/2013/Ch10-presentation.pdf · 2013. 11. 26. · Ch. 10 Avoiding Liveness Hazards JESPER

Liveness and Safety

•  A liveness property, –  something good eventually happens. –  e.g. program termination.

•  A safety property, –  something bad never happens. –  e.g. inconsistent shared states.

•  Tension between liveness and safety. •  Protection à liveness hazards.

Page 3: Ch. 10 Avoiding Liveness Hazards - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDA015F/2013/Ch10-presentation.pdf · 2013. 11. 26. · Ch. 10 Avoiding Liveness Hazards JESPER

Deadlock

Page 4: Ch. 10 Avoiding Liveness Hazards - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDA015F/2013/Ch10-presentation.pdf · 2013. 11. 26. · Ch. 10 Avoiding Liveness Hazards JESPER

Resource Deadlock Lock Ordering Deadlock

Resource!A!

B?!try!

B∞!wait !

A!lock!

A?!try!

B!lock!

Resource!B!

Resource!A!

Resource!B!

A∞!wait !

Page 5: Ch. 10 Avoiding Liveness Hazards - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDA015F/2013/Ch10-presentation.pdf · 2013. 11. 26. · Ch. 10 Avoiding Liveness Hazards JESPER

doLeftRight(){!

synchronized( ){!

synchronized( ){!

// do something!

}}}!

!

doRightLeft() {!

synchronized( ){!

synchronized( ){!

!// do something!

}}}!

Lock Ordering Deadlock

•  Beware nested synchronized blocks. •  Always same order à no deadlocks.

A!

B!

B!

A!

Page 6: Ch. 10 Avoiding Liveness Hazards - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDA015F/2013/Ch10-presentation.pdf · 2013. 11. 26. · Ch. 10 Avoiding Liveness Hazards JESPER

Dynamic Lock Ordering Deadlock

transaction(A, B) {!

synchronized( ){!

synchronized( ){!

// do the transaction!

}}} !!

A!

B!

•  Order  unknown,  defined  by  caller  

Page 7: Ch. 10 Avoiding Liveness Hazards - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDA015F/2013/Ch10-presentation.pdf · 2013. 11. 26. · Ch. 10 Avoiding Liveness Hazards JESPER

Solution: Impose an Order

transaction(A, B) {!

if (#A > #B) {!

transfer(A, B);!

} else if (#A < #B) {!

transfer(B, A);!

} else {!

synchronized( ){!

transfer(A, B);!

}} !!

transfer(X, Y) {!

synchronized( ) {!

synchronized( ) {!

// perform a safe!!

// transfer!

}}}!

X!

Y!

T!

Page 8: Ch. 10 Avoiding Liveness Hazards - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDA015F/2013/Ch10-presentation.pdf · 2013. 11. 26. · Ch. 10 Avoiding Liveness Hazards JESPER

Deadlock and Cooperating Objects

//class Human!

synchronized left() {!

// do something!

}!

!

synchronized right() {!

manipulate(shared);!

alien.right(); // alien!

}!

//class Alien!

synchronized left() {!

manipulate(shared);!

human.left(); // alien!

}!

!

synchronized right() {!

// do something!

}!

Page 9: Ch. 10 Avoiding Liveness Hazards - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDA015F/2013/Ch10-presentation.pdf · 2013. 11. 26. · Ch. 10 Avoiding Liveness Hazards JESPER

Deadlock and Cooperating Objects

//class Human!

synchronized left() {!

// do something!

}!

!

synchronized right() {!

manipulate(shared);!

alien.right(); // alien!

}!

//class Alien!

synchronized left() {!

manipulate(shared);!

human.left(); // alien!

}!

!

synchronized right() {!

// do something!

}!

h!h.right!

a!a.left!

a?!alien.right!

h?!human.left!

a∞!alien.right!

h∞!human.right!

Page 10: Ch. 10 Avoiding Liveness Hazards - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDA015F/2013/Ch10-presentation.pdf · 2013. 11. 26. · Ch. 10 Avoiding Liveness Hazards JESPER

Solution: Open Calls //class Human!

synchronized left() {!

// do something!

}!

!

right() {!

synchronized(this) {!

manipulate(shared);!

}!

alien.right(); // open!

}!

//class Alien!

left() {!

synchronized(this) {!

manipulate(shared);!

}!

human.left(); // open!

}!

!

synchronized right() {!

// do something!

}!

Page 11: Ch. 10 Avoiding Liveness Hazards - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDA015F/2013/Ch10-presentation.pdf · 2013. 11. 26. · Ch. 10 Avoiding Liveness Hazards JESPER

Resource Deadlocks

Pool A! Pool B!

Thread  X  

Thread  Y  

Task Queue!

Task X!

Task Y!

Spawns!Awaits!result!

Thread!

…!

read-Starvation Deadlock Resource Pools

Page 12: Ch. 10 Avoiding Liveness Hazards - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDA015F/2013/Ch10-presentation.pdf · 2013. 11. 26. · Ch. 10 Avoiding Liveness Hazards JESPER

Avoiding and Analyzing Deadlocks

•  Acquiring one lock at a time à no deadlocks. –  Unfeasible à lock ordering must be in the design. –  Or use explicit locks

•  java.util.concurrent.locks, tryLock(long timeout)

•  Deadlocks analysis using thread dumps. –  Triggered when sending SIGQUIT to the JVM. –  Deadlock identification, less support with Lock.

Page 13: Ch. 10 Avoiding Liveness Hazards - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDA015F/2013/Ch10-presentation.pdf · 2013. 11. 26. · Ch. 10 Avoiding Liveness Hazards JESPER

Other Liveness Hazards

•  Starvation –  Denial of access to resources, e.g. CPU time –  Thread priorities causes starvation

•  Poor responsiveness –  Not as severe as starvation –  Heavy processes competing for CPU time

Page 14: Ch. 10 Avoiding Liveness Hazards - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDA015F/2013/Ch10-presentation.pdf · 2013. 11. 26. · Ch. 10 Avoiding Liveness Hazards JESPER

Livelock

•  A thread that cannot progress, due to infinite retries of an action that always fail. –  Common source of failure, error-recovery code.

•  Or, multiple cooperating threads change state in a way that makes no further progress possible. –  Solution: Introduce some randomness

Page 15: Ch. 10 Avoiding Liveness Hazards - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDA015F/2013/Ch10-presentation.pdf · 2013. 11. 26. · Ch. 10 Avoiding Liveness Hazards JESPER

Summary

•  Synchronization give rise to liveness hazards. •  The most common hazard is lock ordering

deadlock. –  It must be handled already at design time. –  Open calls is effective at minimizing this hazard.

•  Other hazards mentioned are: resource deadlock, resource starvation, and livelock.

Page 16: Ch. 10 Avoiding Liveness Hazards - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDA015F/2013/Ch10-presentation.pdf · 2013. 11. 26. · Ch. 10 Avoiding Liveness Hazards JESPER

Thank You