0% found this document useful (0 votes)
6 views27 pages

OS 5 Deadlocks

The document discusses deadlocks in operating systems, defining them as situations where processes are waiting indefinitely for resources held by each other. It outlines strategies for deadlock detection, recovery, prevention, and avoidance, emphasizing the importance of understanding resource allocation and the conditions that lead to deadlocks. Various algorithms and methods are presented for managing deadlocks, including the Ostrich algorithm, detection algorithms, and the Banker's algorithm for resource management.

Uploaded by

Daniel Adeba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views27 pages

OS 5 Deadlocks

The document discusses deadlocks in operating systems, defining them as situations where processes are waiting indefinitely for resources held by each other. It outlines strategies for deadlock detection, recovery, prevention, and avoidance, emphasizing the importance of understanding resource allocation and the conditions that lead to deadlocks. Various algorithms and methods are presented for managing deadlocks, including the Ostrich algorithm, detection algorithms, and the Banker's algorithm for resource management.

Uploaded by

Daniel Adeba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Operating Systems

ECEG-5202

Deadlocks

Surafel Lemma Abebe (Ph. D.)


Outline
• Introduction
• Deadlock Detection and Recovery
• Deadlock Prevention
• Deadlock Avoidance

Surafel Lemma Abebe (Ph. D.) 2


Introduction
• Deadlock
A set of processes is deadlocked if each process in the set is waiting for
an event that only another process in the set can cause.
– The event could be a resource
=> Resource deadlock
• Resources
– Anything that can be acquired, used, and released over the course of
time
– Includes devices, data records, files, …
– Preemptable
• Possible to take away the resource before it finishes
• E.g., Memory
– Non-preemptable
• Resource cannot be taken away from its current owner without potentially
causing failure
• Usually involved in deadlocks

Surafel Lemma Abebe (Ph. D.) 3


Introduction…
• Resources…
– Abstract sequence of events required to use a
resource
1. Request the resource
2. Use the resource
3. Release the resource
– If the requested resource is not available,
requester
• Could be forced to block
• Could fail with an error code

Surafel Lemma Abebe (Ph. D.) 4


Introduction…
• Conditions that must exist for a (resource) deadlock to
occur
– Mutual exclusion
• Each resource is either currently assigned to exactly one process or is
available
– Hold and wait
• A process holding a resource is waiting to acquire additional resources
held by other processes
– No preemption
• A resource can be released only voluntarily by the process holding it,
after that process has completed its task
– Circular wait
• There exists a set {P0, P1, …, Pn} of waiting processes such that P0 is
waiting for a resource that is held by P1, P1 is waiting for a resource
that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn,
and Pn is waiting for a resource that is held by P0

Surafel Lemma Abebe (Ph. D.) 5


Introduction…
• Deadlock modeling
– (a) Process A is holding resource R
– (b) Process B is requesting a resource S
– (c) Deadlock

Surafel Lemma Abebe (Ph. D.) 6


Introduction…
• Deadlock modeling…

Surafel Lemma Abebe (Ph. D.) 7


Introduction…
• Strategies for dealing with deadlocks
1. Just ignore the problem
2. Detection and recovery
3. Dynamic avoidance by careful resource
allocation
4. Prevention, by structurally negating one of the
four conditions

Surafel Lemma Abebe (Ph. D.) 8


Ignore deadlock
• The Ostrich algorithm
– Pretend there is no problem
– Reasonable if
• Deadlocks occur very rarely
• Cost of prevention is high
– Unix and Windows take this approach

Surafel Lemma Abebe (Ph. D.) 9


Deadlock detection and recovery
• No attempt to prevent deadlocks from occurring
• Tries to detect when deadlock happens, and take
some action to recover
• Deadlock detection
– One resource of each type
• There is only one resource of each type
– E.g., one printer, one CD recorder, …
• Construct a resource graph
• See if there is a cycle
– One or more cycles indicate deadlock
• There are many algorithms for detecting cycles in directed
graphs

