Operating System-Deadlock
Operating System-Deadlock
Operating System-Deadlock
A Biswas
IT IIEST Shibpur
Deadlocks
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 wait state. Waiting processes
may never again change state, because
the resources they have requested are
held by other waiting processes. This
situation is called deadlock.
The Deadlock Problem
• 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.
• Example
– semaphores A and B, initialized to 1
P0 P1
wait (A); wait(B)
wait (B); wait(A)
System Model
• Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
• Mutual exclusion:
only one process at
a time can use a
resource.
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
• No preemption: a
resource can be
released only
voluntarily by the
process holding it,
after that process has
completed its task.
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
• Pi requests 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
If cycle involves
resource types, each of
which has single
instance, then a cycle
implies deadlock.
Graph With A Cycle But No Deadlock
Cycle is a necessary
condition but not a
sufficient condition for a
deadlock to occur.
Basic Facts
• If graph contains no cycles ⇒
no deadlock.
• Rationale:
– make the common path faster and more reliable
– Deadlock prevention, avoidance or
detection/recovery algorithms are expensive
– if deadlock occurs only rarely, it is not worth the
overhead to implement any of these algorithms.
Deadlock Prevention
Restrain the ways request can be made.
Deny at least one deadlock characterization condition.
• 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.)
2. The deadlock-avoidance
algorithm dynamically
examines the resource-
allocation state to ensure
that there can never be a
circular-wait condition.
Deadlock Avoidance
Requires that the system has some additional a priori information available.
3. 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.
Safe State
System is in safe state if there
exists a safe sequence <P1, P2,
…, Pn> of ALL the processes in
the system 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
Safe State
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
Claim edge:
Pi → Rj indicated that process Pj
may request resource Rj;
represented by a dashed line.
Resource-Allocation Graph Scheme
Request edge:
- when a process requests a
resource –
claim edge request edge
Resource-Allocation Graph Scheme
Assignment Edge:
when the resource is
allocated to the process
request edge
assignment edge
Resource-Allocation Graph Scheme
• Max: n x m matrix.
• Allocation: n x m matrix.
If Allocation[i,j] = k then Pi is
currently allocated k instances of
Rj.
Data Structures for the Banker’s Algorithm
Let n = number of processes, and
m = number of resources types.
• Need: n x m matrix.
• Detection algorithm
• Recovery scheme
Single Instance of Each Resource
Type
• Maintain wait-for graph
– Nodes are processes.
– Pi → Pj if Pi is waiting for Pj to release
resources needed by Pi.