0% found this document useful (0 votes)
9 views

Chapter 3

The document discusses deadlock in computer systems. It defines deadlock as when a set of processes are blocked waiting for resources held by each other in a cyclic manner. Four conditions must occur simultaneously for deadlock to happen: mutual exclusion, hold and wait, no preemption, and circular wait. Deadlock can be modeled using resource allocation graphs. Approaches to handle deadlock include prevention, detection and recovery, avoidance, and ignoring the problem. Prevention methods aim to satisfy structural conditions to avoid deadlock states from occurring.

Uploaded by

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

Chapter 3

The document discusses deadlock in computer systems. It defines deadlock as when a set of processes are blocked waiting for resources held by each other in a cyclic manner. Four conditions must occur simultaneously for deadlock to happen: mutual exclusion, hold and wait, no preemption, and circular wait. Deadlock can be modeled using resource allocation graphs. Approaches to handle deadlock include prevention, detection and recovery, avoidance, and ignoring the problem. Prevention methods aim to satisfy structural conditions to avoid deadlock states from occurring.

Uploaded by

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

Deadlock

Contents:
 Introduction

 Resource

 Deadlock characterization

 Deadlock modeling

 Deadlock handling mechanism

 Deadlock prevention

 Deadlock detection and recovery

 Deadlock avoidance

 Two phase locking

 Communication deadlock

 Live lock

 starvation

1
Introduction
• 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.
• all the processes are waiting, none of them will ever cause any of the
events that could wake up any of the other members of the set, and all the
processes continue to wait forever.
• Each member of the set of deadlocked processes is waiting for a resource
that is owned by a deadlocked process.
• None of the processes can run, none of them can release any resources,
and none of them can be awakened.
• This kind of deadlock is called a resource deadlock. It is probably the
most common kind, but it is not the only kind.
2
Analogy

Deadlock

System Breakdown
3
Resource deadlock

Deadlock:
A set of blocked processes, each:
11 Holding a resource and

22 Waiting to use a resource held by another process in the set.

Give me your Give me your


recourse recourse first

Deadlock
Resource
Process B Resource
Process A

Hence, blocked processes will never change state (Explain why?)


because the resource it has requested is held by another waiting process. 4
Resource
o A major class of deadlocks involve resources.
o Deadlocks can occur when processes have been granted exclusive access to devices,
data records, files, and so forth.
o In general the objects granted to a process is referred to as resources.
o A resource can be a hardware device (e.g., a tape drive) or a piece of information
(e.g., a locked record in a database).
o Process must request a resource before using it and release it after making use of
it. Each process utilizes a resource as follows:
 Request :A process requests for an instance of a resource type. If the resource is
free, the request will be granted. Otherwise the process should wait until it
acquires the resource
 Use :The process uses the resource for its operations
 Release : The process releases the resource 5
Deadlock characterization

Deadlock can arise if four conditions hold simultaneously in a system.

Deadlock conditions

Mutual Circular wait


Exclusion

Hold and No preemption


wait

6
Mutual Exclusion
only one process can use a resource at a time.

Hold and Wait


A process holding at least one resource is waiting for additional resource held by
another processes

No Preemption
A resource is released only by the process holding it after it
completed its task

Circular Wait
A set of a processes each waits for another one in a circular fashion.

Note: the four conditions must occur to have a deadlock. If one condition
is absent, a deadlock may not exist. 7
Circular
wait
There exists a set {P0, P1, P2, ….., Pn} of waiting processes such that:

P0 waiting for a resource held by P1.


P1 waiting for a resource held by P2.
P2 waiting for a resource held by P3. P0
Pn P1

Circular P2
Wait
P3
P4
Pn waiting for a resource held by P0.

8
Deadlock modeling
In order to solve the deadlock problem, we must find a method to
express it.
This can be achieved using resource allocation graph.
The graphs have two kinds of nodes and directed arc.
 Processes, shown as circles
 Resources, shown as squares.
A directed arc from a resource node to a process node means that
the resource has previously been requested by, granted to, and is
currently held by that process.
A directed arc from a process to a resource means that the process is
currently blocked waiting for that resource. 9
Deadlock modeling(con’t…)

• P (Process Set) ≡ set of processes in the system


Process
• P = {P1, P2, P3, ……., Pn}

