SlideShare ist ein Scribd-Unternehmen logo
1 von 49
Chapter 7: Deadlocks
 The Deadlock Problem
 System Model
 Deadlock Characterization
 Methods for Handling Deadlocks
Don’t care
 Deadlock Prevention
 Deadlock Avoidance
 Deadlock Detection and Recovery (from Deadlock)

Chapter Objectives




To develop an understanding of deadlocks,
which prevents a set of concurrent processes
from completing their tasks, while waiting for
some resources occupied by each other
indefinitely
To present a number of different methods for
preventing, avoiding and handling deadlocks in
a computer system.
Example: Narrow Bridge Crossing










Traffic can flow only in one direction on a bridge
Each section of a bridge can be viewed as a
resource.
If a deadlock occurs, it can be resolved if one car
backs up (preempt resources and rollback).
Several cars may have to be backed up, if a
deadlock occurs.
Starvation is also possible.
Traffic Crossing

T
r
a
f
f
i
c
2

Traffic 1
R1

R2

R4

R3
Traffic 3

There are 4 resources R1,R2,R3 and R4

There are 4 users. traffic T1,T2,T3 and T4
Deadlock: Traffic Crossing
T
r
a
f
f
i
c
2

Traffic 1
R1
R4

Deadlock
T1 holds resource R1, waiting for R2
T2 holds resource R2, waiting for R3
T3 holds resource R3, waiting for R4
T4 holds resource R4, waiting for R1

R2
R3
Traffic 3

Circular wait
User T1 waiting for T2 to complete
User T2 waiting for T3 to complete
User T3 waiting for T4 to complete
User T4 waiting for T1 to complete
The Deadlock Problem

Deadlock: A state in which a set of blocked processes,
each holding one or few resources and waiting to acquire
one or few resources held by other processes in the set,
in a cycle.
 Example




A System has only 2 disk drives.
P1 and P2 each hold one disk drive and each needs another one.
Keep the resource Occupied and wait for the resource occupied
by others

 Example
 Concurrent processes P0 and P1 share semaphores A and B,
which are initialized to 1
P0
wait (A);
wait (B);
………

P1
wait(B)
wait(A)
………

Process P0 waiting for process P1 to release (signal) semaphore B and
P1 is waiting for semaphore A to be signaled by P0
Resources System Model


Resource types R1, R2, . . ., Rm








Physical:-CPU cycles, memory space, I/O devices
Virtual:-That exists only logically (through software)
Critical:-That is sharable, but can be allocated to only
one requester

Multiple Resources: Each resource type Ri may
have Wi instances.
Utilization Protocol: Each process utilizes a
resource as follows:




requests
uses
releases
Deadlock Characterization
Deadlock can occur if following conditions hold simultaneously.








Mutual exclusion: Exclusive use of a resource, only by
one process at a time.
Hold and wait: A process holding at least one resource,
is waiting to acquire additional resources held by other
processes.
Non preemption: A resource cannot be released, until
that process has completed its task.
Circular wait: there exists a set {P0, …, P0} of waiting
processes such that P0 is waiting for a resource that is
being held by P1, P1 is waiting for a resource that is held
by P2, so on … and Pn is waiting for a resource that is
held by P0.
Resource-Allocation Graph (to determine
deadlock in a system)




.
A Graph consists of a set of vertices V and a set of
edges E
V is partitioned into two types:






Processes = {P1, P2, …, Pn}, the set of all the processes in the
system.
Resources = {R1, R2, …, Rm}, the set of all resource types in the
system.

E is partitioned into two types:


Request edge: A process requesting a resource
– directed edge
Pi → Rk



Assignment (Allocation) edge: A resource allocated to a process
– directed edge
Rj → Pk
Resource-Allocation Graph
(Processes and Multiple instances of resources.)


Process



Resource Type with 1 & 4 instances



Pi requests an instance of Rj

Pi
Rj



Pi is holding an instance of Rj

Pi
Rj
Example of a Resource Allocation Graph
Resource Allocation Graph With A Deadlock
Graph With A Cycle But No Deadlock
Basic Facts


If graph contains no cycles ⇒ no deadlock.



If graph contains a cycle ⇒



if only one instance per resource type, then deadlock.
if several instances per resource type, possibility of
deadlock.
Methods for Handling Deadlocks
1.

Ignore the problem and pretend that deadlocks
never occur in the system; used by most
operating systems, including UNIX.

2.

Ensure that the system will never enter in a
deadlock state.



Prevention
Avoidance

Allow the system to enter a deadlock state
detect it and then recover.

3.



Detection and Recovery
Deadlock Prevention
Bulk allocation of all required resources (by process itself or optionally
by OS), in the beginning, if possible, otherwise reject request.
Alternatively break any necessary condition to prevent deadlock




