Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lecture 4 Deadlocks

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

IT105

Operating Systems

Process Management: Deadlocks

Harris Chikunya

1
Objectives
• By the end of this unit, you should be able to:
 Define a deadlocks;
 Discuss four conditions for a deadlock;
 Explain the ways of avoiding deadlocks; and
 Describe two ways to recover from the deadlock situation.

2
Resources
• There are two types of resources namely: Pre-emptable and Non-
pre-emptable Resources.
a) Pre-emptable Resources
• This resource can be taken away from the process with no ill effects.
Memory is an example of a pre-emptable resource.
b) Non-preemptable Resource
• This resource cannot be taken away from the process (without causing ill
effect). For example, CD resources are not pre-emptable at any arbitrary
moment.
• Under normal mode of operation, a process may utilize a resource in
only the following sequence:
 Request: If the request cannot be granted immediately, then the
requesting process must wait until it can acquire the resource.
 Use: The process can operate on the resource.
 Release: The process releases the resource.
3
Deadlocks
• A process requests resources; if the resources are not available at
that time, the process enters a wait state.
• It may happen that waiting processes will never again change state,
because the resources they have requested are held by other
waiting processes.
• This situation is called a deadlock.

4
Characterisation of a Deadlock
1. Mutual Exclusion condition
2. Hold and Wait condition
3. No Preemption
4. Circular Wait

5
Mutual Exclusion Condition
• The resources involved are non-shareable.
• At least one resource must be held in a non-shareable mode, that is,
only one process at a time claims exclusive control of the resource.
• If another process requests that resource, the requesting must be
delayed until the resource has been released.

6
Hold and Wait Condition
• In this condition, a requesting process already holds resources and
waiting for the requested resources.
• There must exist a process that is holding a resource already allocated
to it while waiting for additional resource that are currently being
held by other processes.

7
No-Preemption Condition
• Resources already allocated to a process cannot be preempted.
• Resources cannot be removed forcibly from the processes.
• After completion, they will be released voluntarily by the process
holding it.

8
Circular Wait Condition
• The processes in the system form a circular list or chain where each
process in the list is waiting for a resource held by the next process
in the list.
• There exists a set {P0, P1, …, P0} 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 P0 is waiting for a resource that is held by P0.

9
Deadlock Example

10
Resource allocation Graph
• This graph consists of a set of vertices V and a set of edges E.
• The set of vertices V is partitioned into two different types of nodes P = {PI,
P2, ..., Pn}, the set consisting of all the active processes in the system, and
R = {R1, R2, ..., Rm}, the set consisting of all resource types in the system.

• A directed edge from process Pi to resource type Rj is denoted by PiRj; it signifies


that process Pi requested an instance of resource type Rj and is currently waiting
for that resource.
• A directed edge from resource type Rj to process Pi is denoted by RjPi; it signifies
that an instance of resource type Rj has been allocated to process Pi.

11
Resource allocation Graph illustration

12
Resource Allocation Graph
The following are the ways which will help you to check the graph
easily to predict the presence of a deadlock:
(a) If no cycle exists in the resource allocation graph, there is no
deadlock.
(b) If there is a cycle in the graph and each resource has only one
instance, then there is a deadlock. In this case, a cycle is a necessary
and sufficient condition for deadlock.
(c) If there is a cycle in the graph, and each resource has more than
one instance, there may or may not be a deadlock.
13
Example
• Suppose that process P3 requests an instance of resource type R2.
Since no resource instance is currently available, a request edge
P3→ R2 is added to the graph.
• At this point, two minimal cycles exist in the system:
P1→R1→ P2→ R3→ P3→ R2→ P1
P2→ R3 → P3→ R2→ P2

14
Dealing with Deadlocks
• In general, there are four strategies of dealing with deadlock
problem:
1. Deadlock Prevention: Prevent deadlock by resource scheduling so as
to negate at least one of the four conditions.
2. Deadlock Avoidance: Avoid deadlock by careful resource scheduling.
3. Deadlock Detection and Recovery: Detect deadlock and when it
occurs, take steps to recover.
4. The Ostrich Approach: Just ignore the deadlock problem altogether.