• R (Resource Set) ≡ set of Resource types in the system


Resource
• R = {R1, R2, R3, ……., Rm}

Edge

Rj Rj
Pi Pi
( Request edge ) ( Assignment edge )
Process Pi requests an instance of resource Rj An instance of Resource Rj is assigned
to Process Pi

• E (Edge Set) ≡ set of all edges in Resource Allocation Graph.


• E = {Pn  Rm, Rx Py, …….. } 10
Deadlock modeling(con’t…)
Example:
11
Resource instances:
• One instance of R1 and R3.
• Two instances of R2.
• Three instances of R4.

22 Processes States:
Waiting Holding Process
Instance of R1 Instance of R2 P1
Instance of R3 Instance of R1 and R2 P2
------- Instance of R3 P3 11
Deadlock modeling(con’t…)
If a Resource Allocation Graph contains a cycle, then a deadlock
may or may not exist.
Case 1: The system has one instance per resource type
• If a cycle exists, the system in a deadlock state.
• Each process involved in the cycle is deadlocked.
P1
Cycle:

R2 R1 P1  R1  P2  R2
Cycle
(Deadlock)

As shown, no chance to break the cycle because:


P2 • No process can finish execution.
• No way to free a resource.
12
Case 2: The system has more than one instance per resource type
• If a cycle exists, the system may or may not be in a deadlock state.
• Ex 1: a cycle with deadlock:

Two Cycles exists:


P1  R1  P2  R3  P3 R2

P2  R3  P3  R2

As shown, P1, P2, and P3 are deadlocked

13
Ex 2: a cycle without deadlock:

Cycle:
P1  R1  P3  R2

There is no deadlock because:


• P4 may release its instance of R2.
• This resource can be then allocated to P3
which breaking the cycle.

• Also P2 may release its instance of R1.


• This resource can be allocated to P1.

14
Deadlock handling mechanism
Deadlock problems can be handled in one of the following 4 ways:
1. Structurally negating one of the four required conditions (deadlock
prevention).
2. Allow the system to enter a deadlock state and then recover
(deadlock detection and recovery)
3. Using a protocol avoids deadlock by ensuring that a system will
never enter a deadlock state (deadlock avoidance)
4. Ignore the problem and pretend that deadlocks never occur in the
system(ostrich algorithm); used by most operating systems,
including UNIX
15
Deadlock prevention
1. Mutual Exclusion – This is not required for sharable resources; however
to prevent a system from deadlock, the mutual exclusion condition must
hold for non-sharable resources
2. Hold and Wait – in order to prevent the occurrence of this condition in a
system, we must guarantee that whenever a process requests a resource, it
does not hold any other resources. Two protocols are used to
implement this:
1. Require a process to request and be allocated all its resources
before it begins execution or
2. Allow a process to request resources only when the process has
none 16
Deadlock prevention(con’t…)
Both protocols have two main disadvantages:
o Since resources may be allocated but not used for a long
period, resource utilization will be low
o A process that needs several popular resources has to wait
indefinitely because one of the resources it needs is allocated
to another process. Hence starvation is possible.
3. No Preemption
• If a process holding certain resources is denied further request,
that process must release its original resources allocated to it

17
Deadlock prevention(con’t…)
• If a process requests a resource allocated to another process
waiting for some additional resources, and the requested resource
is not being used, then the resource will be preempted from the
waiting process and allocated to the requesting process
» 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
» This approach is practical to resources whose state can easily
saved and retrieved easily
18
Deadlock prevention(con’t…)

4. Circular Wait
• A linear ordering of all resource types is defined and each
process requests resources in an increasing order of
enumeration.
• So, if a process initially is allocated instances of resource type
R, then it can subsequently request instances of resources
types following R in the ordering

19
Deadlock detection and recovery
o The system does not attempt to prevent deadlocks from occurring.
o It lets them occur, tries to detect when this happens, and then takes
some action to recover after the fact.
o In this mechanism, the system must provide :

 A deadlock detection algorithm that examines the state of the


system if there is an occurrence of deadlock
 An algorithm to recover from the deadlock