Mutual Exclusion – not required for sharable
resources; must hold for non-sharable resources.
Hold and Wait – must guarantee that whenever
a process requests a resource, it does not hold
any other resources.




Requires a process to release the allocated resources
before it requests for more (break hold condition), i.e.
request only when the process has no resource.
Low resource utilization; starvation possible.
Deadlock Prevention (Cont.)


Non Preemption –








If a process that is holding some resource, requests
another resource that cannot be immediately allocated
to it, then all resources currently being held are
released.
Preempted resources are added to the list of resources
for which the process is waiting.
Process will be restarted only when it can regain its old
resources, as well as the new ones that it is
requesting.

Circular Wait – impose a total ordering of all
resource types, and each process requests
resources in an increasing order of enumeration.
(Break circularity: no process could request already allocated resource)
Deadlock Avoidance
Requires that the system has some additional a priori information available.






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,
before servicing a request for resource allocation.
Resource-allocation state is defined by the
number of available and allocated resources, and
the maximum demands of the processes.
Safe, Unsafe , Deadlock Threat

No deadlock threat

Maximum Need

Allocated

claim

P0

10

6

4

P1

4

2

2

P2

9

2

7

Current state:

Total resources = 12

Available = 2

Safe: if next allocation is done to process P1 (leaves 1 tape, needs of P1 can be fulfilled that will return 4)
Unsafe: if next allocation is done to process P0 or P2 (needs of none can be fulfilled)
Basic Facts


If a system is in safe state ⇒ no threat of
deadlock.



If a system is in unsafe state ⇒ possibility of
deadlock (since processes usually state
maximum need, but may not use all resources).



Avoidance ⇒ ensure that a system will never
enter in an unsafe state, before allocation of
resources.
Safe Allocation (Banker’s Algorithm)






When a process requests a resource, system decides, if
current allocation leaves the system in a safe state then
it allocates the resource, otherwise denies allocation.
System is in safe state if there exists a possibility that
current allocation leaves some of the processes that can
complete their execution and the resources thereby
released can satisfy needs of some other processes
That is:


If Pi needs a resource, already allocated to Pj , and is not
immediately available, then Pi can wait until all Pj have finished.



When Pj is finished, Pi can obtain needed resources, execute,
return allocated resources, and terminate.
When Pi terminates, Pk can obtain its needed resources, and so
on.


Avoidance algorithms




In case of Single instance of a resource type.
Use a resource-allocation graph
Multiple instances of a resource type. Use the
banker’s algorithm





Know total needs of borrowers
Do not allocate all money (resources) at a time
Keep track of allocations and claims (still needed)
Allocate only, if current allocation leaves enough
reserves to fulfill the need of some one else, who will
return all borrowed money to fulfill need of others
Resource-Allocation Graph Scheme










Claim edge Pi → Rj indicated that process Pj may
request resource Rj; represented by a dashed
line.
Claim edge converts to request edge when a
process requests a resource.
Request edge converted to an assignment edge
when the resource is allocated to the process.
When a resource is released by a process,
assignment edge reconverts to a claim edge.
Resources must be claimed a priori in the
system.
Resource-Allocation Graph
R1 resource has been allocated to P1, P1 may request R2 resource
P2 have request for R1 resource and may request R2 resource

Request Edge

Assignment Edge

May request (needed)
Unsafe State In Resource-Allocation Graph
R1 resource has been allocated to P1, P1 may request R2 resource
P2 have request for R1 resource and R2 resource has been allocated to
P2

No threat of deadlock until P1 requests R2 resource
Resource-Allocation Graph Algorithm


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 (Circular wait)
Banker’s Algorithm (Single Resource Type)










Multiple instances of single type of resources.
Each process must a priori claim maximum need
(State total need).
Allocation in parts, not in total
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.
Returned resources can fulfill other’s need
Banker’s Algorithm (Safe Allocation)
Algorithm for one type, that can be extended for more types of resources.
Safe-allocation is a procedure to avoid deadlocks, invoked in an allocation procedure

Needed[ ] and Allocated[ ] Arrays contain no. of still needed and allocated resources for
each process. Following program supposedly allocates resource to nth process among all P
processes, calls safe_allocation procedure, if safe, make actual allocation, otherwise not

Allocate (n) {
Allocated [n] = Allocated [n] + 1
Needed[ n] = Needed [ n ] -1

// make allocation for nth process
// Assume allocation for nth process
// decrease need of one resource

If (safe_ allocation ( P ) ) {
// go ahead for actual allocation
}
else {
Allocated [n] = Allocated [n] - 1
Needed[ n] = Needed [ n ] + 1
}
}

