Deadlocks

Operating sytems manages many shared resources (e.g., file, memory, CPUs, status tables, disk drives, messages). Often have many processes competing for limited resources.

Preemptable Resource?????

Model of process action:

     request( resource )
     use( resource, time )
     release( resource )
This environment can lead to deadlock when processes need several resources.

Necessary conditions for deadlock (Coffman Conditions):

  1. mutual exclusion: If I've got it, you can't have it.
  2. hold & wait: Once I get it, I can hold it even if I want something else.
  3. no preemption: I only give it up when I'm done with it.
  4. ciruclar wait: There exists a set { P(0), P(1), ... , P(n-1) } of waiting processes such that P(i) is waiting for a resource held of P(i+1 mod n).

What to do???????

Ostrich Algorithm (Do Nothing)

Deadlock Detection

Deadlocks are allowed to occur, detected through model checking (FSM), and fixed.

Deadlock Prevention

Prevention depends on ensuring at least one of the four necessary conditions cannot occur:

mutual exclusionnot feasible for many resources.
OK for disks, usually not for files.
hold & waitcan preallocate everyting which a process MIGHT require.
advantage: easy (mostly)
disadvaantage: expensive. Will tie up possibly scarce resources for long periods of time, idle.

Can also require require process to release everything anytime it requests something new. Not always feasible.

no preemptionif a process requests and is blocked, then resources it holds are preempted when required by another process.

Works for processes whose state is easily saved and restored. Does not work well for files.

circular waitimpose total ordering on resource types, then require processes to only request resources in ascending order.
disadvantage: useful ordering often not feasible.

Note: If OS uses deadlock prevention (as opposed to avoidance, discussed next) not additional runtime overhead, though process may be needlessly delayed.

Deadlock Avoidance

Use additinal info about active processes to decide, for example, an ordered list of resource use for each process.

        Example:  proc 1 will use file 1 then file 2 then release both
                  proc 2 will use file 2 then file 1 then rel. both
Then OS considers each reqest and blocks or allocates based on keeping system in a SAFE state. I.e., given each process useage, a FEASIBLE SEQUENCE exists: some process can complete no matter what, release its resources, which will allow at least one more process to complete no matter what, and so on until all processes have completed).

Banker's Algorithm

Based on knowing the max number of each resource which a process may require.

Algorithm is simple: before granting any new resource request, make sure:

  1. have enough to grant request, and
  2. a feasible sequence (as defined above) exists.

Example:

Here, Alloc is what resources (A, B, C, or D) a process currently holds, Max is its state maximum for each resource type, and Avail is the number of each resource type now available (unassigned).
                     Alloc      Max       Avail
                     ABCD       ABCD      ABCD
              p0     0012       0012      1520
              p1     1000       1750
              p2     1354       2356
              p3     0632       0654
              p4     0014       0656
Can all process complete given current resources, max, alloc and avail? I.e. does a feasible sequence exist?

p0 can complete (since Max - Alloc >= Avail), Avail becomes 1532, then p2 can complete, then p3 then p4. No matter what!

Problem with banker's alg. O(mn2) (m resources, n processes). This leads to consideration of deadlock detection algorithms.

Deadlock Recovery

Once deadlock is detected, what next:

Is starvation possible? if you always use the same algorithm to select a victim, will you always tend to select the same victim?

Livelock?

Communications Deadlock


Index Previous Next

Copyright ©2015, G. Hill Price
Send comments to G. Hill Price