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

OS Lecture 4

Uploaded by

Mstafa Mhamad
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)
15 views

OS Lecture 4

Uploaded by

Mstafa Mhamad
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/ 54

Deadlocks

2021-2022
Introduction
• 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
• So Deadlock situation is: A set of blocked
processes each holding a resource and waiting
to acquire a resource held by another process
in the set.

• Example
– System has 2 disk drives.
– P1 and P2 each hold one disk drive and each needs
another one.
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. Memory space, CPU cycles, files, and
I/0 devices (such as printers and DVD drives) are
examples of resource types.
• If a system has two CPUs, then the resource type
CPU has two instances. Similarly, the resource
type printer may have five instances.
System Model
• Resource types R1, R2, . . ., Rm
• Each resource type Ri has Wi instances.
• Each process utilizes a resource as follows:
1. 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.
2. use
3. release
Deadlock Characterization
• In a deadlock, processes never finish
executing, and system resources are tied up,
preventing other jobs from starting.

• features that characterize deadlocks:


1. Necessary Conditions
2. Resource-Allocation Graph
Necessary Conditions
Deadlock can arise if four conditions hold simultaneously:

1. Mutual exclusion: only one process at a time


can use a resource.
2. Hold and wait: a process holding at least one
resource is waiting to acquire additional
resources held by other processes.
3. No preemption: a resource can be released
only voluntarily by the process holding it, after
that process has completed its task.
Necessary Conditions
4. Circular wait: there exists a set {P0, P1, …, Pn} 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 byP2, …, Pn–1 is waiting for a resource
that is held by Pn, and Pn is waiting for a resource that
is held by P0.

• Note: The circular-wait condition implies the hold-and-wait


condition, so the four conditions are not completely
independent.
Resource-Allocation Graph
• A set of vertices V and a set of edges E.
• V is partitioned into two types:

– P = {P1, P2, …, Pn}, the set consisting of all the


processes in the system.

– R = {R1, R2, …, Rm}, the set consisting of all resource


types in the system.

• request edge – directed edge Pi  Rj


• assignment edge – directed edge Rj  Pi
Example of a Resource Allocation
Graph
Basic Facts
• If graph contains no cycles  no deadlock

• If graph contains a cycle 


• if only one instance per resource type, then deadlock
has occurred In this case, a cycle in the graph is both a
necessary and a sufficient condition for the existence
of deadlock.
• if several instances per resource type, possibility of
deadlock occurred. In this case, a cycle in the graph is
a necessary but not a sufficient condition for the
existence of deadlock.
Resource Allocation Graph With A
Deadlock
Resource Allocation Graph With A
Deadlock

• Processes P1, P2, and P3 are deadlocked.


• Process P2 is waiting for the resource R3,
which is held by process P3.
• Process P3 is waiting for either process P1 or
process P2 to release resource R2.
• In addition, process P1 is waiting for process
P2 to release resource R1.
Graph With A Cycle But No Deadlock
Graph With A Cycle But No Deadlock

• In this example, we also have a cycle:


P1->R1->P3->R2->P1

• However, there is no deadlock. Observe that