// revert supposed allocation
// reset need
Cont. Banker’s Algorithm (Safe Allocation)
Safe-allocation algorithm for single type of resource (Say Mag. Tapes), but of multiple
instances. It can be extended for other types of resources

logical safe-allocation( int P)

// P is total number of currently running processes

{
logical finish [ ] ;

// To contain indication that process can finish or complete

logical done = FALSE;

// Checked all

int resources = TOTAL ;

// Total available resources of this type

for ( n=0 ; n<P ; n++)

// do for all processes

{
finish [ n ] = FALSE ;

// assume nth process can not finish

resources = resources – Allocated [ n ];
}

// Presently available resources
Cont: safe-allocation procedure
.
while ( ! done )
{
done = TRUE ;
// assume all processes are done / checked
for (n=0; n < P ; n++)
// check processes that can pay-back, if finished
{
if ( ! finish [n] ) && Needed [n] <= resources )
{
finish [n] = TRUE;
// Oh, It can complete
resources = resources + Allocated [ n ];
// reclaim resources
done = FALSE ;
// needs further checking
}
}
}
if ( resources == TOTAL )
return ( TRUE );
else
return ( FALSE );
}

// all processes finished, all resources returned
// allocation is safe

// end of safe-allocation procedure
Example: Single Type
Resources

Process 1

Process 2

Process 3

A

F

A

N

A

N

A

N

0

8

0

6

0

3

0

4

.

.

-

-

-

-

-

-

5

3

4

2

0

3

1

3

Safe

6

2

4

2

1

2

1

3

Safe

7

1

4

2

1

2

2

2

Unsafe

6

2

4

2

1

2

1

3

7

1

5

1

1

2

1

3

A: Allocated;

F: Free;

N : still Needed

Safe

Request
Data Structures: Banker’s Algorithm (multiple types)
Extension to multiple types of resources:-

(use multidimensional array)

Let n = Maximum number of processes,
and m = number of resource types. {one vector (dimension) for each resource type}


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. (It can
be avoided by using only needed array)
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.
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
Request = 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 – Request;
Allocationi = Allocationi + Requesti;

(Vector operation)

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 of Banker’s Algorithm




5 processes P0 through P4;
3 resource types (A, B and C):
A (10 instances), B (5instances), and C (7 instances).
Resource Vector is (A,B,C) or (10,5,7)
Snapshot at some time T0:
Allocation Max Available
ABC ABC ABC
P0 0 1 0 7 5 3
332
P1 2 0 0

322

P2 3 0 2

902

P3 2 1 1

222
Example (Cont.)


The content of the matrix Need is defined to be Max
– Allocation.
Need
ABC
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1



The system is in a safe state since the sequence <
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 1
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.
Can request for (3,3,0) by P4 be granted?
Banker’s Algorithm Example P1 Request (1,0,2)
Processes
Total:
P0
P1
P2
P3
P4
102<332

Max. Need
ABC
753
322
902
222
433
Req. by P1

Allocated
ABC
010
200
302
211
002
Allocate it

Claim
ABC
743
122
600
011
431

Available
10 5 7
10 4 7
847
545
334
332

(1 0 2) P1

322

302

020

230

P1 (0 2 0)
P3 (0 1 1)
P4 (4 3 1)
P0 (7 4 3)
P2 (6 0 0)

322
222
433
753
902

322
222
433
753
902

-----------

532
743
745
755
10 5 7

Reclaiming:

done = FALSE
while ( ! done )
{
done = TRUE ;
for (n=0; n < P ; n++)
{
if ( ! finish [n] ) && Needed [n] <= resource
{
finish [n] = TRUE;
resources = resources + Allocated [ n ];
done = FALSE ;
}
}
}

Allocation
Reclaiming
230 + 302
532 + 211

755 + 302

Tracing algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies and the request for (1,0,2) by P1 be granted?
Deadlock Detection and Recovery
(adopted on systems with remote chances of deadlock)


Allows system to enter into deadlock state





No need to pre state resources need
Saves time otherwise consumed on running algorithms while
allocating each resource

Detection algorithm


Execute automatically after regular intervals or interactively








When system or a group of processes look “BUSY, DOING
NOTHING”
When ready queue is nearly empty and blocked queues are heavily
populated
When idle process is taking most of the execution time

Recovery scheme


detecting deadlock, adopt conserving approach
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.



An algorithm to detect a cycle in a graph requires
an order of n2 operations, where n is the number
of vertices in the graph.
Resource-Allocation Graph and Wait-for Graph

Resource-Allocation Graph

Corresponding wait-for graph
Several Instances of a Resource Type


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 [ij] = k,
then process Pi is requesting k more instances
of resource type. Rj.
Detection 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 Allocationi ≠ 0, then
Finish[i] = false;otherwise, Finish[i] = true.

