Professional Documents
Culture Documents
7 Cases of Deadlock
Conditions for Deadlock
Modeling Deadlocks
Strategies for Handling Deadlocks
Avoidance
Detection
Recovery
Starvation
Understanding Operating
Systems
2
Deadlock is more serious than
indefinite postponement or
starvation because it affects more
than one job.
Understanding Operating
Systems 3
1.Deadlocks on file requests
2.Deadlocks in databases
3.Deadlocks in dedicated device allocation
4.Deadlocks in multiple device allocation
5.Deadlocks in spooling
6.Deadlocks in disk sharing
7.Deadlocks in a network
Understanding Operating
Systems
4
If jobs can request and hold files for duration of their
execution, deadlock can occur.
Understanding Operating
Systems 5
Understanding Operating
Systems 6
1. P1 accesses R1 and locks it. Deadlock can occur if 2 processes
access & lock records in database.
2. P2 accesses R2 and locks it.
3 different levels of locking :
entire database for duration of
3. P1 requests R2, which is locked request
by P2. a subsection of the database
individual record until process is
4. P2 requests R1, which is locked completed.
by P1.
If dont use locks, can lead to a race
condition.
Understanding Operating
Systems 10
Most systems have transformed dedicated devices such as
a printer into a sharable device by installing a high-speed
device, a disk, between it and the CPU.
Disk accepts output from several users and acts as a
temporary storage area for all output until printer is ready
to accept it (spooling).
If printer needs all of a job's output before it will begin
printing, but spooling system fills available disk space with
only partially completed output, then a deadlock can
occur.
Understanding Operating
Systems
11
Disks are designed to be shared, so its not
uncommon for 2 processes access different
areas of same disk.
Without controls to regulate use of disk drive,
competing processes could send conflicting
commands and deadlock the system.
Understanding Operating
Systems
12
Understanding Operating
Systems
13
A network thats congested (or filled large %
of its I/O buffer space) can become
deadlocked if it doesnt have protocols to
control flow of messages through network.
Understanding Operating
Systems 14
Understanding Operating
Systems 15
Deadlock preceded by simultaneous
occurrence of four conditions that operating
system could have recognized:
Mutual exclusion
Resource holding
No preemption
Circular wait
Understanding Operating
Systems
16
Mutual exclusion -- the act of allowing only one process to
have access to a dedicated resource.
Understanding Operating
Systems
17
Directed graphs
Circles represent processes
Squares represent resources
Solid arrow from resource to process
Process holding resource
Solid arrow from a process to resource
Process waiting for resource
Arrow direction indicates flow
Cycle in graph
Deadlock involving processes and resources
Understanding Operating
Systems
29
To prevent a deadlock OS must eliminate 1 out of 4
necessary conditions.
Same condition cant be eliminated from every
resource.
Understanding Operating
Systems
30
Resource holding can be avoided by forcing each job to
request, at creation time, every resource it will need to
run to completion.
Significantly decreases degree of multiprogramming.
Peripheral devices would be idle because allocated to a job
even though they wouldn't be used all the time.
Understanding Operating
Systems
31
Circular wait can be bypassed if OS prevents formation of
a circle.
Havenders solution (1968) is based on a numbering system
for resources such as: printer = 1, disk = 2, tape= 3.
Forces each job to request its resources in ascending order.
Any number one devices required by job requested first; any
number two devices requested next
Understanding Operating
Systems
32
Even if OS cant remove 1 conditions for deadlock, it can avoid
one if system knows ahead of time sequence of requests
associated with each of the active processes.
Understanding Operating
Systems
33
Based on a bank with a fixed amount of capital that operates on
the following principles:
No customer will be granted a loan exceeding banks total capital.
All customers will be given a maximum credit limit when opening an
account.
No customer will be allowed to borrow over the limit.
The sum of all loans wont exceed the banks total capital.
Understanding Operating
Systems
34
Safe
Customer Loan amount Maximum credit Remaining credit
C1 0 4,000 4,000
C2 2,000 5,000 3,000
C3 4,000 8,000 4,000
Total loaned: $6,000
Total capital fund: $10,000
Unsafe
Customer Loan amount Maximum credit Remaining credit
C1 2,000 4,000 2,000
C2 3,000 5,000 2,000
C3 4,000 8,000 4,000
Total loaned: $9,000
Total capital fund: $10,000
Understanding Operating
Systems
36
Use directed graphs to show circular wait
which indicates a deadlock.
Algorithm used to detect circularity can be
executed whenever it is appropriate.
Understanding Operating
Systems
37
1. Find a process that is currently using a resource and
not waiting for one. Remove this process from graph and
return resource to available list.
2. Find a process thats waiting only for resource
classes that arent fully allocated.
Process isnt contributing to deadlock since eventually gets
resource its waiting for, finish its work, and return resource to
available list.
3. Go back to Step 1 and continue the loop until all
lines connecting resources to processes have been
removed.
Understanding Operating
Systems
38
39
R1 R2 R3
P1 Has Wants
P2 Has Wants
P3 Has
P1 Has Wants
P3 Has
Understanding Operating
Systems
42
R1 R1 R1 R2 R2 R3 R3
p1 has ask
p2 ask has has ask
p3 has ask
p4 ask has has
Understanding Operating
Systems
43
Once a deadlock has been detected it must be untangled
and system returned to normal as quickly as possible.
Understanding Operating
Systems
44
3. Terminate jobs involved in deadlock one at a time, checking to see if
deadlock is eliminated after each removal, until it has been resolved.
45
Priority of job under considerationhigh-priority
jobs are usually untouched.
CPU time used by jobjobs close to completion
are usually left alone.
Number of other jobs that would be affected if
this job were selected as the victim.
Programs working with databases deserve special
treatment.
Understanding Operating
Systems
46
Starvation -- result of conservative allocation of
resources where a single job is prevented from
execution because its kept waiting for resources that
never become available.
Understanding Operating
Systems
47