ch 24 deadlocks

Upload: chirag-arora

Post on 04-Apr-2018

234 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Ch 24 Deadlocks

    1/31

    Ceng 334 - Operating Systems 2.4-1

    Chapter 2.4 : Deadlocks

    Process concept

    Process scheduling

    Interprocess communication

    Deadlocks

    Threads

  • 7/31/2019 Ch 24 Deadlocks

    2/31

    Ceng 334 - Operating Systems 2.4-2

    What is Deadlock?

    Process Deadlock

    A process is deadlocked when it is

    waiting on an event which will neverhappen

    System Deadlock

    A system is deadlocked when one ormore processes are deadlocked

  • 7/31/2019 Ch 24 Deadlocks

    3/31

    Ceng 334 - Operating Systems 2.4-3

    Necessary Conditions for a

    Deadlock

    Mutual Exclusion

    Shared resources are used in a

    mutually exclusive manner

    Hold & Wait

    Processes hold onto resources theyalready have while waiting for theallocation of other resources

  • 7/31/2019 Ch 24 Deadlocks

    4/31

    Ceng 334 - Operating Systems 2.4-4

    Necessary Conditions for a

    Deadlock (Cont.)

    No PreemptionResources can not be preempted

    until the process releases them

    Circular WaitA circular chain of processes exists

    in which each process holdsresources wanted by the next processin the chain

  • 7/31/2019 Ch 24 Deadlocks

    5/31

    Ceng 334 - Operating Systems 2.4-5

    No Deadlock Situation

    If you canprevent at least one

    of the necessary deadlockconditionsthen you wont

    have a DEADLOCK

  • 7/31/2019 Ch 24 Deadlocks

    6/31

    Ceng 334 - Operating Systems 2.4-6

    The Ostrich Algorithm

    Pretend there is no problem

    Reasonable if

    deadlocks occur very rarely

    cost of prevention is high

    UNIX and Windows takes this approach

    It is a trade off between

    convenience

    correctness

  • 7/31/2019 Ch 24 Deadlocks

    7/31

    Ceng 334 - Operating Systems 2.4-7

    Ways of Handling Deadlock

    Deadlock Prevention

    Deadlock Detection Deadlock Avoidance

    Deadlock Recovery

  • 7/31/2019 Ch 24 Deadlocks

    8/31

    Ceng 334 - Operating Systems 2.4-8

    Deadlock Prevention

    Remove the possibility of deadlock

    occurring by denying one of the four

    necessary conditions:

    Mutual Exclusion (Can we share everything?)

    Hold & Wait

    No preemptionCircular Wait

  • 7/31/2019 Ch 24 Deadlocks

    9/31

    Ceng 334 - Operating Systems 2.4-9

    Implementation

    A process is given its resources on a

    "ALL or NONE" basis

    Either a process gets ALL its required

    resources and proceeds or it getsNONE of them and waits until it can

    Denying the Hold & Wait

  • 7/31/2019 Ch 24 Deadlocks

    10/31

    Ceng 334 - Operating Systems 2.4-10

    Advantages

    It works

    Reasonably easy to code

    Problems

    Resource wastage

    Possibility of starvation

  • 7/31/2019 Ch 24 Deadlocks

    11/31

    Ceng 334 - Operating Systems 2.4-11

    Denying No preemption

    Implementation

    When a process is refused a resource

    request, it MUST release all other

    resources it holds

    Resources can be removed from aprocess before it is finished with

    them

  • 7/31/2019 Ch 24 Deadlocks

    12/31

    Ceng 334 - Operating Systems 2.4-12

    Advantages

    It worksPossibly better resource utilisation

    Problems

    The cost of removing a process'sresources

    The process is likely to lose work it

    has done. (How often does thisoccur?)

    Possibility of starvation

  • 7/31/2019 Ch 24 Deadlocks

    13/31

    Ceng 334 - Operating Systems 2.4-13

    Denying Circular Wait

    Implementation

    Resources are uniquely numbered

    Processes can only request resources

    in linear ascending order

    Thus preventing the circular waitfrom occurring

  • 7/31/2019 Ch 24 Deadlocks

    14/31

    Ceng 334 - Operating Systems 2.4-14

    Advantages

    It worksHas been implemented in some OSes

    Problems

    Resources must be requested in ascending

    order of resource number rather than asneeded

    Resource numbering must be maintainedby someone and must reflect every addition

    to the OS

    Difficult to sit down and write just writecode

  • 7/31/2019 Ch 24 Deadlocks

    15/31

    Ceng 334 - Operating Systems 2.4-15

    Deadlock Avoidance

    Allow the chance of deadlock occur

    But avoid it happening..

    Check whether the next state (change in

    system) may end up in a deadlock situation

  • 7/31/2019 Ch 24 Deadlocks

    16/31

    Ceng 334 - Operating Systems 2.4-16

    Bankers Problem

    Suppose total bank capital is 1000 MTL Current cash : 1000- (410+210) = 380 MTL

    Customer

    c1

    c2

    Max. Need

    800

    600

    Present Loan

    410

    210

    Claim

    390

    390

  • 7/31/2019 Ch 24 Deadlocks

    17/31

    Ceng 334 - Operating Systems 2.4-17

    Dijkstra's Banker's Algorithm

    Definitions

    Each process has a LOAN, CLAIM,

    MAXIMUM NEED

    LOAN: current number of resources held

    MAXIMUM NEED: total number resources

    needed to complete

    CLAIM: = (MAXIMUM - LOAN)

  • 7/31/2019 Ch 24 Deadlocks

    18/31

    Ceng 334 - Operating Systems 2.4-18

    Assumptions

    Establish a LOAN ceiling (MAXIMUM

    NEED) for each process

    MAXIMUM NEED < total number of resourcesavailable (ie., capital)

    Total loans for a process must be less than

    or equal to MAXIMUM NEED Loaned resources must be returned back in

    finite time

  • 7/31/2019 Ch 24 Deadlocks

    19/31

    Ceng 334 - Operating Systems 2.4-19

    Algorithm

    1. Search for a process with a claim that cansatisfied using the current number of

    remaining resources (ie., tentatively grant

    the claim)2. If such a process is found then assume that

    it will return the loaned resources.

    3. Update the number of remaining resources4. Repeat steps 1-3 for all processes and

    mark them

  • 7/31/2019 Ch 24 Deadlocks

    20/31

    Ceng 334 - Operating Systems 2.4-20

    DO NOT GRANT THE CLAIM if at least

    one process can not be marked.

    Implementation

    A resource request is only allowed ifit results in a SAFE state

    The system is always maintained in a

    SAFE state so eventually all requestswill be filled

  • 7/31/2019 Ch 24 Deadlocks

    21/31

    Ceng 334 - Operating Systems 2.4-21

    Advantages

    It worksAllows jobs to proceed when a prevention

    algorithm wouldn't

    ProblemsRequires there to be a fixed number of

    resources

    What happens if a resource goes down?

    Does not allow the process to change its

    Maximum need while processing

  • 7/31/2019 Ch 24 Deadlocks

    22/31

    Ceng 334 - Operating Systems 2.4-22

    Safe and Unsafe States (1)

    Demonstration that the state in (a) is safe

    (a) (b) (c) (d) (e)

  • 7/31/2019 Ch 24 Deadlocks

    23/31

    Ceng 334 - Operating Systems 2.4-23

    Safe and Unsafe States (2)

    Demonstration that the sate in (b) is not safe

    (a) (b) (c) (d)

  • 7/31/2019 Ch 24 Deadlocks

    24/31

  • 7/31/2019 Ch 24 Deadlocks

    25/31

    Ceng 334 - Operating Systems 2.4-25

    Banker's Algorithm for Multiple Resources

    Example of banker's algorithm with multiple resources

  • 7/31/2019 Ch 24 Deadlocks

    26/31

    Ceng 334 - Operating Systems 2.4-26

    Deadlock Detection

    Methods by which the occurrence of

    deadlock, the processes and resources

    involved are detected.

    Generally work by detecting a circular wait

    The cost of detection must be considered

    One method is resource allocation graphs

  • 7/31/2019 Ch 24 Deadlocks

    27/31

    Ceng 334 - Operating Systems 2.4-27

    Deadlock Recovery

    Recover from the deadlock by

    removing the offending processes

    The process being removed may lose

    work

  • 7/31/2019 Ch 24 Deadlocks

    28/31

  • 7/31/2019 Ch 24 Deadlocks

    29/31

    Ceng 334 - Operating Systems 2.4-29

    Problems

    Most systems do not support the removal

    and then restarting of a process.

    Some processes should NOT be removed.

    It is possible to have deadlock involving

    tens or even hundreds of processes

  • 7/31/2019 Ch 24 Deadlocks

    30/31

    Ceng 334 - Operating Systems 2.4-30

    Implementation

    Processes are simply killed off (lost forever)Usually some sort of priority order exists for

    killing

    Support for suspend/resume (rollback)

    Some systems come with checkpoint/restart

    featuresDevelopers indicate a series of checkpoints when

    designing a software application

    So a process only need be rolled back to the last

    checkpoint, rather than back to the beginning

  • 7/31/2019 Ch 24 Deadlocks

    31/31

    Ceng 334 - Operating Systems 2.4-31

    Question : What is the simplest and

    most used method to recover from adeadlock?