Deadlock - PowerPoint PPT Presentation

About This Presentation
Title:

Deadlock

Description:

But concurrency also results in contention for resources and possibility of ... Circular wait: A closed chain of processes exists, such that each process holds ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 23
Provided by: bramam
Learn more at: https://cse.buffalo.edu
Category:

less

Transcript and Presenter's Notes

Title: Deadlock


1
Deadlock
  • B.Ramamurthy

CSE421
2
Introduction
  • Parallel operation among many devices driven by
    concurrent processes contribute significantly to
    high performance. But concurrency also results in
    contention for resources and possibility of
    deadlock among the vying processes.
  • Deadlock is a situation where a group of
    processes are permanently blocked waiting for the
    resources held by each other in the group.
  • Typical application where deadlock is a serious
    problem Operating system, data base accesses,
    and distributed processing.

3
Topics for discussion
  • Types of resources
  • Conditions for deadlock
  • Deadlock prevention
  • Deadlock detection
  • Deadlock avoidance
  • Dining philosophers problem

4
Types of resources
  • Two general categories of resources reusable and
    consumable.
  • Reusable resource One that can be safely used by
    one process at a time and not depleted by the
    use. Later it can be used by some other process.
    Examples processors, IOU channels, main and
    secondary memory, devices, files, databases,
    semaphores, locks.
  • Consumable resource One that can be produced and
    consumed (destroyed). Examples interrupts,
    signals, messages, information in IO buffers.

5
Conditions for a deadlock
  • Four preconditions that must be present for a
    deadlock to occur
  • Mutual exclusion Only one process can use a
    resource at a time.
  • Hold and wait A process must hold the resources
    while awaiting assignment of other resources.
  • No preemption No resource can be forcibly
    removed from a process holding it.
  • Circular wait A closed chain of processes
    exists, such that each process holds at least one
    resources needed by the next process in the chain.

6
General design policy
  • What is your policy?
  • Spend resources, effort and time during normal
    operation to prevent deadlock from occurring ?
    (pessimistic and conservative) or
  • Incur low overhead during normal deadlock-free
    operation without worrying about deadlock. Take
    care of resolving it when and if it happens?
    (optimistic). In this case, recovery from
    deadlock may be expensive.
  • Detect deadlock and recover.
  • Optimal solution? between cost of recovery and
    cost of prevention.

7
Deadlock prevention
  • To design a system in such a way that the
    possibility of a deadlock is excluded a priori.
  • Prevention philosophy We know what the
    preconditions are So prevent one or more these
    from occurring.
  • For example Circular wait can be prevented by
    linear ordering of the resource types. If a
    process holds resources of type Rj , then it can
    request resources of type Rk, k gt j, but not Ri
    where i lt j. Similarly, any other process
    holding Ri can request Rj but a process holding
    Rj cannot request Ri.

8
Deadlock detection
  • All the resource requests by processes are
    granted as long as they within the availability
    limits. Periodically the OS checks for circular
    wait and resolves it.
  • Policy1 When to check for circular wait? Every
    time a resource request is made? For every n-th
    request?
  • Policy2 How to break the deadlock once detected?
  • Question What is a policy ? and What is a
    mechanism? Ans WHAT to do and HOW to do
    describe policy and mechanism respectively.

9
How to break a deadlock?
  • Soln1 Abort all deadlocked processes and reclaim
    resources.
  • Soln2 Back up all the deadlock processes to a
    predefined checkpoint and restart.
  • Soln3 Gradually abort processes in deadlock one
    by one until deadlock is resolved.
  • Soln4 Successively preempt resources until
    deadlock is cleared.

10
Scope of various policies
  • Total deadlock prevention leads to inefficient
    use of resources and inefficient execution of
    processes.

DETECTION
Definite deadlock
UNSAFE
Deadlock possible but no deadlock
No Deadlock possible
SAFE
AVOIDANCE
PREVENTION
11
Deadlock avoidance
  • Deadlock avoidance requires some future knowledge
    of requests for resources from each of the
    participating processes.
  • Policies for avoidance (what to do?)
  • Do not start a process if its demands might lead
    to a deadlock.
  • Do not grant an incremental resource request to a
    process if this allocation might lead to a
    deadlock.

12
Deadlock avoidance mechanism
  • (How to do it?)
  • Data structures
  • Algorithms
  • Resource allocation algorithm
  • Testing for safety algorithm (Bankers algorithm)
  • Assume n processes, m different types of
    resources.

