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

Deadlock

Deadlock

Uploaded by

Mariya Shibi
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 views13 pages

Deadlock

Deadlock

Uploaded by

Mariya Shibi
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/ 13

PREVENTION

& AVOIDANCE
OF DEAD LOCKS IN AN OS
INTRODUCTION TO
DEADLOCKS

Definition: A deadlock is a situation in an


operating system where a set of processes
are blocked because each process is holding
a resource and waiting for another resource
held by another process.
CONDITIONS
Mutual Exclusion: At least one resource must be
held in a non-shareable mode.
Hold and Wait: A process holding at least one
resource is waiting to acquire additional resources

FOR DEADLOCK
held by other processes.
No Preemption: Resources cannot be forcibly
removed from the processes holding them.
Circular Wait: A set of processes are waiting for
each other in a circular chain.
DEADLOCK
PREVENTION TECHNIQUES
Eliminate Mutual Exclusion:
allowing multiple processes to access the same resource
simultaneously .
Example: Use read-only files for multiple readers.
Eliminate Hold and Wait:
Ensure processes request all the required resources at once.
Example: Two-phase locking protocol.
Eliminate No Preemption:
Allow preemption of resources.
Example: Rollback of processes if required.
Eliminate Circular Wait:
Impose an ordering on resource acquisition.
Example: Number resources and ensure processes request resources
in ascending order.
DEADLOCK AVOIDANCE
TECHNIQUES
Single instance of a resource type
Use a resource-allocation graph

Multiple instances of a resource type


Use the banker’s algorithm
Basic Facts

If a system is in safe state =>no deadlocks

If a system is in unsafe state => possibility of deadlock

A safe state means the system can run all the processes without getting stuck or running out of resources
Resource Allocation Graph
(RAG)
Nodes: Represent processes (P1, P2, ...)
and resources (R1, R2, ...).
Edges:
Request Edge (P → R): A process
requests a resource.
Assignment Edge (R → P): A resource
is allocated to a process.
Claim edge (Pi → Rj) indicated that
process Pj may request resource Rj;
represented by a dashed line.
Resource Allocation Graph
(RAG)
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
Banker's Algorithm
Multiple instances.

Each process must a priori claim maximum


use.

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.

Banker’s algorithm consists of a Safety algorithm and a Resource request algorithm.


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
1) Let Work and Finish be vectors of length
‘m’ and ‘n’ respectively.
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
2) Find an i such that both
a) Finish[i] = false
b) Needi <= Work
if no such i exists goto step (4)
3) Work = Work + Allocation[i]
Finish[i] = true
goto step (2)
4) if Finish [i] = true for all i
then the system is in a safe state
Resource-Request Algorithm
1) If Requesti <= Needi
Goto step (2) ; otherwise, raise an error condition, since the
process has exceeded its maximum claim.
2) If Requesti <= Available
Goto step (3); otherwise, Pi must wait, since the resources are
not available.
3) Have the system pretend to have allocated the requested
resources to process Pi by modifying the state as
follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi– Requesti;
If safe =>the resources are allocated to Pi
If unsafe =>Pi must wait, and the old resource-allocation state
is restored
THANK YOU!

You might also like