0% found this document useful (0 votes)
3 views

Deadlock Notes-Os

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Deadlock Notes-Os

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Deadlocks(w.r.

t resource mgt)
Deadlock is a situation where a set of processes are blocked
because each process is holding a resource and waiting for
another resource acquired by some other process. Deadlock is a
situation which involves the interaction of more than one resources
and processes with each other.

OR

Deadlock is the situation in which two processes are each waiting


for the other in order to complete the present process before going
to the other one. The result is that both the processes are hanging.
The situation of Deadlock generally occurred in multitasking and
client-server environment. A deadlock is also called a deadly
embrace. When the process is unable to change its state
indefinitely because the resources requested by it are being used
by another waiting process, then the system in a deadlock state.

Example of Deadlock

 A real-world example would be traffic, which is going only in


one direction.
 Here, a bridge is considered a resource.
 So, when Deadlock happens, it can be easily resolved if one car
backs up (Pre-empt resources and rollback).
 Several cars may have to be backed up if a deadlock situation
occurs.
 So starvation is possible.
 Consider an example when two trains are coming toward each
other on same track and there is only one track, none of the trains
can move once they are in front of each other.

 We can visualise the occurrence of deadlock as a situation where


there are two people on a staircase. One is ascending the
staircase while the other is descending. The staircase is so narrow
that it can only fit one person at a time. As a result, one has to wait
while the others move on and uses the staircase. Once that person
is finished, the other one can use that staircase. But here, none of
the people is willing to wait for the each other. None of them is
able to use the staircase. The person here is the process and the
staircase is the resource.
 Bank example: when we go to open bank account, manager says
first deposit minimum amount then you are allowed to open an
account but we request that I will first open an account then I will
deposit the money. In this case both are waiting for each other.
Nobody is moving back
Similar situation occurs in operating systems when there are two or
more processes hold some resources and wait for resources held by
other(s). For example, in the below diagram, Process 1 is holding
Resource 1 and waiting for resource 2 which is acquired by
process 2, and process 2 is waiting for resource 1.

A process in operating systems uses different resources and uses


resources in following way.
1) Requests a resource
2) Use the resource
3) Releases the resource
V.IMPT : There are 4 conditions necessary for the occurrence of a
deadlock.
1. Mutual Exclusion:
In this condition, at least two resources are must be non-sharable
and only one process can use the resource any given instant of time.
2. Hold and Wait:
This is also known as Resource holding, a process in which at least
one resource is in holding state and can request more resources
which are being held by other processes This is the next necessary
condition for the deadlock.
A process is holding at least one resource and waiting for resources.
3. No Pre-emption (resource):
In this condition the other process is not able to request the
resources until the previous process is not complete. These
resources must be released by the holding process voluntarily. It is
third condition for deadlock.
A resource cannot be taken from a process unless the process
releases the resource.
4. Circular Wait:
This is the last but not the least condition in which one process waits
for the resource held up by other process which is waiting for the
next process to release the resources. A set of processes are
waiting for each other in circular form.
These are the four necessary conditions for Deadlock which is
named as Coffman conditions. These conditions are enough to
preclude a deadlock from occurring.

Note:
All the 4 conditions are necessary for the deadlock to occur. If
anyone is prevented or resolved, the deadlock is resolved.
Methods for handling deadlock
There are four ways to handle deadlock

Most of the operating systems are not able to prevent the deadlocks to
occur. When the condition of deadlock occurs then different operating
systems respond to them in different and non-standard way. Most
approaches work by preventing one of the four Coffman conditions from
occurring, especially for the Current Wait. Major approaches are
1) Ignoring Deadlock : This approach assumed that a deadlock will
never occur which is one of the most important application of
Ostrich algorithm. The Ignoring deadlock is commonly used by
MINIX and UNIX. When the time interval between the occurrences of
deadlocks are seems to be large in addition to the increment in data
loss incurred each time is tolerable then in such case Ignoring
deadlock is used.
2) Deadlock prevention :
3) Detection and recovery :In this approach, deadlocks are allowed
to occur. Then the state of the system is examined to detect that a
deadlock has occurred and subsequently it is corrected. In this case,
algorithm is employed that tracks resource allocation and process states,
it rolls back and restarts one or more of the processes in order to remove
the detected deadlock.

4.Deadlock Avoidance
Deadlock can be avoided if some of the information about the processes
is well known by the Operating System before the allocation of resources
starts. For every resource request, the system sees whether granting the
request will mean that the system will enter an unsafe state, meaning a
state that could result in deadlock. The system then only grants requests
that will lead to safe states.

Deadlock Characterization: Deadlock Detection:


Resource Allocation Graph Syntax
A resource allocation graph contains a set of vertices V and a set of
edges E.

V is partitioned into two types:

 P = {P1, P2, ..., Pn} is the set of all processes.


 R = {R1, R2, ..., Rm} is the set of all resources.

A request is represented by a directed edge from Pi to Rj.


