ch 10 avoiding liveness hazards
play

Ch. 10 Avoiding Liveness Hazards J ESPER P EDERSEN N OTANDER - PowerPoint PPT Presentation

Ch. 10 Avoiding Liveness Hazards J ESPER P EDERSEN N OTANDER Liveness and Safety A liveness property, something good eventually happens. e.g. program termination. A safety property, something bad never happens. e.g.


  1. Ch. 10 Avoiding Liveness Hazards J ESPER P EDERSEN N OTANDER

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

  3. Deadlock

  4. Lock Ordering Deadlock Resource Deadlock Resource � Resource � A � B? � B ∞� A � B � lock � try � wait � Resource � Resource � B � A? � A ∞� B � A � lock � try � wait �

  5. Lock Ordering Deadlock doLeftRight(){ � doRightLeft() { � synchronized( ){ � synchronized( ){ � A � B � synchronized( ){ � synchronized( ){ � B � A � // do something � � // do something � }}} � }}} � � • Beware nested synchronized blocks. • Always same order à no deadlocks.

  6. Dynamic Lock Ordering Deadlock transaction(A, B) { � synchronized( ){ � A � synchronized( ){ � B � // do the transaction � }}} �� • Order ¡unknown, ¡defined ¡by ¡caller ¡

  7. Solution: Impose an Order transfer(X, Y) { � transaction(A, B) { � synchronized( ) { � X � if (#A > #B) { � synchronized( ) { � Y � transfer(A, B); � // perform a safe �� } else if (#A < #B) { � // transfer � transfer(B, A); � }}} � } else { � synchronized( ){ � T � transfer(A, B); � }} ��

  8. Deadlock and Cooperating Objects //class Human � //class Alien � synchronized left() { � synchronized left() { � // do something � manipulate(shared); � } � human.left(); // alien � � } � synchronized right() { � � manipulate(shared); � synchronized right() { � alien.right(); // alien � // do something � } � } �

  9. Deadlock and Cooperating Objects //class Human � //class Alien � synchronized left() { � synchronized left() { � // do something � manipulate(shared); � a ∞ � h � a? � h.right � alien.right � alien.right � } � human.left(); // alien � � } � h ∞ � a � h? � a.left � human.left � human.right � synchronized right() { � � manipulate(shared); � synchronized right() { � alien.right(); // alien � // do something � } � } �

  10. Solution: Open Calls //class Human � //class Alien � synchronized left() { � left() { � // do something � synchronized(this) { � } � manipulate(shared); � � } � right() { � human.left(); // open � synchronized(this) { � } � manipulate(shared); � � } � synchronized right() { � alien.right(); // open � // do something � } � } �

  11. Resource Deadlocks Resource Pools  read-Starvation Deadlock Thread ¡X ¡ Thread � Task X � Awaits � Pool A � Pool B � Spawns � result � Task Queue � Task Y � Thread ¡Y ¡ … �

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

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

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

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

  16. Thank You

Recommend


More recommend