Docsity

Deadlock Prevention and Avoidance in Operating Systems, Slides of Operating Systems

Partial preview of the text

Download Deadlock Prevention and Avoidance in Operating Systems and more Slides Operating Systems in PDF only on Docsity!

Deadlock (2)

Dave Eckhardt

Todd Mowry

Synchronization

l Today's goals (to be on track with P2)

  • Hopefully you had a fruitful weekend debugging:
    • cyclone and agility_drill
    • maybe paraguay also

Deadlock – Alternative Approaches

l Prevention

  • Pass a law against one of four ingredients l Note: static, absolute ban
  • Every legal application is continuously deadlock-free

l Avoidance

  • Processes pre-declare usage patterns l Note: more complicated for application, but more flexible
  • Request manager avoids “unsafe states”

l Detection/Recovery

  • Clean up only when trouble really happens

Deadlock Prevention – Satisfactory?

l Deadlock prevention passes laws

  • Unenforceable: shared CD-writers???
  • Annoying l Inefficient if extra resources must be held l Mandatory lock-acquisition order may induce starvation - Locked 23, 24, 25, ... 88, 89, now must lock 0... l Lots of starvation opportunities

l Do we really need such strict laws?

  • Couldn't we be more “situational”?

Deadlock Avoidance Assumptions

2. Processes proceed to completion

(a) Don't hold onto resources forever

l Obvious how this helps!

(b) Complete in “reasonable” time

l So it is ok, if necessary, to stall P2 until P1 completes l We will try to avoid this

Safe Execution Sequence

l (P

, P

2

, P

3

, ... P

n

) is a safe sequence if

  • Every process P i
can be satisfied using

l currently-free resources F, plus l resources currently held by P 1 , P 2 , ...P i

l Claim: P

i

's waiting is bounded by the sequence:

  • P 1
will run to completion, release resources
  • P 2
can complete with F + P

1

's + P

2

's
  • P 3
can complete with F + P

1

's + P

2

's + P

3

's
  • P i
won't wait forever, so no wait cycle, no deadlock □

Request Manager - Naïve

l Grant a resource request if

  • Enough resources are free now

l Otherwise, tell requesting process to wait

  • While holding resources l Which are non-preemptible, ...

l Easily leads to deadlock

Request Manager – Avoidance

l Grant a resource request if

  • Enough resources are free now, and
  • Enough resources would still be free l For some process to acquire the rest of its resources, complete, and release all held resources l And then another one l And then you

l Otherwise, tell requesting process to wait

  • While holding a smaller set of resources... l ...which we previously proved it's ok to hold, because other processes don't need them to complete

Example (from text)

System 12 3 - P2 9 2 7 P1 4 2 2 P0 10 5 5 Who Max Has Room How would we show that this state is safe?

Max=declared

Has=allocated

Room=Max - Has

P1: 2 ⇒ 4

System 12 3 - P2 9 2 7 P1 4 2 2 P0 10 5 5 Who Max Has Room System 12 1 - P2 9 2 7 P1 4 4 0 P0 10 5 5 Who Max Has Room

P0: 5 ⇒ 10

System 12 5 - P2 9 2 7 P0 10 5 5 Who Max Has Room System 12 0 - P2 9 2 7 P0 10 10 0 Who Max Has Room

P0: Complete

System 12 0 - P2 9 2 7 P0 10 10 0 Who Max Has Room System 12 10 - P2 9 2 7 Who Max Has Room “Run P1, P0, P2” is a safe sequence. So the system was in a safe state.

P2: 2 ⇒ 3?

System 12 3 - P2 9 2 7 P1 4 2 2 P0 10 5 5 Who Max Has Room System 12 2 - P2 9 3 6 P1 4 2 2 P0 10 5 5 Who Max Has Room

Now, only P1 can be satisfied without waiting.

P1: 2 ⇒ 4?

System 12 2 - P2 9 3 6 P1 4 2 2 P0 10 5 5 Who Max Has Room System 12 0 - P2 9 3 6 P1 4 4 0 P0 10 5 5 Who Max Has Room