process P4 may release its instance of
resource type R2. That resource can then be
allocated to P3, breaking the cycle.
Methods for Handling Deadlocks
• Generally speaking, we can deal with the
deadlock problem in one of two 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.
Deadlock Prevention
• To ensure that deadlocks never occur, the
system can use either 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. These
methods prevent deadlocks by constraining
how requests for resources can be made.
Deadlock Prevention Example
Mutual Exclusion
– Shared resources 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.
Deadlock Prevention Example
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.
– 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.
– Low resource utilization; starvation possible
Deadlock Prevention Example
No Preemption
– 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.
– 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
Deadlock Prevention Example
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 order to request resource Rj, a process
must first release all Ri such that i >= j.
Deadlock Avoidance
• Deadlock Avoidance: 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.
Deadlock Avoidance
• The various algorithms that use this approach differ in
the amount and type of information required. The
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.
Safe State
• When a process requests an available
resource, system must decide if immediate
allocation leaves the system in a 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.
• More formally, a system is in a safe state only
if there exists a safe sequence.
Safe Sequence
• A sequence of processes <P1, P2, ... , Pn> is a safe sequence for
the current allocation state if, for each Pi, the resource requests
that Pi can still make, can be satisfied by the currently available
resources plus the resources held by all Pj, with j < i.
• In this situation, if the resources that Pi needs are not
immediately available, then Pi can wait until all Pj have finished.
• When they have finished, Pi can obtain all of its needed
resources, complete its task, return its allocated resources, and
terminate.
• When Pi terminates, Pi+1 can obtain its needed resources, and
so on.
• If no such sequence exists, then the system state is said to be
unsafe.
Basic Facts
• 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. As long as the state is
safe, the operating system can avoid unsafe
(and deadlocked) states.
Safe, Unsafe , Deadlock State
Example
• consider a system with twelve magnetic tape
drives and three processes: P0 , P1, and P2.
• Process P0 requires ten tape drives, process
P1may need four tape drives, and process P2 may
need up to nine tape drives.
• Suppose that, at time t0 , process P0 is holding five
tape drives, process P1 is holding two tape drives,
and process P2 is holding two tape drives.
• Thus, there are three free tape drives.
Example

• At time t0 , the system is in a safe state. The


sequence <P1, P0, P2> satisfies the safety
condition.
Safe and Unsafe State
• A system can go from a safe state to an unsafe
state.
• In the previous example suppose that, at time
t1, process P2 requests and is allocated one
more tape drive.
• The system is no longer in a safe state.
• Why?!
Avoidance Algorithms
• Initially, the system is in a safe state.
• Whenever a process requests a resource that is
currently available, the system must decide
whether the resource can be allocated
immediately or whether the process must wait.
• The request is granted only if the allocation
leaves the system in a safe state.
• In this scheme, if a process requests a resource
that is currently available, it may still have to
wait. Thus, resource utilization may be lower than
it would otherwise be.
Banker’s Algorithm

• When a new process enters the system, it must


declare the maximum number of instances of
each resource type that it may need. This number
may not exceed the total number of resources in
the system.

• When a process requests a resource it may have


to wait.
Data Structures for the Banker’s
Algorithm
• Let n = number of processes, and m = number of
resources types
• 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]
Banker’s Algortithm
• finding out whether or not a system is in a
safe state -> using safety algorithm

• determining whether requests can be safely


granted -> using resource-request algorithm
Safety Algorithm
1.Let Work and Finish be vectors of length m and n,
respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1
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
Requesti = request vector for process Pi. If Requesti [j] = k then
process Pi wants k instances of resource type Rj
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;
 If safe  the resources are allocated to Pi
 If unsafe  Pi must wait, and the old resource-
allocation state is restored
Example
• 5 processes P0 through P4;
3 resource types:
A (10 instances), B (5instances), and C (7 instances)
Snapshot at time T0:
Allocation Max Available
ABC ABC ABC
P0 0 1 0 753 332
P1 2 0 0 322
P2 3 0 2 902
P3 2 1 1 222
P4 0 0 2 433
Example
• The content of the matrix Need is defined to be Max – Allocation

Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431