2.Find an index i such that both:
(a) Finish[i] == false
(b) Requesti ≤ Work
If no such i exists, go to step 4.
Detection Algorithm (Cont.)
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.
Algorithm requires an order of O( m x n2) operations to detect
whether the system is in deadlocked state .
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:
AllocationRequestAvailable
ABC ABC
ABC
P0 0 1 0
000
000
P1 2 0 0
202
P2 3 0 3 0 0 0
P3 2 1 1
100
P4 0 0 2
002
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true
Example (Cont.)


P2 requests an additional instance of type C.
Request
ABC
P0 0 0 0
P1 2 0 1
P2 0 0 1
P3 1 0 0
P4 0 0 2



State of system?




Can reclaim resources held by process P0, but insufficient
resources to fulfill other processes; requests.
Deadlock exists, consisting of processes P1, P2, P3, and P4.
Detection-Algorithm Usage


When, and how often, to invoke depends on:



How often a deadlock is likely to occur?
How many processes will need to be rolled back?




one for each disjoint cycle

If detection algorithm is invoked arbitrarily, there
may be many cycles in the resource graph and
so we would not be able to tell which of the
many deadlocked processes “caused” the
deadlock.
Recovery from Deadlock:
Process Termination


Abort all deadlocked processes.



Abort one process at a time until the deadlock cycle is
eliminated.



Considerations in order to choose a process to abort:








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.
How many processes will need to be terminated.
Is process interactive or batch?
Recovery from Deadlock: Resource
Preemption


Selecting a victim – minimize cost.



Rollback – return to some safe state, restart
process for that check pointed state.



Starvation Threat– same process may always
be picked as victim for denial of resource
allocation, include number of rollback in cost
factor.

Weitere ähnliche Inhalte

Was ist angesagt?

Compiler Design
Compiler DesignCompiler Design
Compiler Design
Mir Majid
 

Was ist angesagt? (20)

Linker and Loader
Linker and Loader Linker and Loader
Linker and Loader
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
Language processors
Language processorsLanguage processors
Language processors
 
report on aadhaar anlysis using bid data hadoop and hive
report on aadhaar anlysis using bid data hadoop and hivereport on aadhaar anlysis using bid data hadoop and hive
report on aadhaar anlysis using bid data hadoop and hive
 
Producer and consumer classical problem
Producer and consumer classical problemProducer and consumer classical problem
Producer and consumer classical problem
 
Teamcenter AWS Login Background image customization
Teamcenter AWS Login Background image customizationTeamcenter AWS Login Background image customization
Teamcenter AWS Login Background image customization
 
Graphs
GraphsGraphs
Graphs
 
Semantics analysis
Semantics analysisSemantics analysis
Semantics analysis
 
User Interface Standards
User Interface StandardsUser Interface Standards
User Interface Standards
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 
Principles of Programming Languages - Lecture Notes
Principles of Programming Languages -  Lecture NotesPrinciples of Programming Languages -  Lecture Notes
Principles of Programming Languages - Lecture Notes
 
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
 
Indexing and Hashing
Indexing and HashingIndexing and Hashing
Indexing and Hashing
 
OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitors
 
C functions
C functionsC functions
C functions
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Closure properties of context free grammar
Closure properties of context free grammarClosure properties of context free grammar
Closure properties of context free grammar
 
Chapter 11 - File System Implementation
Chapter 11 - File System ImplementationChapter 11 - File System Implementation
Chapter 11 - File System Implementation
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 

Andere mochten auch (7)

Deadlocks
DeadlocksDeadlocks
Deadlocks
 
Deadlock
DeadlockDeadlock
Deadlock
 
Ch7
Ch7Ch7
Ch7
 
Object detection technique using bounding box algorithm for
Object detection technique using bounding box algorithm forObject detection technique using bounding box algorithm for
Object detection technique using bounding box algorithm for
 
Deadlocks
Deadlocks Deadlocks
Deadlocks
 
Edge Detection algorithm and code
Edge Detection algorithm and codeEdge Detection algorithm and code
Edge Detection algorithm and code
 
Operating System Deadlock Galvin
Operating System  Deadlock GalvinOperating System  Deadlock Galvin
Operating System Deadlock Galvin
 

Ähnlich wie Mch7 deadlock

Module 3 Deadlocks.pptx
Module 3 Deadlocks.pptxModule 3 Deadlocks.pptx
Module 3 Deadlocks.pptx
shreesha16
 
Ch8 OS
Ch8 OSCh8 OS
Ch8 OS
C.U
 

Ähnlich wie Mch7 deadlock (20)

Os module 2 d
Os module 2 dOs module 2 d
Os module 2 d
 
