0% found this document useful (0 votes)
9 views16 pages

Unit 3 Part 2

Unit3 mp

Uploaded by

Varun Tyagi
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)
9 views16 pages

Unit 3 Part 2

Unit3 mp

Uploaded by

Varun Tyagi
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/ 16

Operating System 1 (ECS-501)

CSE- 3rd Year

Operating System. Unit 3(2nd Part)

Notes on Deadlock
1.4 Deadlock: A set of blocked processes each holding a resource and waiting to acquire a
resource held by another process in the set.

OR

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

Example
System has 2 process P1 & P2 and 2 tape ddrives A &B .P1 and P2 each hold one tape
drive and each needs another one.P1 requests tape drive 1 & gets it . P2 requests tape
drive 2 & gets it.P1
P1 requests tape drive 2 but is blocked
blocked.P2
P2 requests tape drive 1 but is
blocked.

Prepared By: Lalit Saraswat RKGIT


Operating System 2 (ECS-501)

1.4.2 System Model:


A system consists of a finite number of resources to be distributed among a number of
competing processes. The resources are partitioned into several types, each consisting of some
number of identical instances. Resources are like Memory space, CPU cycles, files, and I/O
devices (such as printers and DVD drives).
If a system has two CPUs, then the resource type CPU has two instances. Similarly, the
resource type printer may have five instances.
 Resource types R1, R2, . . ., Rm ( CPU cycles, memory space, I/O devices)
 Each resource type Ri has Wi instances.
 Each process utilizes a resource as follows:
i. 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.
ii. Use: The process can operate on the resource (for example, if the resource is a
printer, the process can print on the printer).
iii. Release: The process releases the resource.
1.4.3 Deadlock Characterization: Deadlock can arise if four conditions hold simultaneously.
i. Mutual exclusion: only one process at a time can use a resource.
ii. Hold and wait: a process holding at least one resource is waiting to acquire additional
resources held by other processes.
iii. No preemption: a resource can be released only
voluntarily by the process holding it, after that
process has completed its task.
iv. Circular wait: 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.

Prepared By: Lalit Saraswat RKGIT


Operating System 3 (ECS-501)

1.4.4 Resource-Allocation Graph:


Deadlocks can be described more precisely in terms of a directed graph called a system resource-
allocation graph.
A set of vertices V and a set of edges E.
 V is partitioned into two types:
i. P = {P1, P2, …, Pn}, the set consisting of all the processes in the system.
ii. R = {R1, R2, …, Rm}, the set consisting of all resource types in the system.
 request edge – directed edge P1  Rj
 assignment edge – directed edge Rj  Pi

Example of a Resource Allocation Graph Resource Allocation Graph With A Deadlock

Prepared By: Lalit Saraswat RKGIT


Operating System 4 (ECS-501)

Resource Allocation Graph with a cycle but Without A Deadlock

If graph contains no cycles  no deadlock.


If graph contains a cycle 
if only one instance per resource type, then deadlock.
if several instances per resource type, possibility of deadlock.

Methods for Handling Deadlocks:


 Ensure that the system will never enter a deadlock state using Deadlock
Prevention and Deadlock Avoidance.

 Allow the system to enter a deadlock state and then recover.

1.4.5 Deadlock Prevention: Deadlock Prevention provides a set of methods for ensuring that at least
one of the necessary conditions cannot hold.

i. Mutual Exclusion – not required for sharable resources; must hold for non-sharable
resources.
ii. 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;

Prepared By: Lalit Saraswat RKGIT


Operating System 5 (ECS-501)

iii. No 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.
iv. Circular Wait – imposes a total ordering of all resource types, and requires that each
process requests resources in an increasing order of enumeration.

1.4.6 Deadlock Avoidance: Requires that the system has some additional a priori information
available.
 Simplest and most useful model requires that each process declare the maximum number
of resources of each type that it may need.
 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.

A. Safe State:
 A state is safe if the system can allocate resources to each process in some order and still
avoid a deadlock.
 Formally , a System is in safe state if there exists a safe sequence <P1, P2, …, Pn> of all
the processes is the system such that for each Pi, 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:
i. If Pi resource needs are not immediately available, then Pi can wait until all Pj
have finished.
ii. When Pj is finished, Pi can obtain needed resources, execute, return allocated
resources, and terminate.

Prepared By: Lalit Saraswat RKGIT


Operating System 6 (ECS-501)

iii. When Pi terminates, Pi +1 can obtain its needed resources, and so on.
 If a system is in safe state  no deadlocks.
 If a system is in unsafe state  possibility of deadlock.
 Avoidance  ensure that a system will never enter an unsafe state.

Example of safe state :

B. Deadlock Avoidance Algorithm


A. Single instance of a resource type: Use a resource-allocation graph
B. Multiple instances of a resource type: Use the banker’s algorithm

A. Resource-Allocation Graph ( Single instance of a resource type) :

Prepared By: Lalit Saraswat RKGIT


