0% found this document useful (0 votes)
5 views25 pages

Os - Unit Iv 1 10

The document discusses deadlocks in operating systems, detailing conditions that lead to deadlocks, such as mutual exclusion, hold and wait, no preemption, and circular wait. It explains methods for handling deadlocks, including prevention, avoidance, and detection, as well as the use of resource-allocation graphs and the Banker's algorithm for safe resource allocation. Additionally, it describes resource types, their characteristics, and the implications of deadlocks on system performance.

Uploaded by

kanusha4304
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)
5 views25 pages

Os - Unit Iv 1 10

The document discusses deadlocks in operating systems, detailing conditions that lead to deadlocks, such as mutual exclusion, hold and wait, no preemption, and circular wait. It explains methods for handling deadlocks, including prevention, avoidance, and detection, as well as the use of resource-allocation graphs and the Banker's algorithm for safe resource allocation. Additionally, it describes resource types, their characteristics, and the implications of deadlocks on system performance.

Uploaded by

kanusha4304
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/ 25

Deadlocks: Resources, Conditions for resource deadlocks, Ostrich algorithm, Deadlock detection and

recovery, Deadlock avoidance, Deadlock prevention.

Resource:
A resource can be a piece of hardware such as tape drive, disk drive ,printer etc. or a piece of information such as a file ,a record
within a file ,a shared variable ,a critical section etc.
Resources come in two types:
• Preemptable •
Nonpreemptable.
A Preemptable resource is one which can be allocated to a given process for a period of time, then be allocated to another
process and then be reallocated to the first process without any ill effects. Examples of preemptable resources include memory,
buffers, CPU, array processor, etc.
A Nonpreemptable resource cannot be taken from one process and given to another without side effects. One obvious
example is a printer: certainly we would not want to take the printer away from one process and give it to another in the middle
of a print job.
Under the normal mode of operation, a process may utilize a resource in only the following sequence:
• Request. The process requests the resource. 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.
• Use. The process can operate on the resource (for example, if the resource is a printer, the process can print on the printer).

• Release. The process releases the resource.

Deadlock:
• 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.

Deadlock Characterization:
In a deadlock, processes never finish executing, and system resources are tied up, preventing other jobs from starting.
• Necessary Conditions:
• A deadlock situation can arise if the following four conditions are true
• 1.Mutual Exclusion
• 2. Hold and wait
• 3.No preemption • 4. Circular wait.
• 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. If another process requests that resource, the requesting process must be delayed until the resource has
been released.
• No preemption : Resources cannot be preempted; that is, a resource can be released only voluntarily by the process
holding it, after that process has completed its task.
• 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.
• Circular wait: A set {P0, P1, ..., Pn} of waiting processes must exist such that P0 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 Pn is waiting for a resource held by P0.

If any condition fails there is no deadlock

UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 1


UNIT-3 OPERATING SYSTEMS
CSE-AIML, ACOE Page 2
Resource-Allocation Graph
Deadlocks can be described in terms of a directed graph called a 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 set consisting of all the active processes in the system R
= {R1, R2, ..., Rm}, the set consisting of all resource types in the system.
Request Edge
A directed edge from process Pi to resource type Rj is denoted by Pi → Rj ; it signifies that process Pi has requested
an instance of resource type Rj and is currently waiting for that resource.
Assignment Edge:
A directed edge from resource type Rj to process Pi is denoted by Rj → Pi ; it signifies that an instance of resource
type Rj has been allocated to process Pi .

→Process are represented using circle

→Resources are denoted using rectangle. Since resource type Rj may have more than one instance, we
represent each such instance as a dot within the rectangle.
The sets P, R, and E: ◦ P = {P1, P2, P3}
◦ R = {R1, R2, R3, R4}
◦ E = {P1 → R1, P2 → R3, R1 → P2, R2 → P2, R2 → P1, R3 → P3} Resource instances:
◦ One instance of resource type R1
◦ Two instances of resource type R2
◦ One instance of resource type R3 ◦ Three instances of resource type R4 Process states:
◦ Process P1 is holding an instance of resource type R2 and is waiting for an instance of
resource type R1.
◦ Process P2 is holding an instance of R1 and an instance of R2 and is waiting for
an instance of R3. Resource Allocation Graph
◦ Process P3 is holding an instance of R3.

• If the graph contains no cycles, then no process in the system is


deadlocked.

• If the graph does contain a cycle, then a deadlock may exist.

• At this point, two minimal cycles exist in the system:


• P1 → R1 → P2 → R3 → P3 → R2 → P1
• P2 → R3 → P3 → R2 → P2
• Processes P1, P2, and P3 are deadlocked. Resource-allocation graph
with a deadlock.

