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

ch 10 avoiding liveness hazards
SMART_READER_LITE
LIVE PREVIEW

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.


slide-1
SLIDE 1
  • Ch. 10 Avoiding Liveness Hazards

JESPER PEDERSEN NOTANDER

slide-2
SLIDE 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.
slide-3
SLIDE 3

Deadlock

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

Dynamic Lock Ordering Deadlock

transaction(A, B) { synchronized( ){ synchronized( ){ // do the transaction }}}

  • A

B

  • Order ¡unknown, ¡defined ¡by ¡caller ¡
slide-7
SLIDE 7

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

slide-8
SLIDE 8

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 }

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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 }

slide-11
SLIDE 11

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

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

slide-13
SLIDE 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

slide-14
SLIDE 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

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

slide-16
SLIDE 16

Thank You