Академический Документы
Профессиональный Документы
Культура Документы
Deadlocks
By B Lokesh Joel
UNIT-III
Deadlocks: Paging,
System Model Structure of the Page Table
Deadlock Characterization Virtual Memory:
Methods for Handling Deadlocks, Background
Deadlock Prevention Demand Paging
Deadlock Avoidance Copy-on-Write
Deadlock Detection Page Replacement
Recovery from Deadlock. Allocation of Frames
MEMORY MANAGEMENT: Thrashing
Main Memory Memory-Mapped Files
Background Allocating Kernel Memory.
Swapping
Contiguous Memory Allocation
Segmentation.
Deadlock
Examples: Traffic Jam :
Dining Philosophers
Device allocation
process 1 requests tape drive 1 & gets it
process 2 requests tape drive 2 & gets it
process 1 requests tape drive 2 but is blocked
process 2 requests tape drive 1 but is blocked
OS DEADLOCK
Delay
Starvation
Note :
• Starvation Long Waiting
• Deadlock Infinite Waiting
• Every starvation is not Deadlock
• Every Deadlock is a Starvation
Deadlock Modeling
Modeled with directed graphs
R = {R1, R2, …, Rm}, the set consisting of all resource types in the
system
Eg Eg
CPU Files
Printer Registers
Edges
p allocation 2 instance
Resource Allocation Graph
Process
Pi
Pi requests instance of Rj
Rj
Request edge
The sequence of Pi is holding an instance of Rj Pi
Process’s recourse utilization
Assignment edge
Pi releases an instance of Rj Pi
Current allocation Outstand Request
Processes
R1 R2 R3 R1 R2 R3
P1 2 0 0 1 1 0
P2 3 1 0 0 0 0
P3 1 3 0 0 0 1
P4 0 1 1 0 1 0
p1 p2 p3 p4
Resource-allocation graph
R1 R3
P1 P2 P3
R2
R4
Can a deadlock happen?
Resource Allocation Graph With A
Deadlock
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.
Strategies for Handling Deadlock
1. Deadlock prevention
No deadlock
2. Deadlock Avoidance
(Bankers Algorithm)
3. Deadlock Detection Deadlock
Occurs
4. Deadlock Recovery
5. Deadlock ignorance
(Ostrich Algorithm)
Methods for Handling Deadlocks
Ensure that the system will never enter a deadlock state:
Deadlock prevention
Deadlock avoidance
Allow the system to enter a deadlock state and then recover
Ignore the problem and pretend that deadlocks never occur
in the system; used by most operating systems, including
UNIX
Deadlock Prevention
By dissatisfying one / more of necessary condition
Deadlock prevention ensure that system will never enter in
deadlock state
The OS is designed in such a way as to exclude a priori the
possibility of deadlock
Indirect methods of deadlock prevention:
to disallow one of the 3 policy conditions
Direct methods of deadlock prevention:
to prevent the occurrence of circular wait
Deadlock Prevention
Indirect methods of deadlock prevention
Mutual Exclusion – not required for sharable resources (e.g., read-only files); 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
Require process to request and be allocated all its resources before it begins
execution, or allow process to request resources only when the process has none
allocated to it.
Low resource utilization; starvation possible
No Preemption –
If a process that is holding some resources 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
Deadlock Prevention (Cont.)
Direct methods of deadlock prevention
Circular Wait – impose a total ordering of all resource types, and
require that each process requests resources in an increasing order of
enumeration
A protocol to prevent circular wait:
define a strictly increasing linear ordering O() for resource types.
Ex:
R1: tape drives: O(R1) = 2
R2: disk drives: O(R2) = 4
R3: printers: O(R3) = 7
A process initially request a number of instances of a resource type, say Ri.
A single request must be issued to obtain several instances.
After that, the process can request instances for resource type Rj if and only if
O(Rj) > O(Ri)
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
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
System is in safe state if there exists a sequence <P1, P2, …, Pn> of
ALL the processes in the systems such that for each Pi, the
resources that Pi can still request can be satisfied by currently
available resources + resources held by all the Pj, with j < i
That is:
If Pi resource needs are 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, Pi +1 can obtain its needed resources, and so on
Basic Facts
If a system is in safe state no deadlocks
Available
R0 R1 R2 R3
R0 has 8 instances, R1 has 5 instances, R2 has 9 instances, R3 has 7 instances
Available
R0 R1 R2 R3
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 010 743 230
P1 302 020
P2 302 600
P3 211 011
P4 002 431
Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies safety
requirement
The OS is deadlocked.
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
System-based recovery:
abort one or more processes in the circular wait