0% found this document useful (0 votes)
20 views33 pages

Deadlock-2

The document discusses deadlocks including definitions, necessary conditions, resource allocation graphs, and methods for detection, avoidance, and recovery. It provides examples of deadlock modeling using resource graphs and the banker's algorithm for determining safe resource allocation sequences.

Uploaded by

imrank39199
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)
20 views33 pages

Deadlock-2

The document discusses deadlocks including definitions, necessary conditions, resource allocation graphs, and methods for detection, avoidance, and recovery. It provides examples of deadlock modeling using resource graphs and the banker's algorithm for determining safe resource allocation sequences.

Uploaded by

imrank39199
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/ 33

Deadlocks

The Deadlock Problem

• A Computing example: record scanned document

• System has 1 scanner and 1 CD Recorder , 2


processes each need both devices.
• P0 acquires scanner and waits for CD recorder
• P1 acquires CD recorder and waits for scanner
Preemptable and Nonpreemptable
Resources
A preemptable resource is one that can be taken away from
the process owning it with no ill effects.
A non-preemptable resource, in contrast, is one that cannot be
taken away from its current owner without causing the
computation to fail. (Focus more)

Sequence of events required to use a resource:


1. Request the resource.
2. Use the resource.
3. Release the resource.
Introduction To Deadlocks

Deadlock can be defined formally as follows:

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.
Deadlock Characterization:
Necessary Conditions for Occurrence
Mutual Exclusion:
– At least 1 resource must be held in a non-sharable mode.
Hold and wait:
– There must be at least 1 process that is holding one resource and
is waiting for another
No preemption:
– Resources previously granted cannot be forcibly taken away from
a process. They must be explicitly released by the process holding
them.
Circular wait:
– There must be a circular chain of two or more processes, each of
which is waiting for a resource held by the next member of the
chain.
Resource Allocation Graphs
Process A
Resource R
Request a Resource A R
Hold a Resource A R

Deadlock A
R S
B
Deadlock Modeling (1)

Figure 6-3. Resource allocation graphs.


(a) Holding a resource. (b) Requesting a resource. (c) Deadlock.
Deadlock Modeling (2)

Cont’d on next page


Figure 6-4. An example of how deadlock occurs
and how it can be avoided. 8
Deadlock Modeling (3)

Cont’d on next page


Figure 6-4. An example of how deadlock occurs
and how it can be avoided.
Deadlock Modeling (4)

Figure 6-4. An example of how deadlock occurs


and how it can be avoided.
Deadlock Modeling (5)
Strategies for dealing with deadlocks:

1. Just ignore the problem.


2. Detection and recovery. Let deadlocks
occur, detect them, take action.
3. Dynamic avoidance by careful resource
allocation.
4. Prevention, by structurally negating one
of the four required conditions.
Deadlock Detection with
One Resource of Each Type (1)

•Process A holds R and wants S.


•Process B holds nothing but wants T.
•Process C holds nothing but wants S.
•Process D holds U and wants S and T.
•Process E holds T and wants V.
•Process F holds W and wants S.
•Process G holds V and wants U.

Figure 6-5. (a) A resource graph. (b) A cycle extracted from (a).
Recovery from Deadlock
• Recovery through preemption
– Example: snatch the printer from the process A, give it to process B. Process B
completes. Return the printer to process A. Process A completes.

• Recovery through rollback


– Create a checkpoint for the process A, stop process A, allocate the resource
needed to process B. Process B completes. Restart process A from the
checkpoint.

• Recovery through killing processes


– Carefully choose a process to kill so that others can proceed.
Deadlock Avoidance
• The previous approach can be used to avoid a
deadlock (rather than just detect one when it
occurs).
– Declare the maximum number of resources of each type that
will be required before the process starts.
– Before any resource allocation is made, ensure that the
allocation will not leave the system in a state where a
deadlock can occur.
– This will ensure that a process can always get access to its
needed maximums.
Safe State
A system is in a safe state if there exists a sequence of
resource assignments such that each process can be
allocated its maximum request, and then release the
resources, and then allow remaining processes to do the
same...
Process Max Needs Currently Allocated Total
P0 10 5 12
P1 4 2
Available
P2 9 2 3
Safe State
A system is in a safe state if there exists a sequence of
resource assignments such that each process can be
allocated its maximum request, and then release the
resources, and then allow remaining processes to do the
same...
Process Max Needs Currently Allocated Total
P0 10 5 12
P1 4 24
Available
P2 9 2 31
Safe State
A system is in a safe state if there exists a sequence of
resource assignments such that each process can be
allocated its maximum request, and then release the
resources, and then allow remaining processes to do the
same...
Process Max Needs Currently Allocated Total
P0 10 5 12
P1 0 40
Available
P2 9 2 15
Safe State

A system is in a safe state if there exists a sequence of