15
Deadlock Prevention
1. Elimination of “Mutual Exclusion” Condition
• The mutual exclusion condition must hold for non-shareable
resources.
• That is, several processes cannot simultaneously share a single
resource.
• This condition is difficult to eliminate because some resources, such
as the disc drive and printer, are inherently non-shareable.
• Note that shareable resources like read-only-file do not require
mutually exclusive access and thus cannot be involved in deadlock.
16
Deadlock Prevention
2. Elimination of “Hold and Wait” Condition
• The first alternative is that a process request be granted all the resources it
needs at once, prior to execution.
• The second alternative is to disallow a process from requesting resources
whenever it has previously allocated resources.
• This strategy requires that all the resources a process will need must be
requested at once.
• The system must grant resources on “all or none” basis.
• If the complete set of resources needed by a process is not currently
available, then the process must wait until the complete set is available.
• While the process waits, however, it may not hold any resources.
• Thus the “wait for” condition is denied and deadlocks simply cannot occur.
• This strategy can lead to serious waste of resources.

17
Deadlock Prevention
3. Elimination of “Non-preemption” Condition
• The non-preemption condition can be alleviated by forcing a process
waiting for a resource that cannot immediately be allocated to relinquish
all of its currently held resources, so that other processes may use them
to finish.
• This strategy requires that when a process that is holding some resources
is denied a request for additional resources.
• The process must release its held resources and, if necessary, request
them again together with additional resources.
• Implementation of this strategy denies the “no-preemptive” condition
effectively. 18
Deadlock Prevention
4. Elimination of “Circular Wait” Condition
• The last condition, the circular wait, can be denied by imposing a
total ordering on all of the resource types and then forcing, all
processes to request the resources in order (increasing or
decreasing).
• This strategy impose a total ordering of all resources types, and to
require that each process requests resources in a numerical order
(increasing or decreasing) of enumeration.
• With this rule, the resource allocation graph can never have a cycle.
19
Deadlock Avoidance
• This approach to the deadlock problem anticipates deadlock before
it actually occurs.
• This approach employs an algorithm to access the possibility that
deadlock could occur and acting accordingly.
• If the necessary conditions for a deadlock are in place, it is still
possible to avoid deadlock by being careful when resources are
allocated.
• It employs the most famous deadlock avoidance algorithm that is
the Banker’s algorithm.

20
Deadlock Detection and Recovery
Deadlock Detection
• Detection of deadlocks is the most practical policy, which being both
liberal and cost efficient most operating systems deploy.
• To detect a deadlock, we must go about in a recursive manner and
simulate the most favoured execution of each unblocked process:
(a) An unblocked process may acquire all the needed resources and will
execute;
(b) It will then release all the acquired resources and remain dormant
thereafter;
(c) The now released resources may wake up some previously blocked
process;
(d) Continue the above steps as long as possible; and
(e) If any blocked processes remain, they are deadlocked.

21
Deadlock Detection and Recovery
RECOVERY FROM DEADLOCK
• There are various options for breaking a deadlock.

1. Recovery by Process Termination


• Abort all deadlocked processes: This method will break the deadlock
cycle clearly by terminating all process.
• This method is cost effective.
• Abort one process at a time until the deadlock cycle is eliminated: This
method terminates one process at a time, and invokes a deadlock-
detection algorithm to determine whether any processes are still
deadlocked.

22
Deadlock Detection and Recovery
RECOVERY FROM DEADLOCK
2. Recovery by Resource Pre-emption
 the operator or system pre-empts some resources from processes and
give these resources to other processes until the deadlock cycle is
broken.
 If pre-emption is required to deal with deadlocks, then three issues
need to be addressed:
• Selecting a victim: The system or operator selects which resources and
which processes are to be pre-empted based on cost factor.
• Rollback: The system or operator must roll back the process to some safe
state and restart it from that state.
• Starvation: The system or operator should ensure that resources will not
always be pre-empted from the same process?

23
Ostrich Algorithm
• It is hoped that deadlock doesn’t happen
• In general, this is a reasonable strategy.
• Deadlock is unlikely to occur very often; a system can run for years
without deadlock occurring.

24
ACTIVITY
1. What is the purpose of deadlock?
2. What is the different between process nodes and resource nodes ?
3. What is a deadlock and what are the four conditions that will create
the deadlock situation?
4. How can deadlock be avoided? Explain with the help of an example.

25
End

26

You might also like