20
Deadlock Detection with One Resource of Each Type
o only one resource of each type exists.
o If the resource allocation graph contains one or more cycles, a
deadlock exists.
o Any process that is part of a cycle is deadlocked. If no cycles
exist, the system is not deadlocked.
o Many algorithms for detecting cycles in directed graphs are
known.
o Below we will give a simple one that inspects a graph and
terminates either when it has found a cycle or when it has shown
that none exists.
21
Deadlock Detection with One Resource of Each Type
The algorithm operates by carrying out the following steps as
specified:
1. For each node, N in the graph, perform the following five steps
with N as the starting node.
2. Initialize L to the empty list, and designate all the arcs as
unmarked.
3. Add the current node to the end of L and check to see if the node
now appears in L two times. If it does, the graph contains a
cycle(listed in L) and the algorithm terminates.
4. From the given node, see if there are any unmarked outgoing arcs.
22
Deadlock Detection with One Resource of Each Type

5. Pick an unmarked outgoing arc at random and mark it. Then


follow it to the new current node and go to step 3.
6. If this node is the initial node, the graph does not contain any
cycles and the algorithm terminates. Otherwise, we have now
reached a dead end. Remove it and go back to the previous node,
that is, the one that was current just before this one, make that
one the current node, and go to step 3.

23
Example

 The order of processing the nodes is from left to right, top to bottom i.e (R, A, B, C, S, D,

T, E, F, and so forth.)

1. Start at R and initialize L to the empty list.

2. Then we add R to the list and since it appear only once in L. visit unmark outgoing arc

from R.

3. There is only one outgoing arc which is move to A and add it to L, giving L = [R, A].and

visit unmarked for node A. and follow to node S. 24


Example(con’t…)

4. Since there is no unmarked outgoing node, it backtrack to previous


node A. also the outgoing arc from node visited or marked, it backtrack
to previous node, which is root node R. so from this sub graph there is
no cycle found, go to another which is Node A.
5. Follow the same step as node R. it cannot found cycle. Then got the
node B.
6. Following the same step we can get L= [B, T, E, V, G, U, D] at given
instant time. By choosing one of outgoing arc randomly from Node we
can get L= [B, T, E, V, G, U, D,T], if we pick T.
7. Since T is found twice in L, it found cycle from node b, which is
deadlock. At this time algorithm is terminated. 25
Deadlock Detection with Multiple Resources of Each Type
o multiple copies of some of the resources exist.

o Matrix-based algorithm for detecting deadlock among n processes, P 1 through Pn


is used.
o The algorithm uses several data structures similar to the ones in banker’s algorithm.

 Existence: A vector of length m indicates the total number of existing instance


of each resources type.
 Available: A vector of length m indicates the number of available resources of
each type.
 Allocation: An n x m matrix defines the number of resources of each type
currently allocated to each process.
 Request: An n x m matrix indicates the current request of each process. If
Request [ij] = k, then process Pi is requesting k more instances of resource type.

Rj. 26
Deadlock Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively
Initialize:
(a) Work = Available
(b)For i = 1,2, …, n, if Allocationi  0, then
Finish[i] = false; otherwise, Finish[i] = true.
2. Find an index i such that both:
(a) Finish[i] == false
(b)Requesti  Work; If no such i exists, go to step 4.
3. Work = Work + Allocationi
Finish[i] = true; go to step 2
4. If Finish[i] == false, for some i, 1  i  n, then the system is in
deadlock state.
 Moreover, if Finish[i] == false, then Pi is deadlocked.

27
Deadlock Detection Algorithm (contd.)

Example
• Five processes P0 through P4; three resource types :A (7 instances), B (2
instances), and C (6 instances)
Allocation Request Available Existing
ABC ABC ABC ABC

P0 010 000 000 7 2 6

P1 200 202

P2 303 000

P3 211 100

P4 002 002

• Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i. 28
Deadlock Detection Algorithm (cont.)
• P2 requests an additional instance of type C.

Request
ABC
P0 000
P1 201
P2 001
P3 100
P4 002

• State of system?

– Can reclaim resources held by process P0, but insufficient resources to


fulfill other processes; requests.
29
Recovery from Deadlock
o Once a deadlock has been detected, recovery strategy is needed. There are two
possible recovery approaches:
Process termination
Resource preemption
Process Termination
Abort all deadlocked processes