resource assignments such that each process can be
allocated its maximum request, and then release the
resources, and then allow remaining processes to do the
same...
Process Max Needs Currently Allocated Total
P0 10 510 12
P1 0 0
Available
P2 9 2 50
Safe and Unsafe States (1)

Total number of resources=10

Figure 6-9. Demonstration that the state in (a) is safe.


Safe and Unsafe States (2)

Note: An unsafe state is not a deadlocked state, however, it may lead to


deadlocked state. Starting at Fig. 6-10(b), the system can run for a while. In
fact, one process can even complete.

Figure 6-10. Demonstration that the state in (b) is not safe.


Banker's Algorithm
Use vectors and matrices to identify resource
requirements
– Available[j] = k; //resource j has k members available
– Max[i,j] = k; //Process i may need k members of resource
j
– Allocation [i,j] = k; //Process i is currently allocated k
members of resource j
– Need[i,j] = k; // Process i may need as many as k more of
resource j.
Banker's Algorithm
Terms and Formulae

Allocation Matrix: Currently allocated resources

Max Matrix: Maximum needed resources

Need Matrix: Further needed resources = Max – Allocation

Available Matrix: Currently total available resources after allocation


= Total Resources – Total Allocated
Banker's Algorithm
Terms and Formulae

Safety Algorithm (finding safe sequence/order)

If Need <= Available


Then
Execute process
New Available = Currently Available + Allocation
Else
Do not execute process. Go forward.
Banker's Example
Total Resources : A= 10, B= 5, C= 7
Allocation Max Available
A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 [Total Resources – Total
Allocated = 10 5 7 – 7 2 5]

P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
_______
Total: 7 2 5
Safe order/sequence: ???
Banker's Example
Calculate Need Matrix ?
Formula = Max – Allocation
Allocation Max Available Need
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3
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
_______
Total: 7 2 5
Safe order/sequence: ???
Banker's Example
Allocation Max Available Need
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3
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
_______
Total: 7 2 5

P0 => Need > Available


743>332
Don’t execute P0.
Go forward P1

Safe order/sequence:
Banker's Example
Allocation Max Available Need
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3
P1 2 0 0 3 2 2 5 3 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
_______
Total: 7 2 5

P1 => Need < Available


122<332
Execute P1.
New Available = 3 3 2 + 2 0 0 = 5 3 2

Safe order/sequence: P1
Banker's Example
Allocation Max Available Need
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3
P1 2 0 0 3 2 2 5 3 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
_______
Total: 7 2 5

P2 => Need > Available


600>532
Don’t execute P2.
Go forward P3

Safe order/sequence: P1
Banker's Example
Allocation Max Available Need
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3
P1 2 0 0 3 2 2 5 3 2 1 2 2
P2 3 0 2 9 0 2 7 4 3 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
_______
Total: 7 2 5

P3 => Need < Available


011<532
Execute P3.
New Available = 5 3 2 + 2 1 1 = 7 4 3

Safe order/sequence: P1, P3


Banker's Example
Allocation Max Available Need
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3
P1 2 0 0 3 2 2 5 3 2 1 2 2
P2 3 0 2 9 0 2 7 4 3 6 0 0
P3 2 1 1 2 2 2 7 4 5 0 1 1
P4 0 0 2 4 3 3 4 3 1
_______
Total: 7 2 5

P4 => Need < Available


431<743
Execute P4.
New Available = 7 4 3 + 0 0 2 = 7 4 5

Safe order/sequence: P1, P3, P4,


Banker's Example
Allocation Max Available Need
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3
P1 2 0 0 3 2 2 5 3 2 1 2 2
P2 3 0 2 9 0 2 7 4 3 6 0 0
P3 2 1 1 2 2 2 7 4 5 0 1 1
P4 0 0 2 4 3 3 7 5 5 4 3 1
_______
Total: 7 2 5

P0 => Need < Available


743<745
Execute P0.
New Available = 7 4 5 + 0 1 0 = 7 5 5

Safe order/sequence: P1, P3, P4, P0,


Banker's Example
Allocation Max Available Need
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3
P1 2 0 0 3 2 2 5 3 2 1 2 2
P2 3 0 2 9 0 2 7 4 3 6 0 0
P3 2 1 1 2 2 2 7 4 5 0 1 1
P4 0 0 2 4 3 3 7 5 5 4 3 1
_______ 10 5 7
Total: 7 2 5

P2 => Need < Available


600<755
Execute P2.
New Available = 7 5 5 + 3 0 2 = 10 5 7

Safe order/sequence: P1, P3, P4, P0, P2


The Banker’s Algorithm
for a Single Type Resource

If granting the request leads to an unsafe state, the request is denied, else accepted
.

Figure 6-11. Three resource allocation states:


(a) Safe. (b) Safe. (c) Unsafe.

You might also like