• However, there is no deadlock because that process P4 may release its


instance of resource type R2. That resource can then be allocated to P3,
breaking the cycle.

• If a resource-allocation graph does not have a cycle, then the system is


not in a deadlocked state.
• If there is a cycle, then the system may or may not be in a deadlocked
state.

UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 3


UNIT-3 OPERATING SYSTEMS
CSE-AIML, ACOE Page 4
Methods for Handling Deadlocks
• We can deal with the deadlock problem in one of three ways:
– 1)We can use a protocol to prevent or avoid deadlocks, ensuring that the system will never enter
a deadlocked state.
– 2)We can allow the system to enter a deadlocked state, detect it, and recover. – 3)We can ignore
the problem altogether and pretend that deadlocks never occur in the system.

1) To ensure that deadlocks never occur, the system can use either a deadlock prevention or a deadlock
avoidance scheme.
Deadlock prevention provides a set of methods for ensuring that at least one of the necessary conditions cannot
hold:
1. Mutual Exclusion
2. Hold and wait 3.No preemption
4.Circular wait.
Deadlock avoidance requires that the operating system be given additional information in advance concerning
which resources a process will request and use during its lifetime. With this additional knowledge, the operating
system can decide for each request whether or not the process should wait.

2) If a system does not employ either a deadlock-prevention or a deadlock avoidance algorithm, then a deadlock
situation may arise.
– In this environment, the system can provide an algorithm that examines the state of the system to
determine whether a deadlock has occurred and an algorithm to recover from the deadlock (if a
deadlock has indeed occurred).

UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 5


3) If a system neither ensures that a deadlock will never occur nor provides a mechanism for deadlock
detection and recovery then, we may arrive at a situation in which the system is in a deadlocked state yet has no
way of recognizing what has happened.

– In this case, the undetected deadlock will cause the system’s performance to deteriorate, because
resources are being held by processes that cannot run and because more and more processes, as
they make requests for resources, will enter a deadlocked state.

Eventually, the system will stop functioning and will need to be restarted manually.

Deadlock Prevention:
For a deadlock to occur, each of the four necessary conditions must hold.
• 1.Mutual Exclusion
• 2. Hold and wait
• 3.No preemption • 4.Circular wait.
Mutual Exclusion
• The mutual exclusion condition must hold for at least one resource must be non-sharable.
• Example: A printer cannot be simultaneously shared by several process
• Sharable resources, in contrast, do not require mutually exclusive access and thus cannot
be involved in a deadlock.
• Example :Read-only files are a good example of a sharable resource. If several processes
attempt to open a read-only file at the same time, they can be granted simultaneous access
to the file.

UNIT-3 OPERATING SYSTEMS


CSE-AIML, ACOE Page 6
Hold and Wait:
• To ensure that the hold-and-wait condition never occurs in the system, whenever a process
requests a resource, it does not hold any other resources.
• One protocol that is each process to request and be allocated all its resources before it
begins execution – An alternative protocol allows a process to request resources only
when it has none. A process may request some resources and use them. Before it can request
any additional resources, it must release all the resources that it is currently allocated.
Both these protocols have two main disadvantage:
1. Resource utilization may be low.
2.Starvation is possible

No Preemption:
To ensure that this condition does not hold, we can use the following protocol.
• If a process is holding some resources and requests another resource that cannot be immediately allocated
to it, then all resources the process is currently holding are preempted.
• Alternatively, if a process requests some resources, we first check whether they are available. If they are,
we allocate them. If they are not, we check 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.
• Example: This protocol is often applied to resources whose state can be easily saved and restored later,
such as CPU registers and memory space. It cannot generally be applied to such resources as printers and
tape drivers

Circular Wait:
Way to ensure that this condition never holds is to impose a total ordering of all resource types and to require that
each process requests resources in an increasing order of enumeration.
• Example: Suppose Process P1 is allocated Resources R5.Now if P1 requests for Resources R4 and R3(which
are lesser than R5) such requests will not be granted. Only request for resource greater than R5 will be
granted
Developing an ordering, or hierarchy, in itself does not prevent deadlock. It is up to application developers to write
programs that follow the ordering

Deadlock Avoidance
• An alternative method for avoiding deadlocks is to require additional information about how resources are
to be requested.
• With this knowledge of the complete sequence of requests and releases for each process, the system can
decide for each request whether or not the process should wait in order to avoid a possible future deadlock.
Safe State:
• A state is safe if the system can allocate resources to each process (up to its maximum) in some order and
still avoid a deadlock.
• A system is in a safe state only if there exists a safe sequence.
• A safe state is not a deadlocked state. Conversely, a deadlocked state is an unsafe state. Not all unsafe states
are deadlocks.
• An unsafe state may lead to a deadlock. As long as the state is safe, the operating system can avoid unsafe
(and deadlocked) states.
• Example:
Safe, unsafe, and
• We consider a system with twelve magnetic tape drives and three processes: P0, P1,
deadlocked and
state P2.
spaces.

UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 7


Scenario 1:

UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 8


• At time t0, the system is in a safe state. The sequence <P1, P0, P2> satisfies the safety condition.

Resource -allocation graph


for deadlock avoidance.

Unsafe state.

UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 9


• Total tape drives are free:12-9=3
• Now we allocate the 2 tape drives to P1 so it can complete executes. After completion of P1. P0 need 5 tape
drives…so we allocate. So it can complete it executes. After 10 tape drives are free so 7 can be allocate to
P2 .
Scenario 2:
• A system can go from a safe state to an unsafe state. Suppose that at time t1 process P2 requests and is
allocated one more tape drive.
• The system is no longer in safe state

Resource-Allocation-Graph Algorithm :

Claim Edge:
• A claim edge Pi → Rj indicates that process Pi may request resource Rj at some time in the future.

• It resembles a request edge in direction but is represented in the graph by a dashed line
• When process Pi requests resource Rj , the claim edge Pi → Rj is converted to a request edge.
• Similarly, when a resource Rj is released by Pi , the assignment edge Rj → Pi is reconverted to a claim edge
Pi → Rj .
The resources must be claimed a priori in the system.
• If no cycle exists, then the allocation of the resource will leave the system in a safe state. • If a cycle is found,
then the allocation will put the system in an unsafe state.

UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 10


UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 11
Banker’s Algorithm:
• An algorithm that can be used for Deadlock Avoidance in resource allocation Systems with multiple
instances of each resources type.
• It is given the name Banker’s algorithm because 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
Data structures must be maintained to implement the banker’s algorithm.
Let , n is the number of processes in the system and m is the number of resource types:
• Available: A 1-D array of size ‘m’ indicates the number of available resources of each type.
• If Available[j] = k, then k instances of resource type Rj are available.
• Max: An n × m matrix defines the maximum demand of each process.
• If Max[i][j] = k, then process Pi may request at most k instances of resource type Rj .
• Allocation: An n × m matrix defines the number of resources of each type currently allocated to each process.
• If Allocation[i][j] = k, then process Pi is currently allocated k instances of resource type Rj .
• Need. An n × m matrix indicates the remaining resource need of each process.
• If Need[i][j] = k, then process Pi may need k more instances of resource type Rj to complete its task. Note that
Need[i][j] equalsMax[i][j] − Allocation[i][j].

UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 12


UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 13
UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 14
Example for Resource Request Algorithm
• Suppose now that process P1 requests one additional instance of resource type A and two instances of
resource type C, so Request1 = (1,0,2). What will happen if process P1 requests one additional instance of
resource type A and two instances of resource type C?

• We must determine whether this new system state is safe. To do so, we again execute Safety algorithm on the
above data structures.

Deadlock Detection:
For Single Instance of Each Resource Type:
• Single Instance of Each Resource Type.
• If all resources have only a single instance, then we can define a deadlock detection algorithm that uses a
variant of the resource-allocation graph, called a wait-for graph.
• We obtain this graph from the resource-allocation graph by removing the resource nodes and collapsing the
appropriate edges.

UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 15


• In a wait for Graph:
– An edge from Pi to Pj indicates that Pi is waiting for Pj to release a resource that is needed by Pi
– In a Resource-Allocation graph,the same would be denoted as Pi→Rk, and Rk→Pi

UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 16


UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 17
Deadlock exists in the
system if and only if
the wait-for graph
contains a cycle

(a) Resource-allocation graph. (b) Corresponding wait-for graph.

Multiple Instances of a Resource Type


• The wait-for graph scheme is not applicable to a resource-allocation system with multiple instances of each reso
• We turn now to a deadlock detection algorithm that is applicable to such a system. The algorithm employs sever
the banker’s algorithm
• Available: A vector of length m indicates the number of available resources of each type.
• Allocation: An n × m matrix defines the number of resources of each type currently allocated to each process.
• Request: An n × m matrix indicates the current request of each process.
• If Request[i][j] equals k, then process Pi is requesting k more instances of resource type Rj .

Algorithm- for Multiple Instances of a Resource Type


