CS420: Operating Systems Deadlocks & Deadlock Prevention
CS420: Operating Systems Deadlocks & Deadlock Prevention
Deadlocks &
Deadlock Prevention
YORK COLLEGE OF PENNSYLVAN
MNK
YORK COLLEGE OF PENNSYLVANIA
JHG
'@OK
James Moscola
12
Department of Engineering & Computer Science
CS420: Operating Systems Based on Operating System Concepts, 9th Edition by Silberschatz, Galvin, Gagne
The Deadlock Problem
• Deadlock - A condition that arises when two or more processes are waiting
indefinitely for an event that can only be caused by one of the waiting
processes
- The events that the processes are waiting for will never happen :-(
- Other processes in the system that need the resources that are currently locked
will never get access to them
• Deadlock will become an even greater problem with the trend toward more
processor cores and more threads
• Starvation is possible
- Hold and wait - a process holding at least one resource is waiting to acquire
additional resources held by other processes
- Circular wait - there exists a set {P0, P1, …, Pn} of waiting processes such that P0
is waiting for a resource that is held by P1, P1 is waiting for a resource that is held
by P2, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting for a
resource that is held by P0
• P = {P1, P2, …, Pn}, the set consisting of all the processes in the system
• R = {R1, R2, …, Rm}, the set consisting of all resource types in the system
- P2 ➔ R3 ➔ P3 ➔ R2 ➔ P2
- Ignore the problem and pretend that deadlocks never occur in the system
(used by most operating systems, including UNIX)
• To ensure that deadlocks never occur, a system can use either deadlock-
prevention or deadlock-avoidance
• For deadlock to occur, each of the four necessary conditions must hold true
• To prevent deadlock, ensure that at least one of these four conditions does not
hold true
(1) Mutual Exclusion
• For deadlock to occur, each of the four necessary conditions must hold true
• To prevent deadlock, ensure that at least one of these four conditions does not hold
true
(2) Hold-and-wait
- One approach requires a process to request and be allocated all its resources
before it begins execution
• Low resource utilization because resources may allocated but unused for a
long time
- Another approach allows a process to request resources only when it has none
• For deadlock to occur, each of the four necessary conditions must hold true
• To prevent deadlock, ensure that at least one of these four conditions does not
hold true
(3) No Preemption of Resources
- All preempted resources are added to the list of resources for which the
process is waiting
- A process will be restarted only when it can regain its old resources, as
well as the new ones that it is requesting
• For deadlock to occur, each of the four necessary conditions must hold true
• To prevent deadlock, ensure that at least one of these four conditions does not
hold true
(4) Circular Wait
• In a safe state if the system can allocate resources to each process (up to its
maximum) in some order and still avoid deadlock
• System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the processes
in the systems such that for each Pi, the resources that Pi can still request can be
satisfied by currently available resources plus resources held by all the Pj, with j < i
• That is:
- If resources needed by Pi are not immediately available, then Pi can wait until all Pj have
finished
- When Pj is finished, Pi can obtain the resources it needs, execute, return allocated
resources, and terminate
• Two main avoidance algorithms ... choice depends on how many instances of
available resource types exist
- Single instance of a resource type
• Introduce a new type of edge to the resource-allocation graph, the claim edge
- A claim edge Pi → Rj indicates that process Pi may request a resource Rj
- Resources must be claimed a priori in the system, so all claim edges are known
• When a process gets all its resources it must return them in a finite amount of
time
- Max -- n x m matrix that defines the maximum demand of each process on each
resource type. If Max[i,j] = k, then process Pi may request at most k instances of
resource type Rj
If Finish[i] = true for all processes Pi for i=1 .. n, then the system is in a safe state
Otherwise, processes with a Finish[i] value of false are in an unsafe state and can
potentially deadlock
• The sequence <P1, P3, P0, P2, P4> is a safe sequence (there are other safe
sequences as well)
(2) If (Requesti > Availablei) then process Pi must wait because there are not currently sufficient resources
available to satisfy the request
• Since the system is in a safe state, use the Resource Request procedure to
determine if the following request can be granted
- P1 requests one instance of resource A, and two instances of resource C
The request vector looks like --> Request1 = (1, 0, 2)
- First note if the resources are available to fulfill this request: Request1 ≤ Available
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
• After determining that the resources are actually available, pretend to allocate
them to process P1
• Check to see if this new state is safe, if yes, then allocate resources for real
Allocation Max Need Available
A B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 3 3 2
P1 2 0 0 3 2 2 1 2 2 2 3 0
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
• Is this a safe state? The sequence <P1, P3, P4, P0, P2> is a safe sequence
CS420: Operating Systems 32
Example of Banker’s Algorithm - P1 Request (Cont.)
• If all resources have only a single instance, use a wait-for graph for deadlock
detection
- All Nodes are processes
• Deadlock exists in the system if and only if the wait-for graph contains a cycle
• Periodically invoke an algorithm that searches for a cycle in the graph. If there
is a cycle, there exists a deadlock
• The wait-for graph cannot be used in systems with multiple instances of each
resource type
• The deadlock detection algorithm for several instance contains similar data
structures to the Banker’s Algorithm
• Continuing the previous example, suppose that process P2 now requests one
additional resource of type C
• When, and how often, to invoke the detection algorithm depends on:
- How often a deadlock is likely to occur?
• If the detection algorithm is invoked every time a request for allocation cannot be
granted:
- The algorithm can detect specifically which processes caused the deadlock
• If the detection algorithm is invoked less frequently (e.g. once per hour):
- It is difficult to determine which processes caused the deadlock
• Once deadlock has been detected in a system there are several options for
dealing with deadlock
- Notify the system user and let them deal with it
- Have the system preempt resources from one or more of the deadlocked
processes
• Two options:
- Abort all deadlocked processes
• Consider:
- Priority of the process
- How long process has computed, and how much longer to completion
• Continually preempt the resources from processes and give those resources to
other processes until the system is no longer deadlocked
• Would like to return the process to some safe state without having to
completely restart the process. To do so requires some fancy bookkeeping
by the system to know where the most recent safe state was prior to the
deadlock.
• Don’t want resources to always get preempted from the same process