Concurrency: Deadlock and Starvation - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Concurrency: Deadlock and Starvation

Description:

Can prevent deadlock by forbidding one of the 4 conditions. Forbid Mutual Exclusion ... Forbid Hold and Wait. Ask for all resources at one time ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 20
Provided by: paulr7
Category:

less

Transcript and Presenter's Notes

Title: Concurrency: Deadlock and Starvation


1
Chapter 6
  • Concurrency Deadlock and Starvation

2
Deadlock
  • System Model
  • Process must request a resource before using
  • Process must release the resource when done
  • Deadlock
  • A set of processes is in a deadlock state when
    every process in the set is waiting for an event
    that can only be caused by another process in the
    set.

3
Deadlock Characterization
  • Necessary but not sufficient conditions
  • Mutual exclusion
  • Hold and wait
  • No preemption
  • Required condition
  • Circular wait - a closed chain of processes
    exists, such that each process holds at least one
    resource needed by the next process in the chain
  • Unresolvable circular wait is the definition of
    deadlock!
  • All four conditions must hold for deadlock

4
Deadlock
  • Reusable Resources
  • Can only be used by one process at a time. After
    use, can be reassigned to another process
    (printer, memory, files, etc.)
  • Deadlock can occur with two processes copying
    from disk to tape
  • Deadlock can occur with memory allocation when
    there is 200K available, both processes want 80K,
    then make second request for 70K
  • Consumable Resources
  • Can be created or destroyed (signals, messages)
  • No fixed limit on of resources
  • Can deadlock if both waiting for a message from
    the other

5
Handling Deadlock
  • Deadlock Prevention
  • Eliminate one of the 4 conditions
  • Deadlock Avoidance
  • Deadlock possible
  • Make appropriate dynamic choices based on current
    state
  • Deadlock Detection
  • Take some action to recover

6
Deadlock Prevention
  • Can prevent deadlock by forbidding one of the 4
    conditions
  • Forbid Mutual Exclusion
  • Generally not reasonable
  • Multiple read-only access but only exclusive
    access for writes
  • Forbid Hold and Wait
  • Ask for all resources at one time
  • May block a process for a long time when it can
    be making progress
  • Wait for resources it doesnt yet need
  • May tie up unneeded resources
  • May hold resources that are not needed
    immediately
  • May not know what to request
  • Especially true for interactive processes
  • May request resources because they are needed in
    some instances, but not always

7
Deadlock Prevention (continued)
  • Forbid No Preemption
  • Take resources away from waiting processes
    (require it to be released)
  • Only feasible if state can be saved
  • Examples CPU, Memory
  • Not usable for Files, Printer, etc.
  • Forbid Circular Wait
  • Define a linear order on items
  • If it needs resources 3, 15, 6, 9, and 4 then
    must request in the order 3, 4, 6, 9, 15
  • Cannot have circular wait because a process
    cannot have 9 and request 5
  • May not be easy to define the order (files)
  • May force an awkward order of requesting
    resources
  • May have to request resource 3 in order to
    request 4, even when 4 is needed now but 3 is not
    needed until much later

8
Deadlock Avoidance
  • Allow general requests, but make choices to avoid
    deadlock
  • Assume we know the maximum requests (claims) for
    each process
  • Process must state it needs a max of 5 A objects,
    3 B objects, 2 C objects.
  • Do not need to use its max claims
  • Ok to set max5 and only use 3
  • Can make requests at any time and in any order
  • Process Initiation Denial
  • Track current allocations
  • Assume all processes may make maximum requests at
    the same time
  • Only start process if it cant result in deadlock
    regardless of allocations

9
Resource Allocation Denial
  • Safe State We can finish all processes by some
    scheduling sequence
  • Example Finish P1, P4, P2, P5, P3
  • Bankers Algorithm (Dijkstra)
  • Reject a request if it exceeds the processes
    declared maximum claims
  • Grant a request if the new state would be safe
  • Determining if a state is safe
  • Find any process Pi for which we can meet its
    maximum requests
  • Don't forget already allocated resources
  • Mark Pi as done, add its resources to available
    resource pool
  • State is safe if we can mark all processes as
    done
  • Block a process if the resources are not
    currently available or the new state is not safe

10
Avoidance Example
Claim
Available
Resource
  • Are we in a safe state?

Yes!
  • P1 wants 1 more A and 1 more C. Is it ok (safe
    state)?

No!
No! But, there is a potential for deadlock.
  • Are we deadlocked?

11
Deadlock Detection
  • Avoidance methods tend to limit access to
    resources
  • Instead, grant arbitrary requests and note when
    deadlock happens
  • Can vary how often we check
  • Early detection vs. overhead of checks
  • Algorithm (Stallings, Figure 6.9)
  • Preparation
  • Create table of process requests, current
    allocations
  • Note available resources
  • Mark processes with no resources
  • Mark any process whose requests can be met
    (requests available resources)
  • Include resources from that process as
    available (this process can finish)
  • If multiple processes available, pick any
  • If any processes cannot be marked, they are part
    of a deadlock

12
Detection Example 1
Request
Allocation
Resource
Available
  • Mark P4 (not holding anything someone else wants)
  • Mark P3, new available 0 0 0 1 1
  • Cannot mark P1 or P2, so we are deadlocked

13
Detection Example 2
Requests
Allocation
Resource
Available
  • Are we deadlocked?
  • No!

14
Detection Example 3
Request
Allocation
Resource
Available
  • Cannot mark P1 thru P5, so we are deadlocked

15
Detection Example 4
Request
Allocation
Resource
Available
  • Mark P4, (nothing allocated)
  • Mark P2

, P1
, P3
, P5
16
Deadlock Detection Questions?
  • Does it really work?
  • How often do you run it?
  • How often is deadlock likely to occur?
  • How expensive is it?

17
Deadlock Recovery
  • Several possible approaches
  • Abort all deadlocked processes
  • Simple but common
  • Back up processes to a previously saved
    checkpoint, then restart
  • Assumes we have checkpoints and a rollback
    mechanism
  • Runs risk of repeating deadlock
  • Assumes that the deadlock has enough timing
    dependencies it wont happen
  • Selectively abort processes until deadlock broken
  • Preempt resources until deadlock broken
  • Must roll back process to checkpoint prior to
    acquiring key resource

18
Deadlock recovery
  • Process Termination
  • Kill them all
  • One at a time
  • Consider priority
  • Time computing
  • Who has most resources
  • Resource Preemption
  • Who gets preempted
  • Do you consider process rollback and starvation

19
Mixed Strategy
  • May group resources into classes, have a
    different deadlock strategy for each class
  • Swap Space
  • Prevent Deadlocks by requiring all space to be
    allocated at once
  • Avoidance also possible
  • Tapes/Files
  • Avoidance can be effective here
  • Prevention by ordering resources also possible
  • Main Memory
  • Preemption a good approach
  • Internal Resources (channels, etc.)
  • Prevention by ordering resources
  • Can use linear ordering between classes
Write a Comment
User Comments (0)
About PowerShow.com