You are on page 1of 47

Deadlock

7 Cases of Deadlock
Conditions for Deadlock
Modeling Deadlocks
Strategies for Handling Deadlocks
Avoidance
Detection
Recovery
Starvation

Understanding Operating Systems 1


Deadlock (deadly embrace) -- a system-wide tangle of
resource requests that begins when 2+ jobs are put on
hold.

Each job is waiting for a vital resource to become available.


Needed resources are held by other jobs also waiting to run
but cant because theyre waiting for other unavailable
resources.
The jobs come to a standstill.
The deadlock is complete if remainder of system comes to a
standstill as well.

Resolved via external intervention.

Understanding Operating
Systems
2
Deadlock is more serious than
indefinite postponement or
starvation because it affects more
than one job.

Because resources are being tied


up, the entire system (not just a
few programs) is affected.

Requires outside intervention


(e.g., operators or users
terminate a 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.

Any other programs that require F1 or F2 are put on hold as


long as this situation continues.

Deadlock remains until a programs is withdrawn or forcibly


removed and its file is released.

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 7


Understanding Operating Systems 8
1. P1 requests tape drive Deadlock can occur
1 and gets it. when there is a limited
number of dedicated
2. P2 requests tape drive devices.
2 and gets it.
E.g., printers, plotters
3. P1 requests tape drive or tape drives.
2 but is blocked.

4. P2 requests tape drive


1 but is blocked.

Understanding Operating Systems 9


Deadlocks can happen when several processes
request, and hold on to, dedicated devices while
other processes act in a similar manner.

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.

Resource holding -- the act of holding a resource and not


releasing it; waiting for the other job to retreat.

No preemption -- the lack of temporary reallocation of


resources; once a job gets a resource it can hold on to it for as
long as it needs.

Circular wait -- each process involved in impasse is waiting for


another to voluntarily release the resource so that at least one
will be able to continue.

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, Sixth Edition 18


Understanding Operating Systems, Sixth Edition
19
Three graph scenarios to help detect deadlocks
System has three processes (P1, P2, P3)
System has three resources (R1, R2, R3)
Scenario one: no deadlock
Resources released before next process request
Scenario two: deadlock
Processes waiting for resource held by another
Scenario three: no deadlock
Resources released before deadlock

Understanding Operating Systems, Sixth Edition 20


No deadlock
Resources released before next process request

Understanding Operating Systems, Sixth Edition 21


Understanding Operating Systems, Sixth Edition
22
Deadlock
Processes waiting for resource held by another

Understanding Operating Systems, Sixth Edition 23


Understanding Operating Systems, Sixth Edition
24
No deadlock
Resources released before deadlock

Understanding Operating Systems, Sixth Edition 25


Understanding Operating Systems, Sixth Edition
26
Another example
Resources of same type
Allocated individually or grouped in same process
Graph clusters devices into one entity
Allocated individually or grouped in different
process
Graph clusters devices into one entity

Understanding Operating Systems, Sixth Edition 27


Understanding Operating Systems, Sixth Edition
28
Prevent one of the four conditions from
occurring.

Avoid the deadlock if it becomes probable.

Detect the deadlock when it occurs and


recover from it gracefully.

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.

Mutual exclusion is necessary in any computer system


because some resources (memory, CPU, dedicated
devices) must be exclusively allocated to 1 user at a
time.
Might be able to use spooling for some devices.
May trade 1 type of deadlock (Case 3) for another (Case
5).

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.

No preemption could be bypassed by allowing OS to


deallocate resources from jobs.
OK if state of job can be easily saved and restored.
Bad if preempt dedicated I/O device or files during
modification.

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

Require that jobs anticipate order in which they will


request resources.
A best order is difficult to determine.

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.

Dijkstras Bankers Algorithm (1965) used to regulate resources


allocation to avoid deadlock.

Safe state -- if there exists a safe sequence of all processes


where they can all get the resources needed.
Unsafe state -- doesnt necessarily lead to deadlock, but it does
indicate that system is an excellent candidate for one.

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.

OS (bank) must be sure never to satisfy a request that moves it


from a safe state to an unsafe one.
Job with smallest number of remaining resources < = number of
available resources

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 35


1. As they enter system, jobs must state in advance the
maximum number of resources needed.
2. Number of total resources for each class must remain
constant.
3. Number of jobs must remain fixed.
4. Overhead cost incurred by running the avoidance
algorithm can be quite high.
5. Resources arent well utilized because the algorithm
assumes the worst case.
6. Scheduling suffers as a result of the poor utilization and
jobs are kept waiting for resource allocation.

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

Understanding Operating Systems 40


R1 R2 R3

P1 Has Wants

P2 Wants Has Wants

P3 Has

Understanding Operating Systems 41


Process p1 holds 1 unit of R1 and wants 1
unit of R2
p2 holds 2 units of R2 and wants 1 unit of R1
and R3
p3 holds 1 unit of R1 and wants 1 unit of R2
p4 holds 2 units of r3 and wants 1 unit of R1

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.

There are several recovery algorithms, all requiring at


least one victim, an expendable job, which, when
removed from deadlock, frees system.

1. Terminate every job thats active in system and restart


them from beginning.
2. Terminate only the jobs involved in deadlock and ask their
users to resubmit them.

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.

4. Have job keep record (snapshot) of its progress so it can be


interrupted and then continued without starting again from the
beginning of its execution.
5. Select a non-deadlocked job, preempt resources its holding, and
allocate them to a deadlocked process so it can resume execution,
thus breaking the deadlock
6. Stop new jobs from entering system, which allows non-deadlocked
jobs to run to completion so theyll release their resources (no
victim).

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.

The dining philosophers Dijkstra (1968).

Avoid starvation via algorithm designed to detect


starving jobs which tracks how long each job has
been waiting for resources (aging).

Understanding Operating
Systems
47

You might also like