Deadlock and Starvation - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Deadlock and Starvation

Description:

Starvation: Resource is always granted to other waiting processes. Example: 2 tape drives, P1 and P2 each hold one and need both ... { g.drawString(new Date(),10,30) ... – PowerPoint PPT presentation

Number of Views:87
Avg rating:3.0/5.0
Slides: 22
Provided by: cscinst
Category:

less

Transcript and Presenter's Notes

Title: Deadlock and Starvation


1
Deadlock and Starvation
  • Deadlock A set of Processes holding resources
    and blocked waiting for others held by a
    processes in the same wait set.
  • Starvation Resource is always granted to other
    waiting processes
  • Example 2 tape drives, P1 and P2 each hold one
    and need both
  • Example P0 wait(A) wait(B) P1 wait(B)
    wait(A)
  • Resource The narrow bridge
  • Deadlock Resolution Cars back up (preemption and
    rollback)
  • Starvation is possible.

2
Conditions Necessary for Deadlock
  • Hold and wait Processes partially allocate
    resources
  • Limited Resources There is a limited number of
    resources of a particular type. Processes will
    block if requesting a limited that is
    unavailable.
  • No preemption A resource can be released only
    voluntarily by the process holding it.
  • Circular wait there exists a process set P0,
    P1, , P0 where P0 is waiting for a resource
    that is held by P1, P1 is waiting for a resource
    that is held by P2, , Pn1 is waiting for a
    resource that is held by P0.

3
Directed Resource Allocation Graph
  • Process
  • Resource Type with 4 instances
  • Pi requests Rj instance
  • Pi holding Rj instance
  • Graph A set of vertices edges.
  • Two vertex categories
  • Circles represent Processes P1, P2, , Pn
  • Rectangles represent Resource types R1,R2,,Rm
  • Two edge categories
  • Pi requests resource typeRj Pi?Rj
  • Resource Rj instance assigned to Pi Rj?Pi

Pi
Pi
Note rectangles within Rj represent instances
wj,k Note processes request a resource, use
it, and then release it for others
4
Graph Cycles
  • No cycles ? no deadlock.
  • cycles ?
  • Deadlock if there is only one instance per
    resource type
  • Deadlock possibility if there is several
    instances per resource type

No Deadlock
Deadlock
5
Possible Deadlock
  • class B implements Runnable
  • private Lock one, two
  • public(Lock one,Lock two)
  • this.one one
  • this.two two
  • public void run()
  • try
  • two.lock()
  • doSomething()
  • one.lock()
  • doSomethingElse()
  • finally
  • two.unlock()
  • one.unlock()
  • class A implements Runnable
  • private Lock one, two
  • public(Lock one,Lock two)
  • this.one one
  • this.two two
  • public void run()
  • try
  • one.lock()
  • doSomething()
  • two.lock()
  • doSomethingElse()
  • finally
  • two.unlock()
  • one.unlock()

6
Handling Deadlocks
  • Prevention Eliminate one of the four necessary
    conditions
  • Avoidance Ensure that the system will never
    enter a deadlock state.
  • Recovery Allow the system to enter a deadlock
    state and then recover.
  • Ignore the possibility pretend that deadlocks
    never occur in the system used by most operating
    systems, including UNIX and WINDOWS. Defers the
    problem to application programs

7
Handling Deadlocks in Java
Avoid deprecated suspend method when leaving and
returning to applet
  • Thread t boolean okfalse Object mnew
    Object()
  • public void run()
  • while (true) try thread.sleep(1000)
    repaint()
  • synchronized(m)
  • while(okfalse)
    m.wait()
  • catch InterruptedException
    e)
  • public void start()
  • ok true if (tnull) tnew
    Thread(this).start
  • else synchronized(m) m.notify()
  • public void stop() synchronized(m) ok false
  • public void paint(Graphics g)
  • g.drawString(new Date(),10,30)