13
Deadlock avoidance - Data structures
  • Data structures
  • Resource vector R1....Rm Total number of
    resource in the system. Resource1 is R1, tells
    that there R1 units of resource type 1.
  • Available vector AV1....AVm Currently
    available. Available1 is AV1 tells that there
    are AV1 units of resource 1 available.
  • Claim matrix C1....Cn where each is vector
    of m elements Maximum requirement for each
    process. Claim is a nXm matrix defines the
    maximum of resources demanded by each process.
  • Allocation matrix A1...An current
    allocation for each process. It is an nXm matrix .

14
... avoidance - Resource allocation
  • If Request gt Available suspend process
  • else define newstate by
  • 1) Allocationi, Allocationi, Request
  • 2) Available Available - Request
  • If safe (newstate) then go ahead with allocation
  • else restore original state and suspend process.

15
Safety algorithm - bool
safe(state)
  • CurrentAvail Available
  • Rest set of all processes
  • Possible True
  • while (Possible) (Rest not Empty)
  • Find Pk in Rest such that Claimk, -
    Allocationk, lt CurrentAvail
  • if found,
  • CurrentAvail CurrentAvail
    Allocationk,
  • Rest Rest - Pk
  • else Possible False
  • / ASSERT either Rest is empty or Allocation
    failed /
  • safe (Rest is empty)

16
Example bool safe(state)
  • P1 P2 P3 P4 P1 P2 P3 P4
  • R1 3 6 3 4 1 6 2 0
  • R2 2 1 1 2 0 1 1 0
  • R3 2 3 4 2 0 2 1 2
  • CLAIM MATRIX ALLOCATION MATRIX
  • R1 2 0 1 4
  • R2 2 0 0 2
  • R3 2 1 3 0
  • NEED MATRIX CLAIM --- ALLOCATION

17
Example (contd.)
  • With the state of the system as given above and
    the current availability as given by AVAIL R1 R2
    R3 0 1 1, can you find a sequence of
    processor execution that will result in a safe
    state (no deadlock state)?
  • Some form of serializability. Quite common in
    data base applications.
  • Is the given state safe? YES. (means you have
    safe sequence)
  • What is the safe sequence? P2, P1, P3, P4

18
Example (contd.)
  • In other words, in the current state P2 can run
    with AVAIL, P1 can go after completion of P2 with
    the reclaimed resources from P2AVAIL, next P3
    can go with available resources from
    P2,P1AVAIL, and finally P4 with resources from
    P2,P1,P3AVAIL.
  • Next we will look at an example that illustrates
    complete allocation scheme avoids deadlock by
    making use of this safety algorithm discussed
    above.

19
Current state - AVAIL 1 1 2
  • P1 P2 P3 P4 P1 P2 P3 P4
  • R1 3 6 3 4 1 5 2 0
  • R2 2 1 1 2 0 1 1 0
  • R3 2 3 4 2 0 1 1 2
  • CLAIM MATRIX ALLOCATION MATRIX
  • Request of 1 0 1 by P2 can be safely allocated.
    Proof Above example.
  • But the same request from P1 at this state given
    above is unsafe. So cannot be allocated.

20
Dining philosopher problem
  • Classic problem is usually discussed with 5
    philosophers But you should really consider N
    philosophers.
  • 5 philosophers, 5 plates of noodles, 5 forks and
    circular dining table. Each one needs two forks.
    Examine how it satisfies the preconditions for
    deadlock!
  • Mutual exclusion shared fork.
  • Hold and wait One may hold the left fork and
    wait forever for the right fork.
  • No preemption One may not want to give up the
    fork he/she has.
  • Circular wait Of course, one waiting on the
    other around the table.

21
Monitor solution
  • See the enclosed solution for DP problem using
    monitors. Can you define a general base class DP
    that can used to solve any problem that can be
    modeled as DP? use Posix/Solaris condition
    variables and mutex for condition and suspension
    requirements of the monitor.

22
Summary
  • In general deadlock handling is left to the
    application level. But this situation may change
    with proliferation of database and multimedia
    applications. We need built-in mechanisms. We
    looked at some of them.
  • Prevention is conservative, detection and
    resolution is too optimistic, avoidance attempts
    to provide a satisfactory solution for some
    restricted classes of applications.
Write a Comment
User Comments (0)
About PowerShow.com