Abort one process at a time until the deadlock cycle is eliminated

 In which order should we choose a process to abort? Chose the process with
 Least amount of processor time consumed so far
 Least amount of output produced so far
 Most estimated time remaining
 Least total resources allocated so far
 Lowest priority 30
Recovery from Deadlock
Resource Preemption
o In this recovery strategy, we successively preempt resources and allocate them to another
process until the deadlock is broken
o While implementing this strategy, there are three issues to be considered

 Selecting a victim – which resources and process should be selected to minimize cost
just like in process termination. The cost factors may include parameters like the number
of resources a deadlocked process is holding, number of resources it used so far
 Rollback – if a resource is preempted from a process, then it can not continue its normal
execution
• The process must be rolled back to some safe state and started
 Starvation – same process may always be picked as victim several times. As a result,
starvation may occur. The best solution to this problem is to only allow a process to be
picked as a victims for a limited finite number of times. This can be done by including
the number of rollback in the cost factor
31
Deadlock avoidance
• Deadlock avoidance scheme requires each process to declare the
maximum number of resources of each type that it may need in
advance
• The deadlock-avoidance algorithm dynamically examines the
resource-allocation state to 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.
• Simplest and most useful model requires that each process declare
the maximum number of resources of each type that it may need.
32
Safe and Unsafe States
• A state is said to be safe 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 immediately.
• A state said to be unsafe if there is no granted, given to any of process to
complete.

• If a system is in a safe state, then there

are no deadlocks.

• If a system is in unsafe state, then there

is a possibility of deadlock

• Deadlock avoidance method ensures

that a system will never enter an unsafe

state 33
Example(safe state)
• Show whether the following process are safe or not using a total of
10 instances of the resource exist
• the state is safe because the system, by careful scheduling, can
avoid deadlock.

34
Example(unsafe state)
• the state of is unsafe because the system, have no sequence that
guarantees completion.

35
Deadlock Avoidance Algorithms

• Based on the concept of safe state, we can define algorithms


that ensures the system will never deadlock.
• If there is a single instance of a resource type,

Use a resource-allocation graph

• If there are multiple instances of a resource type,

Use the Dijkstra’s banker’s algorithm

36
Deadlock Avoidance Algorithms (contd.)
Resource-Allocation Graph Scheme
• A new type of edge (Claim edge), in addition to the request and assignment
edge is introduced.

• Claim edge Pi  Rj indicates that process Pi may request resource Rj at some


point in the future. The edge resembles a request edge but is represented by a
dashed line in the graph
– Claim edge is converted to request edge when a process requests a resource
– Request edge is converted to an assignment edge when the resource is
allocated to the process
– When a resource is released by a process, assignment edge reconverts to a
claim edge
– If no cycle exists in the allocation, then system is in safe state otherwise the
system is in unsafe state 37
Deadlock Avoidance Algorithms (contd.)

Assignment Request Edge


Edge

Claim Edge

Suppose that process Pi requests a resource Rj

The request can be granted only if converting the request edge to


an assignment edge does not result in a cycle in the resource
38
allocation graph
Deadlock Avoidance Algorithms (contd.)
Example: consider the following resource allocation graph:

Suppose that
P2 requests R2

Although R2 is free, we can not allocate it to P2 since this creates a cycle

Cycle

39
Deadlock Avoidance Algorithms (contd.)
Banker’s Algorithm

• This algorithm is used when there are multiple instances of resources


• When a process enters a system, it must declare the maximum number
of each instance of resource types it may need

– The number however may not exceed the total number of

resource types in the system

• When a process requests a resource it may have to wait

• When a process gets all its resources it must return them in a finite
amount of time

40
Deadlock Avoidance Algorithms (contd.)
Banker’s Algorithm
• The following data structures are used in the algorithm:
Let n = number of processes, and
m = number of resources types.
• Available: Vector of length m.

– If available [j] = k, there are k instances of resource type Rj available.

• Max: n x m matrix.

– If Max [i,j] = k, then process Pi may request at most k instances of resource

type Rj.

• Allocation: n x m matrix.

– If Allocation[i,j] = k then Pi is currently allocated k instances of Rj.

• Need: n x m matrix.

– If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task


