Professional Documents
Culture Documents
08 Deadlocks
08 Deadlocks
Permanent blocking of a set of processes
that either compete for system resources or
Operating Systems I communicate with each other
No efficient solution
Unit 8 – Deadlocks
Involve conflicting needs for resources by
Dr. Alejandro Zunino - ISISTAN two or more processes
Examples
Example
System has 2 tape drives.
P1 and P2 each hold one tape drive and each
needs another one.
Example
semaphores A and B, initialized to 1
P0 P1
wait (A); wait(B)
wait (B); wait(A)
Used by one process at a time and not
depleted by that use
Processes obtain resources that they later
release for reuse by other processes
Processors, I/O channels, main and
secondary memory, files, databases, and
semaphores
Deadlock occurs if each process holds one
resource and requests the other
Space is available for allocation of 200K
Created (produced) and destroyed
bytes, and the following sequence of events (consumed) by a process
occur
Interrupts, signals, messages, and
information in I/O buffers
P1 P2
... ...
1 2
Deadlock may occur if a Receive message is
Request 80K bytes; Request 70K bytes;
... ...
50K
Request 60K bytes; Request 80K bytes;
blocking
May take a rare combination of events to
cause deadlock
Deadlock occurs if both processes progress
to their second request
Example of Deadlock Resource-Allocation Graph
Deadlock occurs if receive is blocking
Process
...
P1
...
P2
Resource Type with 4 instances
Receive(P2); Receive(P1);
... ...
Send(P2, M1); Send(P1, M2);
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
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 Deadlock Prevention
Restrain the ways request can be made.
Ensure that the system will never enter a
Mutual Exclusion – not required for
deadlock state. sharable resources; must hold for
nonsharable resources.
Allow the system to enter a deadlock state
Hold and Wait – must guarantee that
and then recover. whenever a process requests a resource, it
does not hold any other resources.
Ignore the problem and pretend that
Require process to request and be allocated all
deadlocks never occur in the system; used its resources before it begins execution, or allow
by most operating systems, including UNIX. process to request resources only when the
process has none.
Low resource utilization; starvation possible.
When a process requests an available resource, system
If a system is in safe state no
must decide if immediate allocation leaves the system in a
safe state. deadlocks.
System is in safe state if there exists a safe sequence of all
processes.
Sequence < P1, P2, …, Pn> is safe if for each Pi, the
If a system is in unsafe state
resources that Pi can still request can be satisfied by possibility of deadlock.
currently available resources + resources held by all the Pj,
with j<I.
If Pi resource needs are not immediately available, then Pi can wait
Avoidance ensure that a system will never
until all Pj have finished. enter an unsafe state.
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.
Resource-Allocation Graph
Safe, Unsafe , Deadlock State
Algorithm
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.
When a resource is released by a process,
assignment edge reconverts to a claim
edge.
Resources must be claimed a priori in the
system.
Recovery scheme
Periodically invoke an algorithm that
searches for a cycle in the graph.
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.
Use most appropriate technique for handling
deadlocks within each class.