An assignment is represented by a directed edge from Rj to Pi.

 resource type with four instances:

 Pi requests an instance of Rj

 Pi is holding an instance of Rj
 If a resource-allocation graph contains no cycles, then the system
is not deadlocked. (When looking for cycles, remember that these
are directed graphs)
 If a resource-allocation graph does contain cycles AND each
resource category contains only a single instance, then a deadlock
exists.
 If a resource category contains more than one instance, then the
presence of a cycle in the resource-allocation graph indicates
the possibility of a deadlock, but does not guarantee one.
Resource allocation graph with a deadlock

Resource allocation graph with a cycle but no deadlock


Deadlock ignorance (Ostrich method)

Deadlock Prevention

Deadlocks can be prevented by preventing at least one of the four

such as read-only files do not lead to deadlocks.

 Unfortunately some resources, such as printers and tape drives,


require exclusive access by a single process.
No Hold and Wait

 To prevent this condition processes must be prevented from


holding one or more resources while simultaneously waiting for
one or more others. There are several possibilities for this:
o Require that all processes request all resources at one time.
This can be wasteful of system resources if a process needs
one resource early in its execution and doesn't need some
other resource until much later.
o Require that processes holding resources must release them
before requesting new resources, and then re-acquire the
released resources along with the new ones in a single new
request. This can be a problem if a process has partially
completed an operation using a resource and then fails to
get it re-allocated after releasing it.
o Either of the methods described above can lead to starvation
if a process requires one or more popular resources.

Preemption

 Preemption of process resource allocations can prevent this


condition of deadlocks, when it is possible.
o One approach is that if a process is forced to wait when
requesting a new resource, then all other resources
previously held by this process are implicitly released,
( preempted ), forcing this process to re-acquire the old
resources along with the new resources in a single request,
similar to the previous discussion.
o Another approach is that when a resource is requested and
not available, then the system looks to see what other
processes currently have those resources and are
themselves blocked waiting for some other resource. If such
a process is found, then some of their resources may get
preempted and added to the list of resources for which the
process is waiting.
o Either of these approaches may be applicable for resources
whose states are easily saved and restored, such as
registers and memory, but are generally not applicable to
other devices such as printers and tape drives.
No Circular Wait

 One way to avoid circular wait is to number all resources, and to


require that processes request resources only in strictly increasing
(or decreasing ) order.
 In other words, in order to request resource Rj, a process must first
release all Ri such that i >= j.
 One big challenge in this scheme is determining the relative
ordering of the different resources

R1 R2 R3, R4, R5….

P1 ---- R3, R5

Deadlock Avoidance

 The general idea behind deadlock avoidance is to prevent


deadlocks from ever happening, by preventing at least one of the
aforementioned conditions.
 This requires more information about each process, AND tends to
lead to low device utilization. (i.e. it is a conservative approach.)
 In some algorithms the scheduler only needs to know
the maximum number of each resource that a process might use.
In more complex algorithms the scheduler can also take
advantage of the schedule of exactly what resources may be
needed in what order.
 When a scheduler sees that starting a process or granting
resource requests may lead to future deadlocks, then that process
is just not started or the request is not granted.
 A resource allocation state is defined by the number of available
and allocated resources and the maximum requirements of all
processes in the system.

Safe State

 A state is safe if the system can allocate all resources requested


by all processes (up to their stated maximums) without entering a
deadlock state.
 More formally, a state is safe if there exists a safe sequence of
processes { P0, P1, P2, ..., PN } such that all of the resource
requests for Pi can be granted using the resources currently
allocated to Pi and all processes Pj where j < i. ( i.e. if all the
processes prior to Pi finish and free up their resources, then Pi will
be able to finish also, using the resources that they have freed up)
 If a safe sequence does not exist, then the system is in an unsafe
state, which MAY lead to deadlock. (All safe states are deadlock
free, but not all unsafe states lead to deadlocks.)

Safe, unsafe, and deadlocked state spaces.

Two deadlock avoidance algorithms:

 Resource-allocation graph algorithm (single instance)


 V impt ----Banker's algorithm
Resource-allocation graph algorithm

 only applicable when we only have 1 instance of each resource


type
 claim edge (dotted edge), like a future request
edge
 when a process requests a resource, the claim edge is converted
to a request edge
 when a process releases a resource, the assignment edge is
converted to a claim edge
Resource allocation graph for deadlock avoidance

The resulting RAG would have a cycle in it so the request cannot


be granted.

An unsafe state in a resource allocation graph

Banker's Algorithm

 For resource categories that contain more than one instance the
resource-allocation graph method does not work, and more
complex (and less efficient) methods must be chosen.
 The Banker's Algorithm gets its name because it is a method that
bankers could use to assure that when they lend out resources
they will still be able to satisfy all their clients. (A banker won't loan
out a little money to start building a house unless they are assured
that they will later be able to loan out the rest of the money to finish
the house.)
 When a process starts up, it must state in advance the maximum