• The system is in a safe state since the sequence < P1, P3, P4, P2, P0>
satisfies safety criteria
Example: P1 Request (1,0,2)
• Check that Request  Available (that is, (1,0,2)  (3,3,2)  true.
Allocation Need Available
ABC ABC ABC
P0 0 1 0 743 230
P1 3 0 2 020
P2 3 0 2 600
P3 2 1 1 011
P4 0 0 2 431

• Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2>
satisfies safety requirement.
• Now can request for (3,3,0) by P4 be granted?
• Can request for (0,2,0) by P0 be granted?
Deadlock Detection
• If a system does not employ either a deadlock-
prevention or a deadlock avoidance algorithm,
then a deadlock situation may occur. 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
Single Instance of Each Resource Type

• Maintain wait-for graph


– Nodes are processes.
– Pi  Pj if Pi is waiting for Pj.

• Periodically invoke an algorithm that searches


for a cycle in the graph. If there is a cycle,
there exists a deadlock.
Resource-Allocation Graph and Wait-
for Graph

Resource-Allocation Graph Corresponding wait-for graph


Several Instances of a Resource Type
• The algorithm employs several data structures that are
similar to those used in the banker's algorithm
• Available: A vector of length m indicates the number
of available resources of each type.

• Allocation: An n x m matrix defines the number of


resources of each type currently allocated to each
process.

• Request: An n x m matrix indicates the current request


of each process. If Request [i , j ] = k, then process Pi is
requesting k more instances of resource type. Rj
Detection Algorithm
• The detection algorithm simply investigates every
possible allocation sequence of the processes
that remain to be completed.

The Algorithm:
1. Let Work and Finish be vectors of length m and n,
respectively Initialize:
(a) Work = Available
(b) For i = 1,2, …, n, if Allocation i  0, then
Finish[i] = false; otherwise, Finish[i] = true.
Detection Algorithm
2. Find an index i such that both:
(a) Finish[i] == false
(b) Requesti  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] == false, for some i, 1  i  n, then the
system is in deadlock state. Moreover, if Finish[i]
== false, then Pi is deadlocked
Example of Detection Algorithm
• Five processes P0 through P4; three resource types
A (7 instances), B (2 instances), and C (6 instances).
• Snapshot at time T0:
Allocation Request Available
ABC ABC ABC
P0 0 1 0 000 000
P1 2 0 0 202
P2 3 0 3 000
P3 2 1 1 100
P4 0 0 2 002

• Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i.
Example of Detection Algorithm
• P2 requests an additional instance of type C.
Request
ABC
P0 0 0 0
P1 2 0 2
P2 0 0 1
P3 1 0 0
P4 0 0 2
• State of system?
– Deadlock exists, consisting of processes P1, P2, P3, and P4
Detection-Algorithm Usage
• When should we invoke the detection
algorithm? The answer depends on two
factors:

1. How often is a deadlock likely to occur?


2. How many processes will be affected by
deadlock when it happens?
Detection-Algorithm Usage
• Deadlocks occur only when some process makes a
request that cannot be granted immediately. This
request may be the final request that completes a
chain of waiting processes.

• We can invoke the deadlock detection algorithm every


time a request for allocation cannot be granted
immediately.

• In this case, we can identify not only the deadlocked


set of processes but also the specific process that
"caused" the deadlock.
Detection-Algorithm Usage
• Of course, if the deadlock-detection algorithm
is invoked for every resource request, this will
incur a considerable overhead in computation
time.
• A less expensive alternative is simply to invoke
the algorithm at less frequent intervals
• For example, once per hour or whenever CPU
utilization drops below 40 percent.
Recovery from Deadlock

1. Process Termination
2. Preempting Recourses
Process Termination
• we use one of two methods. In both methods, the system
reclaims all resources allocated to the terminated
processes.

1. 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 and probably will have to be recomputed
later.
2. Abort one process at a time until the deadlock cycle is
eliminated: after each process is aborted, a deadlock-
detection algorithm must be invoked to determine
whether any processes are still deadlocked.
Process Termination
• In which order should we choose to abort?
• The question is basically an economic one; we should
abort those processes whose termination will incur the
minimum cost. Unfortunately, the term minimum cost
is not a precise one. Many factors may affect which
process is chosen, including:
– Priority of the process.
– How long process has computed, and how much longer to
completion.
– Resources the process has used.
– Resources process needs to complete.
Resource Preemption
• Selecting a victim process – minimize cost.

• Rollback – return to some safe state, restart process for


that state.

• Starvation – same process may always be picked as


victim. Clearly, we must ensure that a process can be
picked as a victim only a (small) finite number of times.

• The most common solution is to include the number of


rollbacks in the cost factor.

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