Deadlock.ppt
Deadlock.pptDeadlock.ppt
Deadlock.ppt
 
deadlock in OS.pptx
deadlock in OS.pptxdeadlock in OS.pptx
deadlock in OS.pptx
 
Module 3 Deadlocks.pptx
Module 3 Deadlocks.pptxModule 3 Deadlocks.pptx
Module 3 Deadlocks.pptx
 
Gp1242 007 oer ppt
Gp1242 007 oer pptGp1242 007 oer ppt
Gp1242 007 oer ppt
 
Module-2Deadlock.ppt
Module-2Deadlock.pptModule-2Deadlock.ppt
Module-2Deadlock.ppt
 
Os unit 4
Os unit 4Os unit 4
Os unit 4
 
Chapter 7 - Deadlocks
Chapter 7 - DeadlocksChapter 7 - Deadlocks
Chapter 7 - Deadlocks
 
10. deadlock
10. deadlock10. deadlock
10. deadlock
 
OSLec14&15(Deadlocksinopratingsystem).pptx
OSLec14&15(Deadlocksinopratingsystem).pptxOSLec14&15(Deadlocksinopratingsystem).pptx
OSLec14&15(Deadlocksinopratingsystem).pptx
 
Ice
IceIce
Ice
 
Mca ii os u-3 dead lock & io systems
Mca  ii  os u-3 dead lock & io systemsMca  ii  os u-3 dead lock & io systems
Mca ii os u-3 dead lock & io systems
 
Operating System
Operating SystemOperating System
Operating System
 
CH07.pdf
CH07.pdfCH07.pdf
CH07.pdf
 
Deadlock (1).ppt
Deadlock (1).pptDeadlock (1).ppt
Deadlock (1).ppt
 
Deadlock- System model, resource types, deadlock problem, deadlock characteri...
Deadlock- System model, resource types, deadlock problem, deadlock characteri...Deadlock- System model, resource types, deadlock problem, deadlock characteri...
Deadlock- System model, resource types, deadlock problem, deadlock characteri...
 
Sucet os module_3_notes
Sucet os module_3_notesSucet os module_3_notes
Sucet os module_3_notes
 
Ch8 OS
Ch8 OSCh8 OS
Ch8 OS
 
OS_Ch8
OS_Ch8OS_Ch8
OS_Ch8
 
OSCh8
OSCh8OSCh8
OSCh8
 

Mehr von wahab13

Abbott's Textual Analysis : Software Engineering 2
Abbott's Textual Analysis : Software Engineering 2Abbott's Textual Analysis : Software Engineering 2
Abbott's Textual Analysis : Software Engineering 2
wahab13
 
Hw1 SE sol
Hw1 SE solHw1 SE sol
Hw1 SE sol
wahab13
 
deadlock avoidance
deadlock avoidancedeadlock avoidance
deadlock avoidance
wahab13
 
Polygon Fill
Polygon FillPolygon Fill
Polygon Fill
wahab13
 

Mehr von wahab13 (6)

Texture mapping
Texture mapping Texture mapping
Texture mapping
 
Abbott's Textual Analysis : Software Engineering 2
Abbott's Textual Analysis : Software Engineering 2Abbott's Textual Analysis : Software Engineering 2
Abbott's Textual Analysis : Software Engineering 2
 
Hw1 SE sol
Hw1 SE solHw1 SE sol
Hw1 SE sol
 
deadlock avoidance
deadlock avoidancedeadlock avoidance
deadlock avoidance
 
Polygon Fill
Polygon FillPolygon Fill
Polygon Fill
 
Paging examples
Paging examplesPaging examples
Paging examples
 

Kürzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 