Surafel Lemma Abebe (Ph. D.) 10


Deadlock detection and recovery…
• Deadlock detection…
– One resource of each type…
• Algorithm
– Uses one dynamic data structure, L, a list of nodes and list of arcs
1. Take each node in turn as the root of a tree
2. Do a depth-first search
1. If the search finds a node that is already in the list, it has
found a cycle
2. If it exhausts all the arcs from any given node, it backtracks
to the previous node
• If it backtracks to the root and cannot go further, the
sub-graph reachable from the current node doesn’t
contain any cycle

Surafel Lemma Abebe (Ph. D.) 11


Deadlock detection and recovery…
• Deadlock detection…
– One resource of each type…
• Example
– Start from R
» L = {R, A, S}

– Start from B
» L = {B, T, E, V, G, U, D, S}
» L = {B, T, E, V, G, U, D, T}
• Deadlock

Surafel Lemma Abebe (Ph. D.) 12


Deadlock detection and recovery…
• Deadlock detection…
– Multiple resources of each type
• Let
m = the number of resource classes (e.g., printer, CD-ROM)
E = existing resource vector (E1 = resources of class 1, E2 = resources
of class 2; E1 = 2 means there are 2 items of resource 1)
A = available resource vector (Ai = number of instances of resource i
that are currently available)
C = current allocation matrix (Cij = number of instances of resource j
that are held by process i)
R = request matrix (Rij = number of instances of resource j that
process i wants)
• Every resource is either allocated or available

Surafel Lemma Abebe (Ph. D.) 13


Deadlock detection and recovery…
• Deadlock detection…
– Multiple resources of each type…

Surafel Lemma Abebe (Ph. D.) 14


Deadlock detection and recovery…
• Deadlock detection…
– Multiple resources of each type…
• Algorithm for detection
– Assumes all processes keep all acquired resources until they
exit (worst case scenario)
– Each process is initially said to be unmarked
1. Look for an unmarked process, Pi , for which the ith row of R
is less than or equal to A
2. If such a process is found, add the ith row of C to A, mark the
process, and go back to step 1
3. If no such process exists, the algorithm terminates

Surafel Lemma Abebe (Ph. D.) 15


Deadlock detection and recovery…
• Deadlock detection…
– Multiple resources of each type…
• Example
– 1st and 2nd processes do not satisfy step 1
– 3rd process (3rd row ≤ A), execute and return all resources in C
=> A = (2, 2, 2, 0)
– Process 2 can now execute (2nd request row ≤ A) and return all resources in C
=> A=(4,2,2,1)
– Process 1 will then execute
• Example: (Add) process 3 requires a Blue-ray drive
=> dead lock

Surafel Lemma Abebe (Ph. D.) 16


Deadlock detection and recovery…
• Deadlock detection…
– Multiple resources of each type…
• When to look for deadlocks?
– Check every time a resource is requested
» Advantage: Detects deadlock as early as possible
» Disadvantage: Expensive in terms of CPU time
– Check every k minutes
– Check when CPU utilization has dropped
» if enough processes are deadlocked, there will be few
runnable processes, and the CPU will often be idle

Surafel Lemma Abebe (Ph. D.) 17


Deadlock detection and recovery…
• Recovery from deadlock
– Recovery through preemption
• Temporarily take a resource away from its current owner and give it to
another process
• Depends on the nature of resource to be taken away
– Identifying a process with such resource is difficult or impossible
– Recovery through rollback
• Processes are checkpointed periodically
• Checkpoiniting
– Save states of a process so that it can be restarted later
• When a deadlock is detected, a process that holds a needed resource
is rolled back to a point in time before it acquired that resource
– Recovery through killing processes
• Requires to properly select a process to be killed
• Example: A process that can be rerun

Surafel Lemma Abebe (Ph. D.) 18


