Professional Documents
Culture Documents
Deadlock definition:
- defined as the permanent blocking of a set of processes that compete for system
resource, including database records and communication lines. (by Eskicioglu &
Marsland)
Example
1. System has 2 tape drives.
P1 and P2 each hold one tape drive and each needs another one.
Deadlock occurs when a set of processes are in a wait state, because each
process is waiting for a resource that is held by some other waiting process.
Therefore, all deadlocks involve conflicting resource needs by two or more
processes.
RESOURCE - is a hardware device (e.g. tape drive, memory) or a piece of information (e.g.
locked record in the database).
1. Reusable - something that can be safely used by one process at a time and is not
depleted by that use. Processes obtain resources that they later release
for reuse by others.
Types of Resources:
1. Preemptable resource – a resource that can be taken away from the process owning it
with no ill effect (needs save/ restore)
2. Non-preemptable – a resource that cannot be taken away from its current owner
without causing the computation to fail.
Question:
Classify the following resources as either sharable or non-sharable:
a) hard disk b) file c) keyboard
System Model:
System consists of a finite number of resources and a finite number of
processes competing for such resource.
Resources are partitioned into several types, each of which consists of
some number of identical instances.
A set of processes is deadlocked when every process in the set is waiting for an
event that can be caused only by another process in the set. Therefore, deadlocks are
undesirable.
Printer
P
2. HOLD AND WAIT – there must exist a process that is holding a resource and is waiting
to acquire additional resources currently being held by other processes.
P1
Printer File
P Wait
Hold
3. NO PREEMPTION – a resource cannot be preempted, that is, process will not give up
a resource until it is finished with it. Releasing of resource can only be done voluntarily
by the process after using it.
P
4. CIRCULAR WAIT – each process in the chain holds a resource requested by another.
P1
File
Printer
P2
Additional:
Rx has 2 units
Some resources come in multiple units called number of instances.
Ri Ri Ri
Pi Pi Pi
P= P1, P2, P3
R =R1, R2, R3, R4
Instances: R1 = 1, R2 =2, R3 = 1, R4 = 3
Basic Facts
Example:
P1
R1 R2
P2
Questions:
Is there a cycle? How many? ________ ________ ________
Is there a deadlock? ________ ________ ________
R2 R4
Blocked P2 P3
P1 Blocked
R1 R3 R5
P1 R2 P2 R4 P3 R3
Example 1: Sample 2:
R1 R1
R4 R4
P4 P4
P1 P1
P2 P2
R2 R3
P3
R2 R3
P2
Sample 3: Sample 4:
R1
R1 P3 R5
P4 R5
R4
R2 R2
P2 R4
P2
P1
P4
P3
R3
P1
R3
Engineers: ask how serious it is, and do not want to pay a penalty in
performance and convenience.
A. Deadlock Prevention:
- Preventing the occurrence of one of the necessary conditions leading to deadlock.
Deadlock prevention strategy is very conservative; it solves the problem of deadlock
by limiting access to resources and by imposing restrictions on processes.
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.
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.
Circular Wait – impose a total ordering of all resource types, and require that each
process requests resources in an increasing order of enumeration.
B. 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 safe sequence of all processes.
Sequence <P1, P2, …, Pn> is safe if 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.
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
Given the RAG of system (initially in a safe state), grant a resource allocation request if
and only if doing so will not introduce a cycle in the graph (taking into consideration all
assignment, request and claim edges). Otherwise, the requesting process has to wait.
R1
1. Operation : P2 requests R3
Action : Request Granted
R3 System State : SAFE
P1 P2
2. Operation : P3 requests R2
Action : REQUEST DENIED!!!
System State : Leads to deadlock
P3
R2
Sample 3:
Total instances: 8
Is it Safe/Unsafe? If safe, what is the safe sequence?
_______________________________________________________________________
Sample 4:
Process Maximum Need Currently Remaining Need
allocated
P0 5 3
P1 9 1
P2 7 2
Total instances: 8
Is it Safe/Unsafe? If safe, what is the safe sequence?
_______________________________________________________________________
Banker’s Algorithm
(for Multiple Resource Instances)
Multiple instances.
Each process must a priori claim maximum use.
When a process requests a resource it may have to wait.
When a process gets all its resources it must return them in a finite amount of time.
Data Structures:
Safety Algorithm
Step 1. Find and i such that both: (Finish [i] = false and Need[i] Work)
If no such i exists, go to step 3.
Request = request vector for process Pi. If Requesti [j] = k then process Pi wants k
instances of resource type Rj.
Sample problem #2: Consider a system with 5 processes and four resource types with
instances as follows: A = 3, B = 10, C = 12, and D = 12:
Sample problem #3: Consider a computer system that has 4 magnetic tape drives (MT), 4
exchangeable disk drives (DD), 3 line printers (LP), and 2 card readers (CR).
Input/output to these devices is not spooled and these devices are non-sharable. The
system is currently running 4 processes, whose maximum requirement and current
allocations of the four resources are given below:
Max P1 P2 P3 P4 Current P1 P2 P3 P4
MT 0 0 2 3 MT 0 0 1 1
DD 0 1 2 2 DD 0 0 2 2
LP1 1 2 1 1 LP 1 0 0 1
CR 2 1 1 0 CR 0 1 1 0
Detection algorithm:
I. Single Instance of Each Resource Type
Maintain wait-for graph
o Nodes are processes.
o Pi Pj if Pi is waiting for Pj.
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.
Detection Algorithm
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i.
Therefore, NOT Deadlock.
State of system?
Detection-Algorithm Usage
When, and how often, to invoke deadlock detection?
Factors to consider:
How often a deadlock is likely to occur?
How many processes will need to be rolled back?
Approaches:
Execute the algorithm at predetermined time interval
Execute the algorithm whenever a resource request cannot be granted
immediately
Approaches:
I. Process Termination
Problems:
a. Show that the four necessary conditions for deadlock indeed hold in this example.
b. State a simple rule that will avoid deadlock in this system.
2. Suppose that a system is in an unsafe state. Show that it is possible for the processes to
complete their execution without entering a deadlock state.
3. Consider a system consisting of four resources of the same type that are shared by three
processes, each of which needs at most two resources. Show that the system is deadlock-
free?
5. Can a system be in a state that is neither deadlocked nor safe for a deadlock? If so, give an
example. If not, prove that all states are either deadlocked or safe?
6. Cinderella and the Prince are getting divorced. To divide their property, they have agreed on
the following algorithm. Every morning, each one may send a letter to the other’s lawyer
requesting one item of property. Since it takes a day for letters to be delivered, they agreed
that if both discover that they have requested the same item on the same day, the next day
they will send a letter canceling the request. Among their property is their dog (Woofer),
doghouse, their canary (Tweety) and Tweety’s cage. The animals love their houses, so it
has been agreed that any division of property separating an animal from its house is
invalid, requiring the whole division to start over from scratch. Both Cinderella and the
prince desperately want Woofer. So they can go on (separate) vacations, each spouse has
programmed a personal computer to handle the negotiation. When they came back from
vacation, the computers were still negotiating. Why? Is deadlock possible? Is starvation
possible? Discuss.