Question Why are the suspend and kill methods
deprecated?
8
Deadlock Prevention
Eliminate one of the necessary conditions
  • Hold and Wait
  • sharable resources like read-only files are
    deadlock free
  • Limited Resources
  • Resources with unlimited instances are deadlock
    free.
  • No Preemption (Disadvantage loss of data
    integrity)
  • Release all resources instead of blocking on a
    resource request. The preempted resources are
    added to the resource wait list.
  • Restarted when all requested resources are
    allocated.
  • Circular Wait (Disadvantage Low resource
    utilization)
  • Impose a total ordering of resource types.
    Processes must request resources in an increasing
    order of enumeration.
  • Processes cannot request resources when already
    holding others.

9
Deadlock Avoidance
  • The system must have know in advance the
    resources processes will need
  • All processes must return resources within a
    fixed amount of time
  • Safe state A sequence of requests exist that can
    be satisfied without deadlock
  • Safe state ? no deadlocks.
  • Unsafe state ? possibility of deadlock.
  • Deadlock avoidance algorithms disallow unsafe
    states
  • Avoidance algorithm pseudo code
  • System is initially safe
  • At each request for a resource
  • IF allocation causes an unsafe state THEN block
    the process
  • ELSE grant the allocation
  • At each resource release
  • WHILE a request can be granted safely
  • Grant the allocation unblock the process

10
Resource Allocation Graph for Deadlock Avoidance
  • Dashed line Pi to Rj Pi may request Rj
  • Solid line Pi to RjPi has requested Rj.
  • Solid line Rj to Pi Rj is assigned to Pi.

If all instances have one resource We can
implement a cycle detection algorithm
11
Dijkstra Request Algorithm
  • Request Algorithm
  • Let Request be a vector of length r. Requesti
    j k means that process Pi wants k instances
    of resource type Rj..
  • Throw Exception IF Requesti gt Needi
  • IF Requesti gt Available, block Pi
  • Tentatively allocate requested resources to Pi as
    followsAvailable - Requesti Allocationi
    Requesti Needi - Requesti
  • IF state is safe THEN Allocate
    resources to PiELSE Restore state and Block Pi

Notation There are r resources and p
processes Requesti, Allocationi, Needi, and
Available, are arrays of length r
12
Dijkstras Bankers Algorithm
  • Safety Algorithm
  • Let Work and Finish be vectors of length m and n,
    respectively.
  • Work Available
  • Finish i false for i - 1,3, , n.
  • WHILE an i exists such that both Finish
    ifalse AND Needi?WorkTHEN
    WorkWorkAllocationi Finishitrue
  • IF Finishitrue for all i, then the system is
    in a safe state.

Data structures for this algorithmp number of
processes, r number of resources types.
Available availablejk if k instances of Rj
available. Max maxi,jk if process Pi may
request up to k of Rj. Allocation
allocationi,jk if Pi currently owns k of
Rj. Need needi,j k if Pi may need k more of
Rj. need i,j maxi,j allocation i,j.
13
Dijkstras Algorithm Example
5 processes P0 through P4 and 3 resource types
(A,B,C) Resource A has 10 instances Resource B
has 5 instances Resource C has 7 instances.
  • The system is in a safe state
  • lt P1, P3, P4, P2, P0gt satisfies the safety
    criteria.
  • P1 now requests (1,0,2).
  • Request ? Need
  • Request ? Available
  • Execute safety algorithm
  • ltP1, P3, P4, P0, P2gt satisfies the safety
    criteria
  • Questions
  • Can request for (3,3,0) by P4 be granted?
  • Can request for (0,2,0) by P0 be granted?
  • Need lt Max Currently
  • to Own Allocated
  • A B C A B C A B C
  • P0 7 4 3 lt 7 5 3 0 1 0
  • P1 1 2 2 lt 3 2 2 2 0 0
  • P2 6 0 0 lt 9 0 2 3 0 2
  • P3 0 1 1 lt 2 2 2 2 1 1
  • P4 4 3 1 lt 4 3 3 0 0 2
  • Available 3 3 2

14
Deadlock Detection
Allow Deadlock, detect its occurrence, recover
  • Design Issues
  • Detect Run detection algorithm
  • When, and how often, to invoke?
  • How often a deadlock is likely to occur?
  • How many processes will need to be rolled back?
  • How many cycles and how many processes involved
    in each?
  • Can we detect the cause?
  • Recover
  • Abort all? Abort one or more processes till
    deadlock broken?
  • Which one? How to define the cost function?
  • Cost options priority, runtime used, time till
    completion, resources used, resources still
    needed, cascade affect, interactive or batch
    process
  • Rollback to safe state
  • How far to roll back?
  • Avoid starvation if Same process always fails

15
Deadlock Detection Single resource instances
  • Single Instance
  • Maintain wait-for graph
  • Nodes are processes.
  • Pi ? Pj if Pi is waiting for Pj.
  • Periodically invoke an algorithm to detect a
    cycle in the graph.
  • Text claims cycle detection requires O(V2). Is
    that true?

Resource allocation wait-for
graph graph
16
Deadlock DetectionMultiple Resource Instance
Algorithm
  • Data structures required for this algorithm
  • n number of processes, m number of resources
    types.
  • Available m length vector availablejk ? k
    instances of Rj free.
  • Allocation An n x m matrix Allocationi,jk ?
    Pi currently owns k of Rj.
  • Request An n x m matrix RequestI,jk ? Pi is
    requesting k more of Rj
  • Let Work and Finish be vectors of length m and n,
    respectively
  • Work AvailableFor each processor i IF
    Allocationi ? 0 Finishi falseELSE
    Finishi true.
  • WHILE there is an i such that finishifalse
    AND Requesti ?Work Work Work
    Allocationi Finishi true
  • IF Finishi false, for some i THEN System
    is deadlocked

17
Deadlock Detection Example
  • Sequence ltP0, P2, P3, P1, P4gt causes Finishi
    true for all i.
  • If P2 requests an additional instance of type C,
    the request array becomes
  • Request
  • A B C
  • P0 0 0 0
  • P1 2 0 1
  • P2 0 0 1
  • P3 1 0 0
  • P4 0 0 2
  • We can reclaim P0 resources, but there arent the
    resources to fulfill the other requests.
  • The deadlock consists of P1, P2, P3, and P4.
  • Description of the system
  • Five processes P0 through P4
  • Three resource types A has 7 instancesB has 2
    instancesC has 6 instances
  • Allocation Request Available
  • A B C A B C A B C
  • P0 0 1 0 0 0 0 0 0 0
  • P1 2 0 0 2 0 2
  • P2 3 0 3 0 0 0
  • P3 2 1 1 1 0 0
  • P4 0 0 2 0 0 2

18
Recovery Process Termination
  • Abort all deadlocked processes.
  • Abort one process at a time until the deadlock
    cycle is eliminated.
  • In which order should we choose to abort?
  • Priority of the process.
  • How long process has computed, and how much
    longer to completion.
  • Resources the process has used.
  • Resources process needs to complete.
  • How many processes will need to be terminated.
  • Is process interactive or is it a batch job?

19
Recovery Resource Preemption
  • Select a victim to minimize cost
  • How do we define cost?
  • Rollback return to a safe state and restart the
    process
  • Starvation
  • What is the same process is always the victim
  • Selection algorithm should consider the number of
    rollbacks

20
Conclusion
  • Most current operating systems ignore deadlocks.
    It is left to the application developer to
    handle.
  • There is not one approach that is sufficient
  • The eventual solution is to combine the three
    basic approaches of prevention, avoidance, and
    detection
  • Different resource classes can then employ a
    selected solution

21
Exercise
  • Refer to the above figure
  • How are all four conditions present?
  • How can deadlock be prevented, avoided, and
    detected?
Write a Comment
User Comments (0)
About PowerShow.com