41
Need [i,j] = Max[i,j] – Allocation [i,j]
Deadlock Avoidance Algorithms (contd.)
Banker’s Algorithm
The algorithm is as follows:
1. Process pi makes requests for resources. Let Request(i) be the corresponding
request vector. So, if pi wants k instances of resource type rj, then Request(i)[j]
=k
2. If Request(i) !≤ Need(i), there is an error.
3. Otherwise, if Request(i) !≤ Available, then pi must wait
4. Otherwise, Modify the data structures as follows :
• Available = Available - Request(i)
• Allocation(i) = Allocation(i) + Request(i)
• Need(i) = Need(i) - Request(i)
5. Check whether the resulting state is safe. (Use the safety algorithm presented in
the next slide)
42
6. If the state is safe, do the allocation. Otherwise, pi must wait for Request(i)
Deadlock Avoidance Algorithms (contd.)
Banker’s Algorithm
Safety Algorithm
• It is used to identify whether or not a system is in a safe state. The algorithm can be
described as follow:
1. Let Work and Finish be vectors of length m and n, respectively. Initialize:

Work = Available
Finish [i] = false for i = 0, 1, …, n- 1.
2. Find an i such that both:
(a) Finish [i] = false

(b) Needi  Work; If no such i exists, go to step 4.

3. Work = Work + Allocationi


Finish [i] = true; go to step 2.
4. If Finish [i] == true for all i, then the system is in a safe state.
43
Deadlock Avoidance Algorithms (contd.)
Banker’s Algorithm

Examples of Banker’s algorithm:


• Assume a system has
– 5 processes P0 through P4;
– 3 resource types: A (10 instances), B (5instances), and C (7
instances)
Allocation Max Available Need
ABC ABC ABC
ABC P0 7 4 3
P0 010 753 332 P1 1 2 2
P1 200 322 P2 6 0 0
P2 302 902 P3 0 1 1
P3 211 222 P4 4 3 1
P4 002 433
• The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria44
Deadlock Avoidance Algorithms (contd.)
Banker’s Algorithm

Example: P1 Request (1,0,2)


• Check that Request  Available (that is, (1,0,2)  (3,3,2)  true

Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies
safety requirement.

• Can request for (3,3,0) by P4 be granted?

• Can request for (0,2,0) by P0 be granted?

Allocation MaxAvailable Allocation Need Available


ABC ABC ABC
ABC ABC AB
P0 0 1 0 743 230
C P1 3 0 2 020
P0 010 753 332 P2 3 0 2 600
P1 200 322 P3 2 1 1 011
P2 302 902 P4 0 0 2 431
P3 211 222 45
Two-Phase Locking
o Deadlock avoidance and prevention are not terribly promising in the general
case for deadlock handling.
o When multiple processes are running at the same time, there is a real danger
of deadlock.
o As an example, in many database systems, an operation that occurs frequently
is requesting locks on several records and then updating all the locked
records.
o The approach often used is called two-phase locking.

o In the first phase, the process tries to lock all the records it needs, one at a
time.
o If it succeeds, it begins the second phase, performing its updates and
46
releasing the locks.
Communication deadlock
o process wants something that another process has and must wait until the first one gives it up.

o Another kind of deadlock can occur in communication systems (e.g., networks), in which two

or more processes communicate by sending messages.


o A common arrangement is that process A sends a request message to process B, and then

blocks until B sends back a reply message.


o Communication deadlocks cannot be prevented by ordering the resources (since there are

none) or avoided by careful scheduling (since there are no moments when a request could be
postponed).
o The technique that can usually be employed to break communication deadlocks: timeouts.

o In most network communication systems, whenever a message is sent to which a reply is

expected a timer is also started.


o If the timer goes off before the reply arrives, the sender of the message assumes that the
message has been lost and sends it again (and again and again if needed).

47
Livelock
o In some situations, polling (busy waiting) is used to enter a critical region or access
a resource.
o Let pair of processes (process A and process B)using two resources.

o process A is use resource 1 and request resource 2 and process B use resource 2
and request resource 1
o if the processes wait for required resource by spooling rather than blocking, this
situation is called livelock.
o Thus we do not have a deadlock (because no process is blocked) but we have
something functionally equivalent to deadlock; will make no further progress.
Starvation

o A problem closely related to deadlock and live lock is.

o When processes wait for execution, but it cannot be possible to get CPU to be run,
because of scheduler or something else these processes are said to be under
48
starvation.

You might also like