Operating System 7 (ECS-501)

 We can make use of resource allocation graph for deadlock avoidance.


 In addition to request and assignment edges, there is also a new type of edge called
“claim edge”.
 Claim edge Pi  Rj indicated that process Pj may request resource Rj at some time in the
future.; represented by a dashed line.
 Claim edge converts to request edge when a process requests a resource.
 Request edge 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.
 Resources must be claimed a priori in the system.

Resource-Allocation Graph Unsafe State In Resource-Allocation Graph

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

Prepared By: Lalit Saraswat RKGIT


Operating System 8 (ECS-501)

Banker’s Algorithm for resource types with multiple instances:

The resource-allocation-graph algorithm is not applicable to a resource allocation system with


multiple instances of each resource type. The name was chosen because the algorithm could be
used in a banking system to ensure that the bank never allocated its available cash in such a way
that it could no longer satisfy the needs of all its customers.

Several data structures must be maintained to implement the banker's algorithm. These data structures
encode the state of the resource-allocation system. Let n be the number of processes in the system and m
be the number of resource types.

Data Structures for the Banker’s Algorithm

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.

Need [i,j] = Max[i,j] – Allocation [i,j].

Safety Algorithm: We can now present the algorithm for finding out whether or not a system
is in a safe state.
1. Let Work and Finish be vectors of length m and n, respectively.

Initialize: Work = Available Finish [i] = false for i ,1,2, …, n.


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.

Resource-Request Algorithm for Process Pi :

Let Request = request vector for process Pi. If Requesti [j] = k then process Pi wants k instances
of resource type Rj.

Prepared By: Lalit Saraswat RKGIT


Operating System 9 (ECS-501)

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 = Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;;
4. If safe  the resources are allocated to Pi.
• If unsafe  Pi must wait, and the old resource-allocation state is restored

Example :
Consider a system with five processes P0 through P4 and three resource types A, B, and C.
Resource type A has 10 instances, resource type B has 5 instances, and resource type C has 7
instances. Suppose that, at time To, the following snapshot of the system has been taken:

Prepared By: Lalit Saraswat RKGIT


Operating System 10 (ECS-501)

Step by step solution of banker’s algorithm:

Safety algorithm :

For process P0,


Need(P0)<=available
7 4 3 <= 3 3 2 (Not Possible)

For process P1 Need(P1)<=available


1 2 2 <= 3 3 2(Possible)

Sequence<P1>
Avaliable =available +allocation(P1)
=3 3 2 + 2 0 0
=5 3 2

Prepared By: Lalit Saraswat RKGIT


Operating System 11 (ECS-501)

For process P2
Need(P2)<=available
6 0 0 <= 5 3 2 (Not Possible)

For process P3
Need(P3)<=available
0 1 1 <= 5 3 2(Possible)
1
Sequence <P1,P3>
Avaliable =available +allocation(P3)
=5 3 2 +2 1 1
=7 4 3

For process P4

Need(P4)<=available
4 3 1<= 7 4 3(Possible)

Sequence<P1,P3,P4>

Avaliable =available +allocation(P4)


=7 4 3 + 0 0 2
=7 4 5

For process P0 Need(P0)<=available


7 4 3<= 7 4 5(Possible)

Sequence<P1,P3,P4,P0>

Avaliable =available +allocation(P0)


=7 4 5 + 0 1 0
=7 5 5

For process P2 Need(P2)<=available


6 0 0 <= 7 5 5(Possible)

Sequence<P1,P3,P4,P0,P2>

Avaliable =available +allocation(P2)


=7 5 5 + 3 0 2
= 10 5 7

Safe Sequence is < P1,P3,P4,P0,P2>

So the system is in Safe State .

Prepared By: Lalit Saraswat RKGIT


Operating System 12 (ECS-501)

Resource Request algorithm (step by step solution):

Refer class Notes

1.4.7 Deadlock Detection


In this environment, the system must provide:
• An algorithm that examines the state of the system to determine whether a deadlock has
occurred
• An algorithm to recover from the deadlock.

A. Single Instance of Each Resource Type

Resource-Allocation Graph Wait-for Graph

B. Multiple Instances of a Resource Type

Prepared By: Lalit Saraswat RKGIT


Operating System 13 (ECS-501)

i. Available: A vector of length m indicates the number of available resources of each type.
ii. Allocation: An n x m matrix defines the number of resources of each type currently
allocated to each process.
iii. 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 :

Prepared By: Lalit Saraswat RKGIT


Operating System 14 (ECS-501)

For step by step solution of this algorithm given above (refer class notes)

Prepared By: Lalit Saraswat RKGIT


Operating System 15 (ECS-501)

1.4.8 Recovery from Deadlock:


There are two possible ways to recover from deadlock.
A. Process Termination:

Prepared By: Lalit Saraswat RKGIT


Operating System 16 (ECS-501)

B. Resource Preemption:

Prepared By: Lalit Saraswat RKGIT

You might also like