1. LetWork and Finish be vectors of length m and n, respectively. Initialize Work = Available.
Finish[i] = false if Allocation i != 0, then
Finish[i] = true if Allocation i = 0, then
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.
3. Work =Work + Allocation i Finish[i] = true Go to step 2.
4. If Finish[i] ==false for some i, 0≤i<n, then the system is in a deadlocked state. Moreover, if Finish[i] == false, then p
Example:
• Consider a system with five processes P0 through P4 and three resource types A, B, and C. Resource type A has sev
C has six instances. Suppose that, at time T0, we have the following resource-allocation state:

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


results in Finish[i] == true for all i.

Solution:
• In this, Work = [0, 0, 0] &
Finish = [false, false, false, false, false]

• i=0 is selected as both Finish[0] = false and [0, 0, 0]<=[0, 0, 0].


Work =[0, 0, 0]+[0, 1, 0] =>[0, 1, 0] & Finish = [true, false, false, false, false].

UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 18


UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 19
• i=2 is selected as both Finish[2] = false and [0, 0, 0]<=[0, 1, 0].
• Work =[0, 1, 0]+[3, 0, 3] =>[3, 1, 3] &
Finish = [true, false, true, false, false].
• i=1 is selected as both Finish[1] = false and [2, 0, 2]<=[3, 1, 3]. • Work =[3, 1, 3]+[2, 0, 0] =>[5, 1, 3] &
Finish = [true, true, true, false, false].
• i=3 is selected as both Finish[3] = false and [1, 0, 0]<=[5, 1, 3]. • Work =[5, 1, 3]+[2, 1, 1] =>[7, 2, 4] &
Finish = [true, true, true, true, false].
• i=4 is selected as both Finish[4] = false and [0, 0, 2]<=[7, 2, 4]. • Work =[7, 2, 4]+[0, 0, 2] =>[7, 2, 6] &
Finish = [true, true, true, true, true]. • Since Finish is a vector of all true it means there is no
deadlock in this example

Recovery from Deadlock


What to do when a deadlock detection algorithm determines that a deadlock exists?
– 1. One possibility is to inform the operator that a deadlock has occurred and to let the operator
deal with the deadlock manually.
– 2. Second possibility 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 preempt some resources from one or more of the deadlocked processes.
Process Termination:
To eliminate deadlocks by aborting a process, we use one of two methods.
• 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.
• Abort one process at a time until the deadlock cycle is eliminated.
– A deadlock-detection algorithm must be invoked to determine whether any processes are still
deadlocked.
– Aborting a process may not be easy. If the process was in the midst of updating a file,
terminating it will leave that file in an incorrect state.f the process was in the midst of printing
data on a printer, the system must reset the printer to a correct state before printing the next job.
we should abort those processes whose termination will incur the minimum cost Many
factors may affect which process is chosen, including:
1.What the priority of the process is
2.How long the process has computed and how much longer the process will compute before completing its
designated task
3.How many and what types of resources the process has used (for example, whether the resources are simple
to preempt)
4.How many more resources the process needs in order to complete
5.How many processes will need to be terminated?
6.Whether the process is interactive or batch Resource Preemption
• we successively preempt some resources from processes and give these resources to other
processes until the deadlock cycle is broken.
• If preemption is required to deal with deadlocks, then three issues need to be addressed: –
Selecting a victim.
– Rollback.
– Starvation.

UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 20


UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 21
UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 22
• Selecting a victim - Deciding which resources to preempt from which processes involves many of the same
decision criteria outlined above.
• 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. )
• 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.

Ostrich Algorithm
• Deadlock Ignorance is also called as Ostrich Algorithm
• This algorithm is a well-used method to ignore problems. It gets its name
from The Ostrich and the way it sticks its head in the sand and ignores
everything that’s happening around just as the picture below shows
• Scientists all over the world believe that the most efficient method to deal
with deadlock is deadlock prevention.
• But the Engineers that deal with the system believe that deadlock prevention should be paid less attention
as there are very less chances for deadlock occurrence.
• System failure, compiler error, programming bugs, hardware crashes that occur once a week should be paid
more attention rather than deadlock problem that occur once in years.
• Therefore most of the engineers don’t pay much amount in eliminating the deadlock.
Pros of ignorance in deadlocks?
• No manpower is required to do any extra work. When the system crashes, just restart it. This is viable only
when a deadlock occurs rarely. If a computer is having deadlock once per 10 years then rebooting the
system is more preferred than pre Cons of ignorance in deadlocks?
• If it happens quite often, then this is not an optimal solution. You’ll have to invest some time in properly
dealing with deadlocks, that’s any of the next three methods mentioned above in prevention of deadlock.

UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 23


UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 24
UNIT-3 OPERATING SYSTEMS CSE-AIML, ACOE Page 25

You might also like