allocation of resources it may request, up to the amount available
on the system.
 When a request is made, the scheduler determines whether
granting the request would leave the system in a safe state. If not,
then the process must wait until the request can be granted safely

Bankers Algorithm is futuristic approach and has two parts:

1. Safety Algorithm
2. Resource Request algorithm

QUESTION ON BANKERS ALGORITHM

Consider a system with 5 processes (P0 ... P4) and 3 resources types

A=10 B=5 C=7

Resource- allocation state at time t0: i)Need=MAX-ALLOCATION

Process Allocation Max Need Available


A B C A B C A B C A B C

P0 0 1 0 7 5 3 7 4 3 3 3 2

P1 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1

New P1 New available


2,1,1 1,1,1
for part(ii) 3,2,1

ii) Is the system in a safe state? If so, which sequence satisfies the
safety criteria?
Go in increasing order and check NEED matrix which process can be
granted the resource

Available 3 3 2
+2 0 0 (P1 is the first process to be granted its
resources and after its work is over it will release
its allocated resources and that would be added
in available)
= 5 3 2 ----New available
+ 2 1 1 (P3 would be granted)
= 7 4 3 ----New Available
+0 0 2 (P4 would be granted and it will release its
allocated resources)
= 7 4 5 ----New Available
+0 1 0 (P0 would be granted and it will release its
allocated resources)
=7 5 5 ----New Available
+3 0 2 (P2 would be granted and it will release its
allocated resources)
= 10 5 7 (total available from which we started)

Safe sequence :< P1, P3, P4, P0, P2 >

System is deadlock free.

iii) If P1 comes with the request A=0, B=1, C=1then that request would
be granted or not? If yes then tell whether the system will go into
deadlock or not?

We know that no process can request more than its NEED matrix

In Resource request algorithm, if the request comes it is not granted


immediately coz Bankers algorithm is futuristic approach where system would
see if the request is granted then the system should not go into deadlock state
in future.

Step 1: check if request <= need of that process and request <= available

So in above case 0 1 1 <= 1 2 2 of P1 and

0 1 1 <= 3 3 2(available)
That means it is ok to give and fulfil the request but still the system won’t give
the resources immediately. System will pretend as if it has given or granted the
resource and would check and apply banker’s algorithm’s safety algorithm and
see if safe state arises or not

Step 2: So in above case the updation would occur in NEED matrix of P1 as well
as Available resources and Allocation because if request of 0 1 1 is granted the
NEED of P1 would be 1 2 2 – 0 1 1= 1 1 1 and Available would be 3 3 2 – 0 1 1 =
3 2 1 and Allocation = 2 0 0 + 0 1 1 = 2 1 1

Step 3: Apply the safety algorithm again and check if any safe sequence is
made or not if there is a safe sequence then the request can be immediately
granted otherwise not.

Deadlock Detection and Recovery Techniques:

 Allow the system to go into deadlock state


 Apply detection algorithm after every interval of time
 Apply Recovery technique.

Deadlock Detection Algorithms: Two types:

i) draw wait for Graph from Resource allocation graph (for


single instance of resources)
ii) For multiple instances we have Banker’s Algorithm

If cycle is there in wait for graph then it is a deadlock


Recovery from Deadlock

 There are two basic approaches to recovery from deadlock:


1. Terminate one or more processes involved in the deadlock
2. Pre-empt resources.

Process Termination

 Two basic approaches, both of which recover resources allocated


to terminated processes:
o Terminate all processes involved in the deadlock. This
definitely solves the deadlock, but at the expense of
terminating more processes than would be absolutely
necessary.
o Terminate processes one by one until the deadlock is
broken. This is more conservative, but requires doing
deadlock detection after each step.
 In the latter case there are many factors that can go into deciding
which processes to terminate next:

1. Process priorities.
2. How long the process has been running, and how close it is
to finishing.
3. How many and what type of resources is the process
holding. (Are they easy to preempt and restore?)
4. How many more resources does the process need to
complete.
5. How many processes will need to be terminated
6. Whether the process is interactive or batch.
7. (Whether or not the process has made non-restorable
changes to any resource.)

Resource Pre-emption

 When pre-empting resources to relieve deadlock, there are three


important issues to be addressed:
1. Selecting a victim - Deciding which resources to preempt
from which processes involves many of the same decision
criteria outlined above.
2. Rollback - Ideally one would like to roll back a preempted
process to a safe state prior to the point at which that
resource was originally allocated to the process.
Unfortunately it can be difficult or impossible to determine
what such a safe state is, and so the only safe rollback is to
roll back all the way back to the beginning. (i.e. abort the
process and make it start over. )
3. Starvation - How do you guarantee that a process won't
starve because its resources are constantly being
preempted? One option would be to use a priority system,
and increase the priority of a process every time its
resources get preempted. Eventually it should get a high
enough priority that it won't get preempted any more.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy