classppt deadlock

34
DEADLOCKS

Upload: ankur-agrawal

Post on 08-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 1/34

DEADLOCKS

Page 2: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 2/34

The Deadlock Problem

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

#Deadlock

 A waiting process is never again able to change state, because the resources it

requested are held by other waiting processes

Page 3: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 3/34

A system consists of a finite number of resources to be distributed among a

number of competing processes

->Resource types R1, R2, . . ., Rm(CPU cycles, memory space, files, and I/O devices

(printers & DVD drivers))

->Each resource type Ri has W i instances

-If a process requests an instance of a resource type, the allocation of any

instance of the type will satisfy the request

-if it will not, then the instances are not identical

Page 4: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 4/34

Each process utilizes a resource as follows:

=>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

Page 5: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 5/34

Deadlock Characterization

Deadlock can arise if four conditions hold simultaneously in a system

Mutual exclusion

=>At least one resource must be held in a non-sharable mode

 Means, only one process at a time can use a resource

 If another process requests that resource. The request process must be delayed

until the resource has been released

Hold and wait

A process holding at least one resource is waiting to acquire additional resources held

by other processes

No preemption

=>Resources cannot preempted

- A resource can be released only voluntarily by the process holding it, after that

process has completed its task

Page 6: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 6/34

Circular wait

=>There exists a set {P0, P1, , Pn} of waiting processes

  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

  Pn is waiting for a resource that is held by P0

Page 7: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 7/34

Resource-Allocation Graph

Deadlocks can be described in terms of a directed graph called a system

resource-allocation graph

A set of vertices V 

V is partitioned into two types:

 P= {P1, P2, , Pn}, the set consisting of all the active processes in the system

 R= {R1, R2, , Rm}, the set consisting of all resource types in the system

A set of edges E 

=>Request edge

 A directed edge Pi  Rj. Process Pi  has requested an instance of resource typeRj and is currently waiting for that resource

=>Assignment edge

 A directed edge Rj   Pi. An instance of resource type Rj has been allocated to

process Pi 

Page 8: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 8/34

Process is a circle, resource type is square; dots represent number of instances of 

resource in type.

Page 9: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 9/34

Page 10: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 10/34

Resource Allocation Graph With A Deadlock

Page 11: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 11/34

Resource Allocation Graph With A Cycle But No Deadlock

Page 12: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 12/34

HOW TO HANDLE DEADLOCKS GENERAL STRATEGIES

There are three methods:

1. Ignore Deadlocks:Most Operating systems do this. Including UNIX & Windows

2. Ensure deadlock never occurs using either

Prevention -Prevent any one of the 4 conditions from happening.

Deadlock avoidance -Its requires that the operating system be given in advance

additional information concerning which resources a process will request and use

during its lifetime. With this additional knowledge, it can decide for each request

whether or not the process should wait. To decide whether the current request can be

satisfied or must be delayed, the system must consider the resources currently

available, the resources currently allocated to each process, and the future requests

and releases of each process.

3. Allow deadlock to happen.

This requires using

Detection Know a deadlock has occurred.

Recovery Regain the resources.

Page 13: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 13/34

Deadlock Prevention

For a deadlock to occur, each of the four necessary conditions must hold

By ensuring that at least one of these conditions cannot hold, we can prevent the

occurrence of a deadlock

Mutual Exclusion

Must hold for non-sharable resources

 Printer cannot be shared by several processes

Not required for sharable resources

 Read-only files are sharable resources

We cannot prevent deadlocks by denying the mutual-exclusion.Because some

resources are non-sharable .

Page 14: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 14/34

Hold and Wait

Must guarantee that whenever a process requests a resource, it does not hold any

other resources

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

execution

2.Allow process to request resources only when the process has none

Both these protocols have two main disadvantages.

First, resource utilization may be low, since resources may be allocated but unused

for a long period.

Second, starvation is possible. A process that needs several popular resources may

have to wait indefinitely, because at least one of the resources that it needs is

always allocated to some other process.

Page 15: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 15/34

No Preemption The third necessary condition for deadlocks is that there be no

preemption of resources that have already been allocated. To ensure that this

condition does not hold, we can use the following protocol.

If a process that is holding some resources requests another resource that cannot beimmediately allocated to it, then all resources currently being held are preempted.

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

Alternatively, if a process requests some resources ,If they are available, we

allocate them, If they are not, we check to whether they are allocated to some

other process that is waiting for additional resources

  If so, we preempt the desired resources from the waiting process and allocate them

to the requesting process  If the resources are neither available nor held by a waiting process, the request

process must wait

A process can be restarted only when it is allocated the new resources it is

requesting and recovers any resources that were preempted while it was waiting.

Page 16: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 16/34

Circular Wait

Impose a total ordering of all resource types, and require that each process requests

resources in an increasing order of enumeration

--LetR = {R1, R2, , Rm} be the set of resource types-- Define a one-to-one function F: R N, where N is the set of nature numbers

F(tape driver) = 1

F(disk driver) = 5

F(printer) = 12

Each process can request resources only in an increasing order of enumeration

A process can initially request any number of instances of a resource type Ri

--After that, the process can request instances of resource type Rj if and only if 

F(Rj) > F(Ri)

  If several instances of the same resource type are needed, a single request for allof them must be issued

Alternatively, we can require that, whenever a process requests an instance of 

resource type Rj , it has released any resources Ri such that F(Ri) >= F(Rj)

Page 17: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 17/34

With deadlock avoidence a dicision is made dynamically ether the current resource

allocation request will ,if grantted,potentially lead to a dead lock.dead lock avoidence

thus requires knowledge of future process resource request

Page 18: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 18/34

Two Approaches to Deadlock Avoidance

1. Do not start a process if its demands might lead to deadlock

2. Do not grant an incremental resource request to a process if this allocation might lead to

deadlock

Process Initiation Denial:

Do not start a process if its initiation leads to a possible deadlock,

Consider a system of n process and m different type of resources. Let us define following

matrices

Page 19: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 19/34

The matrix claim gives the maximum requirement of each process for each resource

,with one row dedicated to each process .This information must be declared in

advance by a process for deadlock avoidance to work. Similarly, The matrix Allocation

gives the current allocation to each process. The following relationships hold

Page 20: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 20/34

Resource Allocation Denial

This technique is also known as bankers algorithm.

State: Represents the current allocation of resources to a process, which includes

resource and available vectors, two matrices representing claim and allocation.

Safe State: Represents a state in which atleast one sequence exist which cannot lead

to a deadlock.

Unsafe State: Obviously, the reverse of the safe state.

Page 21: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 21/34

Page 22: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 22/34

Page 23: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 23/34

Fig:6.7-a

Fig:6.7-b

Fig:6.7-c

Page 24: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 24/34

Fig:6.7-d

Page 25: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 25/34

Page 26: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 26/34

Page 27: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 27/34

Page 28: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 28/34

Page 29: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 29/34

Page 30: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 30/34

Page 31: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 31/34

Page 32: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 32/34

Page 33: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 33/34

Page 34: Classppt Deadlock

8/7/2019 Classppt Deadlock

http://slidepdf.com/reader/full/classppt-deadlock 34/34