Mch7 deadlock

  • 1. Chapter 7: Deadlocks  The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks Don’t care  Deadlock Prevention  Deadlock Avoidance  Deadlock Detection and Recovery (from Deadlock) 
  • 2. Chapter Objectives   To develop an understanding of deadlocks, which prevents a set of concurrent processes from completing their tasks, while waiting for some resources occupied by each other indefinitely To present a number of different methods for preventing, avoiding and handling deadlocks in a computer system.
  • 3. Example: Narrow Bridge Crossing      Traffic can flow only in one direction on a bridge Each section of a bridge can be viewed as a resource. If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback). Several cars may have to be backed up, if a deadlock occurs. Starvation is also possible.
  • 4. Traffic Crossing T r a f f i c 2 Traffic 1 R1 R2 R4 R3 Traffic 3 There are 4 resources R1,R2,R3 and R4 There are 4 users. traffic T1,T2,T3 and T4
  • 5. Deadlock: Traffic Crossing T r a f f i c 2 Traffic 1 R1 R4 Deadlock T1 holds resource R1, waiting for R2 T2 holds resource R2, waiting for R3 T3 holds resource R3, waiting for R4 T4 holds resource R4, waiting for R1 R2 R3 Traffic 3 Circular wait User T1 waiting for T2 to complete User T2 waiting for T3 to complete User T3 waiting for T4 to complete User T4 waiting for T1 to complete
  • 6. The Deadlock Problem Deadlock: A state in which a set of blocked processes, each holding one or few resources and waiting to acquire one or few resources held by other processes in the set, in a cycle.  Example    A System has only 2 disk drives. P1 and P2 each hold one disk drive and each needs another one. Keep the resource Occupied and wait for the resource occupied by others  Example  Concurrent processes P0 and P1 share semaphores A and B, which are initialized to 1 P0 wait (A); wait (B); ……… P1 wait(B) wait(A) ……… Process P0 waiting for process P1 to release (signal) semaphore B and P1 is waiting for semaphore A to be signaled by P0
  • 7. Resources System Model  Resource types R1, R2, . . ., Rm      Physical:-CPU cycles, memory space, I/O devices Virtual:-That exists only logically (through software) Critical:-That is sharable, but can be allocated to only one requester Multiple Resources: Each resource type Ri may have Wi instances. Utilization Protocol: Each process utilizes a resource as follows:    requests uses releases
  • 8. Deadlock Characterization Deadlock can occur if following conditions hold simultaneously.     Mutual exclusion: Exclusive use of a resource, only by one process at a time. Hold and wait: A process holding at least one resource, is waiting to acquire additional resources held by other processes. Non preemption: A resource cannot be released, until that process has completed its task. Circular wait: there exists a set {P0, …, P0} of waiting processes such that P0 is waiting for a resource that is being held by P1, P1 is waiting for a resource that is held by P2, so on … and Pn is waiting for a resource that is held by P0.
  • 9. Resource-Allocation Graph (to determine deadlock in a system)   . A Graph consists of a set of vertices V and a set of edges E V is partitioned into two types:    Processes = {P1, P2, …, Pn}, the set of all the processes in the system. Resources = {R1, R2, …, Rm}, the set of all resource types in the system. E is partitioned into two types:  Request edge: A process requesting a resource – directed edge Pi → Rk  Assignment (Allocation) edge: A resource allocated to a process – directed edge Rj → Pk
  • 10. Resource-Allocation Graph (Processes and Multiple instances of resources.)  Process  Resource Type with 1 & 4 instances  Pi requests an instance of Rj Pi Rj  Pi is holding an instance of Rj Pi Rj
  • 11. Example of a Resource Allocation Graph
  • 12. Resource Allocation Graph With A Deadlock
  • 13. Graph With A Cycle But No Deadlock
  • 14. Basic Facts  If graph contains no cycles ⇒ no deadlock.  If graph contains a cycle ⇒   if only one instance per resource type, then deadlock. if several instances per resource type, possibility of deadlock.
  • 15. Methods for Handling Deadlocks 1. Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX. 2. Ensure that the system will never enter in a deadlock state.   Prevention Avoidance Allow the system to enter a deadlock state detect it and then recover. 3.  Detection and Recovery
  • 16. Deadlock Prevention Bulk allocation of all required resources (by process itself or optionally by OS), in the beginning, if possible, otherwise reject request. Alternatively break any necessary condition to prevent deadlock   Mutual Exclusion – not required for sharable resources; must hold for non-sharable resources. Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources.   Requires a process to release the allocated resources before it requests for more (break hold condition), i.e. request only when the process has no resource. Low resource utilization; starvation possible.
  • 17. Deadlock Prevention (Cont.)  Non Preemption –     If a process that is holding some resource, requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. Preempted resources are added to the list of resources for which the process is waiting. Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. Circular Wait – impose a total ordering of all resource types, and each process requests resources in an increasing order of enumeration. (Break circularity: no process could request already allocated resource)
  • 18. Deadlock Avoidance Requires that the system has some additional a priori information available.    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, before servicing a request for resource allocation. Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes.
  • 19. Safe, Unsafe , Deadlock Threat No deadlock threat Maximum Need Allocated claim P0 10 6 4 P1 4 2 2 P2 9 2 7 Current state: Total resources = 12 Available = 2 Safe: if next allocation is done to process P1 (leaves 1 tape, needs of P1 can be fulfilled that will return 4) Unsafe: if next allocation is done to process P0 or P2 (needs of none can be fulfilled)
  • 20. Basic Facts  If a system is in safe state ⇒ no threat of deadlock.  If a system is in unsafe state ⇒ possibility of deadlock (since processes usually state maximum need, but may not use all resources).  Avoidance ⇒ ensure that a system will never enter in an unsafe state, before allocation of resources.
  • 21. Safe Allocation (Banker’s Algorithm)    When a process requests a resource, system decides, if current allocation leaves the system in a safe state then it allocates the resource, otherwise denies allocation. System is in safe state if there exists a possibility that current allocation leaves some of the processes that can complete their execution and the resources thereby released can satisfy needs of some other processes That is:  If Pi needs a resource, already allocated to Pj , and is not immediately available, then Pi can wait until all Pj have finished.  When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate. When Pi terminates, Pk can obtain its needed resources, and so on. 
  • 22. Avoidance algorithms   In case of Single instance of a resource type. Use a resource-allocation graph Multiple instances of a resource type. Use the banker’s algorithm     Know total needs of borrowers Do not allocate all money (resources) at a time Keep track of allocations and claims (still needed) Allocate only, if current allocation leaves enough reserves to fulfill the need of some one else, who will return all borrowed money to fulfill need of others
  • 23. Resource-Allocation Graph Scheme      Claim edge Pi → Rj indicated that process Pj may request resource Rj; represented by a dashed line. Claim edge converts to request edge when a process requests a resource. Request edge converted to an assignment edge when the resource is allocated to the process. When a resource is released by a process, assignment edge reconverts to a claim edge. Resources must be claimed a priori in the system.
  • 24. Resource-Allocation Graph R1 resource has been allocated to P1, P1 may request R2 resource P2 have request for R1 resource and may request R2 resource Request Edge Assignment Edge May request (needed)
  • 25. Unsafe State In Resource-Allocation Graph R1 resource has been allocated to P1, P1 may request R2 resource P2 have request for R1 resource and R2 resource has been allocated to P2 No threat of deadlock until P1 requests R2 resource
  • 26. Resource-Allocation Graph Algorithm  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 (Circular wait)
  • 27. Banker’s Algorithm (Single Resource Type)       Multiple instances of single type of resources. Each process must a priori claim maximum need (State total need). Allocation in parts, not in total 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. Returned resources can fulfill other’s need
  • 28. Banker’s Algorithm (Safe Allocation) Algorithm for one type, that can be extended for more types of resources. Safe-allocation is a procedure to avoid deadlocks, invoked in an allocation procedure Needed[ ] and Allocated[ ] Arrays contain no. of still needed and allocated resources for each process. Following program supposedly allocates resource to nth process among all P processes, calls safe_allocation procedure, if safe, make actual allocation, otherwise not Allocate (n) { Allocated [n] = Allocated [n] + 1 Needed[ n] = Needed [ n ] -1 // make allocation for nth process // Assume allocation for nth process // decrease need of one resource If (safe_ allocation ( P ) ) { // go ahead for actual allocation } else { Allocated [n] = Allocated [n] - 1 Needed[ n] = Needed [ n ] + 1 } } // revert supposed allocation // reset need
  • 29. Cont. Banker’s Algorithm (Safe Allocation) Safe-allocation algorithm for single type of resource (Say Mag. Tapes), but of multiple instances. It can be extended for other types of resources logical safe-allocation( int P) // P is total number of currently running processes { logical finish [ ] ; // To contain indication that process can finish or complete logical done = FALSE; // Checked all int resources = TOTAL ; // Total available resources of this type for ( n=0 ; n<P ; n++) // do for all processes { finish [ n ] = FALSE ; // assume nth process can not finish resources = resources – Allocated [ n ]; } // Presently available resources
  • 30. Cont: safe-allocation procedure . while ( ! done ) { done = TRUE ; // assume all processes are done / checked for (n=0; n < P ; n++) // check processes that can pay-back, if finished { if ( ! finish [n] ) && Needed [n] <= resources ) { finish [n] = TRUE; // Oh, It can complete resources = resources + Allocated [ n ]; // reclaim resources done = FALSE ; // needs further checking } } } if ( resources == TOTAL ) return ( TRUE ); else return ( FALSE ); } // all processes finished, all resources returned // allocation is safe // end of safe-allocation procedure
  • 31. Example: Single Type Resources Process 1 Process 2 Process 3 A F A N A N A N 0 8 0 6 0 3 0 4 . . - - - - - - 5 3 4 2 0 3 1 3 Safe 6 2 4 2 1 2 1 3 Safe 7 1 4 2 1 2 2 2 Unsafe 6 2 4 2 1 2 1 3 7 1 5 1 1 2 1 3 A: Allocated; F: Free; N : still Needed Safe Request
  • 32. Data Structures: Banker’s Algorithm (multiple types) Extension to multiple types of resources:- (use multidimensional array) Let n = Maximum number of processes, and m = number of resource types. {one vector (dimension) for each resource type}  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. (It can be avoided by using only needed array) 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.
  • 33. 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.
  • 34. Resource-Request Algorithm for Process Pi Request = 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 – Request; Allocationi = Allocationi + Requesti; (Vector operation) Needi = Needi – Requesti; If safe ⇒ the resources are allocated to Pi. If unsafe ⇒ Pi must wait, and the old resource-allocation state is restored
  • 35. Example of Banker’s Algorithm   5 processes P0 through P4; 3 resource types (A, B and C): A (10 instances), B (5instances), and C (7 instances). Resource Vector is (A,B,C) or (10,5,7) Snapshot at some time T0: Allocation Max Available ABC ABC ABC P0 0 1 0 7 5 3 332 P1 2 0 0 322 P2 3 0 2 902 P3 2 1 1 222
  • 36. Example (Cont.)  The content of the matrix Need is defined to be Max – Allocation. Need ABC P0 7 4 3 P1 1 2 2 P2 6 0 0 P3 0 1 1 P4 4 3 1  The system is in a safe state since the sequence <
  • 37. 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 1 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. Can request for (3,3,0) by P4 be granted?
  • 38. Banker’s Algorithm Example P1 Request (1,0,2) Processes Total: P0 P1 P2 P3 P4 102<332 Max. Need ABC 753 322 902 222 433 Req. by P1 Allocated ABC 010 200 302 211 002 Allocate it Claim ABC 743 122 600 011 431 Available 10 5 7 10 4 7 847 545 334 332 (1 0 2) P1 322 302 020 230 P1 (0 2 0) P3 (0 1 1) P4 (4 3 1) P0 (7 4 3) P2 (6 0 0) 322 222 433 753 902 322 222 433 753 902 ----------- 532 743 745 755 10 5 7 Reclaiming: done = FALSE while ( ! done ) { done = TRUE ; for (n=0; n < P ; n++) { if ( ! finish [n] ) && Needed [n] <= resource { finish [n] = TRUE; resources = resources + Allocated [ n ]; done = FALSE ; } } } Allocation Reclaiming 230 + 302 532 + 211 755 + 302 Tracing algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies and the request for (1,0,2) by P1 be granted?
  • 39. Deadlock Detection and Recovery (adopted on systems with remote chances of deadlock)  Allows system to enter into deadlock state    No need to pre state resources need Saves time otherwise consumed on running algorithms while allocating each resource Detection algorithm  Execute automatically after regular intervals or interactively     When system or a group of processes look “BUSY, DOING NOTHING” When ready queue is nearly empty and blocked queues are heavily populated When idle process is taking most of the execution time Recovery scheme  detecting deadlock, adopt conserving approach
  • 40. 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.  An algorithm to detect a cycle in a graph requires an order of n2 operations, where n is the number of vertices in the graph.
  • 41. Resource-Allocation Graph and Wait-for Graph Resource-Allocation Graph Corresponding wait-for graph
  • 42. Several Instances of a Resource Type  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 [ij] = k, then process Pi is requesting k more instances of resource type. Rj.
  • 43. Detection 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 Allocationi ≠ 0, then Finish[i] = false;otherwise, Finish[i] = true. 2.Find an index i such that both: (a) Finish[i] == false (b) Requesti ≤ Work If no such i exists, go to step 4.
  • 44. Detection Algorithm (Cont.) 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. Algorithm requires an order of O( m x n2) operations to detect whether the system is in deadlocked state .
  • 45. 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: AllocationRequestAvailable ABC ABC ABC P0 0 1 0 000 000 P1 2 0 0 202 P2 3 0 3 0 0 0 P3 2 1 1 100 P4 0 0 2 002 Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true
  • 46. Example (Cont.)  P2 requests an additional instance of type C. Request ABC P0 0 0 0 P1 2 0 1 P2 0 0 1 P3 1 0 0 P4 0 0 2  State of system?   Can reclaim resources held by process P0, but insufficient resources to fulfill other processes; requests. Deadlock exists, consisting of processes P1, P2, P3, and P4.
  • 47. Detection-Algorithm Usage  When, and how often, to invoke depends on:   How often a deadlock is likely to occur? How many processes will need to be rolled back?   one for each disjoint cycle If detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph and so we would not be able to tell which of the many deadlocked processes “caused” the deadlock.
  • 48. Recovery from Deadlock: Process Termination  Abort all deadlocked processes.  Abort one process at a time until the deadlock cycle is eliminated.  Considerations in order to choose a process to abort:       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. How many processes will need to be terminated. Is process interactive or batch?
  • 49. Recovery from Deadlock: Resource Preemption  Selecting a victim – minimize cost.  Rollback – return to some safe state, restart process for that check pointed state.  Starvation Threat– same process may always be picked as victim for denial of resource allocation, include number of rollback in cost factor.