Deadlock prevention
• Based on conditions for deadlock
• Attacking the mutual-exclusion condition
– No resource is assigned exclusively to a single
process
– Processes use the resource concurrently
• Make resource read only
• Using a spooler for printer
– Avoid assigning a resource unless absolutely
necessary
Surafel Lemma Abebe (Ph. D.) 19
Deadlock prevention…
• Attacking the hold-and-wait condition
– Prevent a process that hold a resource from waiting for
more resources
– Require all processes to request all their resources before
starting execution
– Problem
• Processes may not know how many resources they will need until
they start running
– Solution
• Programmers are required to list the resources they need for their
program
– Problem: Overhead to the programmer
– Not optimal, might hold a resource that it doesn’t need
until sometime later in the execution

Surafel Lemma Abebe (Ph. D.) 20


Deadlock prevention…
• Attacking the no-preemption condition
– Allow preemption
– If a process that is holding some resources requests
another resource that cannot be immediately
allocated to it, then all resources currently being held
are released
– Preempted resources are added to the list of
resources for which the process is waiting
– Process will be restarted only when it can regain its
old resources, as well as the new ones that it is
requesting

Surafel Lemma Abebe (Ph. D.) 21


Deadlock prevention…
• Attacking the circular wait condition
• Impose a total ordering of all resource types, and require that
each process requests resources in an increasing order of
enumeration
• Example
– Either I < j or j < i

• Variation
– No process request a resource lower than what it is already holding
• Challenge
– May be impossible to find an ordering that satisfies everyone

Surafel Lemma Abebe (Ph. D.) 22


Deadlock avoidance
• Is there an algorithm that can always avoid deadlock by making the
right choice all the time?
– Yes… but only if certain information is available in advance

– Requires that each process declare the maximum number of resources


of each type that it may need
– The deadlock-avoidance algorithm ensure that there can never be a
circular-wait condition
– Resource-allocation state is defined by the number of available and
allocated resources, and the maximum demands of the processes
– When a process requests an available resource, system must decide if
immediate allocation leaves the system in a safe state
• Safe and unsafe states
– System is in safe state
• If there is some scheduling order in which every process can run to
completion even if all of them suddenly request their maximum number of
resources

Surafel Lemma Abebe (Ph. D.) 23


Deadlock avoidance…
• Safe and unsafe states…
– System is in safe state …
• if there exists a sequence <P1, P2, …, Pn> of ALL the processes in
the systems such that for each Pi, the resources that Pi can still
request can be satisfied by currently available resources and
resources held by all the Pj, with j < i
– That is:
• If Pi resource needs are not immediately available, then Pi
can wait until all Pj have finished
• When Pj is finished, Pi can obtain needed resources, execute,
return allocated resources, and terminate
• When Pi terminates, Pi +1 can obtain its needed resources,
and so on

Surafel Lemma Abebe (Ph. D.) 24


Deadlock avoidance…
• Safe and unsafe states…
– Example
• State at (a) is safe. Why?
– There is a sequence of allocation that allows all processes to complete
B-> C -> A
– Exercise
• If A requests and gets 1 resource (i.e., A Has 4 resources and Free
is 2), can we find a sequence that is guaranteed to work?

Surafel Lemma Abebe (Ph. D.) 25


Deadlock avoidance…
• Safe and unsafe states…
– Unsafe state is not a deadlocked state
– From unsafe state, there is no guarantee that all
processes will finish

Surafel Lemma Abebe (Ph. D.) 26


Deadlock avoidance…
• Banker’s algorithm for multiple resource
– Combines the deadlock detection algorithm for
multiple resources with the concept of safe and
unsafe state
– If a request leads to unsafe state, it must be deferred
for a while
– Limitation
• Processes rarely know in advance what their maximum
resource needs will be
• Number of processes is not fixed
• Resources which were available at one time, may not be
available in another time

Surafel Lemma Abebe (Ph. D.) 27

You might also like