Vous êtes sur la page 1sur 25

Deadlock

A
SEMINAR REPORT
Submitted by
Mahipal Singh
(Roll No:-1305020116)
In partial fulfilment for the award of the degree
Of
MASTER OF COMPUTER APPLICATION

2015

DEPARTMENT OF
INFORMATION TECHNOLOGY
`
CERTIFICATE
This is to certify that the Seminar titled Deadlock was prepared and
presented by Mahipal Singh Roll No. 1305020116 of V Semester in partial
fulfilment of requirement for the award of Degree of Masters in Computer
Application under Sikkim Manipal University Directorate of Distance
Education

SEMINAR GUIDE

Acknowledgement

The enduring of the work is the cumulative sequence of extensive guidance & arduous work.
We wish to acknowledge and express our personal gratitude to all those without whom this
work could not have been reality. I am greatly indebted to Mr. Navin Purohit & Mr. Richard
Kumar gave me an opportunity to work in such a professional & inspiring environment. I feel
very delighted to get this rare opportunity to show my profound senses of reverence &
indebtedness to our esteemed teachers to Mr.Navin Purohit & Mr. Richard Kumar for their
keen & sustained interest, valuable advice. For this type of consideration I behold there in a
special manner & no words can fully convey our regards for them. I want to acknowledge all
those who have directly or indirectly helped & cooperated in accomplishing this seminar.

Abstract

Deadlock has been widely studied in many fields of computer science, notably in
communications, database, and operating systems. Perhaps because (at least one state called)
deadlock is tractable, there exists extensive literature on the subject.
Deadlock was apparently decisively defined over thirty years ago, with its
characteristics and handlers. Yet, much of the literature remains inconsistent in its treatment
of this anomaly.
A more precise definition is clearly needed to distinguish between the different states
that are termed deadlock. A classification of dead states is required to distinguish the causes
and appropriate handlers for each.

Table of Contents

CHAPTER 1

INTRODUCTION

CHAPTER 2
CHAPTER 3
CHAPTER 4
CHAPTER 5
CHAPTER 6
REFERENCES

DEADLOCK CHARACTERIZATION
RESOURCE ALLOCATION GRAPH
METHODS OF HANDLING DEADLOCK
RECOVERY FROM DEADLOCK
CONCLUSION

1: INTRODUCTION

In a multiprogramming environment, several processes may compete for a finite number of


resources. A process requests resources; if the resources are not available at that time, the
process enters a waiting state. Sometimes, a waiting process is never again able to change
state, because the resources it has requested are held by other waiting processes. This
situation is called a deadlock.
Perhaps the best illustration of a deadlock can be drawn from a law passed by the Kansas
legislature early in the 20th century. It said, in part: "When two trains approach each other at
a crossing, both shall come to a full stop and neither shall start up again until the other has
gone."
Some applications can identify programs that may deadlock, operating systems typically do
not provide deadlock-prevention facilities, and it remains the responsibility of programmers
to ensure that they design deadlock-free programs. Deadlock problems can only become
more common, given current trends, including larger numbers of processes, multithreaded
programs, many more resources within a system, and an emphasis on long-lived file and
database servers rather than batch systems.

Dead-lock is a condition that may involve two or more processes in a state such that each is
waiting for release of a resource currently held by some other process.

Examples:

Two cars crossing a single-lane bridge from opposite directions.


6

A person going down a ladder while another person is climbing up the ladder.

Two trains travelling toward each other on the same track.

Deadlock can be defined as the permanent blocking of a set of processes that


either compete for system resources or communicate with each other. A set of processes is
deadlocked when each process in the set is blocked awaiting an event (typically the freeing
up of some requested resource) that can only be triggered by another blocked process in the
set. Deadlock is permanent because none of the events is ever triggered.

A process must request a resource before using it and must release the resource after using it.
A process may request as many resources as it requires to carry out its designated task.
Obviously, the number of resources requested may not exceed the total number of resources
available in the system. In other words, a process cannot request three printers if the system
has only two.
Under the normal mode of operation, a process may utilize a resource in only the following
sequence:
1. Request- If the request cannot be granted immediately (for example, if the resource is
being used by another process), then the requesting process must wait until it can acquire the
resource.
2. Use-The process can operate on the resource (for example, if the resource is a printer,
the process can print on the printer).
3. Release-The process releases the resource.

2. DEADLOCK CHARACTERIZATION
A deadlock situation can arise if the following four conditions hold simultaneously in a
system:

1. Mutual exclusion- At least one resource must be held in a non-sharable mode; that is,
only one process at a time can use the resource.
2. Hold and wait-A process must be holding at least one resource and waiting to acquire
additional resources that are currently being held by other processes.
3. No pre-emption-Resources cannot be pre-empted; that is, a resource can be released
only voluntarily by the process holding it, after that process has completed its task.
4.Circular wait- A set { P0 , P1, ... , Pn } of waiting processes must exist such that Po is
waiting for a resource held by P1, P1 is waiting for a resource held by P2, ... , Pn-1 is waiting
for a resource held by Pn and P0 is waiting for a resource held by Pn.

3. RESOURCE ALLOCATION GRAPH


Deadlocks can be described more precisely in terms of a directed graph called System
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 == { P1, P2, ... , Pn}, the
9

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 P-> Rj is called a request edge,


A directed edge R1 -> P; is called an assignment edge.

Without a deadlock

with a deadlock

4. METHODS OF HANDLING DEADLOCK


We can deal with the deadlock problem in one of three ways:

10

Prevention-We can use a protocol to prevent or avoid deadlocks, ensuring that the system will
never enter a deadlocked state.
Detection-We can allow the system to enter a deadlocked state, detect it, and recover.

Avoidance-We can ignore the problem altogether and pretend that deadlocks never occur in
the system.

Deadlock prevention
1. Mutual Exclusion not required for sharable resources; must hold for non-sharable

resources

2. Hold and Wait must guarantee that whenever a process requests a resource, it does
not hold any other resources

Require process to request and be allocated all its resources before it


begins execution, or allow process to request resources only when the
process has none
Low resource utilization; starvation possible

3. No Pre-emption

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

Pre-empted 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.

4. Circular Wait impose a total ordering of all resource types, and require that each process
Requests resources in an increasing order of enumeration.

Deadlock avoidance

Simplest and most useful model requires that each process declare the maximum number of
resources of each type that it may need
11

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

Safe state

When a process requests an available resource, system must decide if immediate allocation
leaves the system in a safe state

System is in safe state if there exists a sequence <P1, P2, , Pn> of ALL the processes
is the systems such that for each P i, the resources that Pi can still request can be
satisfied by currently available resources + 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

Customer

Used

Max need

Current need

5
12

Available units = 2
(Safe state)

Customer

Used

Max need

Current need

Available units = 1
(Unsafe state)
One process should releases resources at that time

Resource allocation graph in safe and unsafe state 1. Safe state

13

2. Unsafe state

Safe, unsafe and deadlocked state spaces:-

If a system is in safe state no deadlocks

If a system is in unsafe state possibility of deadlock


Avoidance sure that a system will never enter an unsafe state.

14

Resource allocation graph algorithms:Single instance of a resource type


-

Use a resource-allocation graph algorithm

Multiple instances of a resource type


-

Use the bankers algorithm

2. Resource allocation graph algorithm

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 the formation of a cycle in the resource allocation graph

3. bankers algorithm
This algorithm can be used in a banking system to ensure that the bank never allocates its
available cash such that it can no longer satisfy the needs of its customers.

Multiple instances
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
Bankers algorithm uses two tests whenever a process makes a request:

Feasibility test: a request is feasible if no. of requested units is less then no. of free units at that time.

Safety test: a request is safe if after granting the request, there exist at least one sequence of resource
allocation.

Data structures used for bankers algorithm


Let n=number of process and m=number of resources types
1. Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj
available
2. Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances of
resource type Rj
15

3. Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj


4. Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its
task
Need [i,j] = Max[i,j] Allocation [i,j]

Resource-request algorithm for process pi


Request = request vector for process Pi. If Requesti [j] = k then process Pi wants k instances
of resource type Rj
1. If Requesti Needi go to step 2. Otherwise, raise error condition, since process has
exceeded its maximum claim
2. If Requesti Available, go to step 3. Otherwise Pi must wait, since resources are not
available
3. Pretend to allocate requested resources to Pi by modifying the state as follows:
Available = Available Request;
Allocationi = Allocationi + Requesti;
Needi = Needi Requesti;
If safe
If unsafe

the resources are allocated to Pi

Pi must wait, and the old resource-allocation state is restored.

Example of bankers algorithm


5 processes P0 through P4; 3 resource types:
A (10 instances), B (5instances), and C (7 instances)
Snapshot at time T0:
Allocation

Max

Available
16

ABC

ABC

ABC

P0

010

753

332

P1

200

322

P2

302

902

P3

211

222

P4

002

433

The content of the matrix Need is defined to be Max Allocation


Need
ABC

P0

743

P1

122

P2

600

P3

011

P4

431

The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety
criteria

When p1 request(1,0,2)
Check that Request Available (that is, (1, 0, 2) (3, 3, 2) true

P0

Allocation

Need

Available

ABC

ABC

ABC

010

743

230
17

P1

302

020

P2

301

600

P3

211

011
4

P4

002

3
1

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

Deadlock detection
Allow system to enter deadlock state
Detection algorithm
Recovery scheme

Single instance of each resource type:-

Maintain wait-for graph


Nodes are processes
PiPj if Pi is waiting for Pj

Periodically invoke an algorithm that searches for a cycle in the graph. If there is a

cycle, there exists a deadlock


2

An algorithm to detect a cycle in a graph requires an order of n operations, where


n is the number of vertices in the graph

Resource allocation graph and wait for graph

Wait for graph (WAG)


can be obtained from
RAG
by
removing
resource nodes and
18

adding
the
appropriate edge.

Resource allocation graph

Wait for graph

Several instances of a resource type:1. Available: A vector of length m indicates the number of available resources
of each type.
2. Allocation: An n x m matrix defines the number of resources of each type
currently allocated to each process.
3. 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.

Detection algorithm
1. Let Work and Finish be vectors of length m and n,
respectively. Initialize Work= Available. For i= 0, 1, ...
n, if Allocation 0,
then Finish[i] =false; otherwise, Finish[i] = true.
2. Find an index i such that both
a. Finish[i] ==false
b. Request i Work
If no such i exists, go to step 4.

19

3. Work= Work+ Allocation;


Finish[i] = true, Go to
step 2.

4. If Finish[i] ==false for some i, 1 i n, then the system is in a deadlocked state. Moreover,
if
Finish[i] ==false, then process P; is deadlocked.
This algorithm requires an order of m x n2 operations to detect whether the system is in a
deadlocked state.
If detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph
and so we would not be able to tell which of the many deadlocked processes caused the
deadlock

Example of deadlock detection


Five

P4;
processes
P0
through
three
resource
A (7 instances), B (2 instances), and C (6 instances)

types

Snapshot at time T0:

P0
P1
P2

Allocation

Request

Available

ABC

ABC

ABC

010

000

000

200
303

202
000

P3

211

100

P4

002

002

Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i

P2 requests an additional instance of type C

20

Request
ABC
P0

000

P1

201

P2

001

P3

100

P4

002

State of system
o Can reclaim resources held by process P0, but insufficient
resources to fulfil other processes; requests
o Deadlock exists, consisting of processes P1, P2, P3, and
P4

5. RECOVERY FROM DEADLOCK


21

When a detection algorithm determines that a deadlock exists, several alternatives are
available. One possibility is to inform the operator that a deadlock has occurred and to let the
operator deal with the deadlock manually. Another possibility is to let the system recover
from the deadlock automatically. There are two options for breaking a deadlock one is simply
to abort one or more processes to break the circular wait. The other is to pre-empt some
resources from one or more of the deadlocked processes.
1. Process Termination-To eliminate deadlocks by aborting a process, we use one of two
methods. In both methods, the system reclaims all resources allocated to the terminated
processes.
Abort all deadlocked processes-This method clearly will break the deadlock cycle, but
at great expense; the deadlocked processes may have computed for a long time, and
the results of these partial computations must be discarded and probably will have to
be recomputed later.
Abort one process at a time until the deadlock cycle is eliminated. This method incurs
considerable overhead, since after each process is aborted, a deadlock-detection
algorithm must be invoked to determine whether any processes are still deadlocked.
In which order should we choose to abort?
o Priority of the process
o How long process has computed, and how much longer to completion
o Resources the process has used
o Resources process needs to complete
o how many processes will need to be terminated
o Is process interactive or batch?
2. Resource pre emption
To eliminate deadlocks using resource pre-emption, we successively pre-empt 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- Which resources and which processes are to be pre-empted? As in
process termination, we must determine the order of pre-emption to minimize cost.
Cost factors may include such parameters as the number of resources a deadlocked
process is holding and the amount of time the process has thus far consumed during
its execution.
22

Rollback- If we pre-empt a resource from a process, what should be done with that
process? Clearly, it cannot continue with its normal execution; it is missing some
needed resource. We must roll back the process to some safe state and restart it from
that state.
Since, in general, it is difficult to determine what a safe state is, the simplest
solution is a total rollback: abort the process and then restart it. Although it is more
effective to roll back the process only as far as necessary to break the deadlock, this
method requires the system to keep more information about the state of all running
processes.
Starvation- How do we ensure that starvation will not occur? That is, how can we
guarantee that resources will not always be pre-empted from the same process?

5. CONCLUSION
23

Deadlock is occurring due to non-preemptable resources.


We may be able to break a dead-lock by having a few additional copies of a
resource. When one copy is taken, there is always another copy of that
resource.
A deadlocked state occurs when two or more processes are waiting
indefinitely for an event that can be caused only by one of the waiting
processes.
A method for avoiding deadlocks, rather than preventing them, requires that
the operating system have a priori information about how each process will
utilize system resources.
Researchers have argued that none of the basic approaches alone is
appropriate for the entire spectrum of resource-allocation problems in
operating systems. The basic approaches can be combined, however,
allowing us to select an optimal approach for each class of resources in a
system.

REFERENCES
24

Silberschatz, Galvin, Gagne Operating System Concepts, 8th Edition


OS_William_Stalling_Sandy
http://codex.cs.yale.edu/avi/os-book/OS8/os8c/deadlock.html
http://highered.mcgraw-hill.com/sites/0070702039/student_view0/index.html

25

Vous aimerez peut-être aussi