course overview principles of operating systems
DESCRIPTION
Introduction Computer System Structures Operating System Structures Processes Process Synchronization Deadlocks CPU Scheduling. Memory Management Virtual Memory File Management Security Networking Distributed Systems Case Studies Conclusions. - PowerPoint PPT PresentationTRANSCRIPT
Deadlocks 1 © 2000 Franz Kurfess
Course OverviewPrinciples of Operating Systems
Course OverviewPrinciples of Operating Systems
Introduction Computer System
Structures Operating System
Structures Processes Process Synchronization Deadlocks CPU Scheduling
Memory Management Virtual Memory File Management Security Networking Distributed Systems Case Studies Conclusions
Deadlocks 2 © 2000 Franz Kurfess
Chapter Overview Deadlock and Starvation
Chapter Overview Deadlock and Starvation
Motivation Objectives Deadlock Principles Starvation Deadlock Conditions
Mutual Exclusion Hold and Wait No Preemption Circular Wait
Dealing with Deadlock Deadlock Prevention Deadlock Avoidance Deadlock Detection and
Recovery
Deadlock and Operating Systems
Important Concepts and Terms
Chapter Summary
Deadlocks 3 © 2000 Franz Kurfess
MotivationMotivation
there is the potential of conflicts for processes who cooperate or share resources
deadlock and starvation are the most severe conflicts, leading to the permanent halting of the processes affected
common practice is not satisfactory ad hoc solutions neglecting the problem
Deadlocks 4 © 2000 Franz Kurfess
ObjectivesObjectives
recognize potential problems with the coordination of activities for several processes (cooperation or resource sharing)
understand the necessary conditions for a formal deadlock situation
distinguish deadlocks from other reasons for “frozen” systems
analyze the status of a system based on resource allocation graphs
apply deadlock prevention, avoidance, and detection principles and algorithms to sets of processes
Deadlocks 5 © 2000 Franz Kurfess
“Deadlocked” Systems“Deadlocked” Systems
the term “deadlock” is often used casually to indicate that the computer system doesn’t respond (it is “frozen”)
there are other reasons for a system to be in such a state endless loop waiting for an event that may never occur synchronization between processes
deadlock needs to be defined carefully to distinguish it from these other reasons
Deadlocks 6 © 2000 Franz Kurfess
Deadlock PrinciplesDeadlock Principles
permanent mutual blocking of a set of resources that either compete for resources or cooperate on a task
deadlocked processes don’t make any progress, and never finish their execution
deadlocked processes may tie up system resources
Deadlocks 7 © 2000 Franz Kurfess
StarvationStarvation
a process can’t proceed because other processes always have the resources it needs
the request of the process is never satisfied in principle, it is possible to get the resource, but
doesn’t happen because of low priority of the process timing of resource requests ill-designed resource allocation or scheduling algorithm
different from deadlock
Deadlocks 8 © 2000 Franz Kurfess
Examples StarvationExamples Starvation
batch processes with low priority in a heavily used time-sharing system
crossing a very busy road trying to call a very popular phone number
NJIT modem pool radio station giveaways
getting into a very popular course with limited enrollment
Deadlocks 9 © 2000 Franz Kurfess
Solution StarvationSolution Starvation
fairness: each process gets its fair share of all resources it requests how is fair defined? how is it enforced?
aging the priority of a request is increased the longer the
process waits for it
Deadlocks 10 © 2000 Franz Kurfess
Resource TypesResource Types
reusable resources can be used repeatedly by different processes does not imply simultaneous use OS examples: CPU, main memory, I/O devices, data
structures
consumable resources are produced by one entity, and consumed by another reuse is not possible, or impractical OS examples: messages
Deadlocks 11 © 2000 Franz Kurfess
Example Reusable ResourcesExample Reusable Resources main memory allocation
two processes make successive requests for main memory
the overall memory size is 16 MByte
Process A
request 5 MBytes;
request 8 MBytes;
Process B
request 7 MBytes;
request 7 MBytes;
deadlock no process can proceed unless one gives up some
memory (preemption) frequent solutions: preallocation, virtual memory
Deadlocks 12 © 2000 Franz Kurfess
Example Consumable ResourcesExample Consumable Resources message passing
two processes wait for a message from each other, and then send one to each other
receive operation is blocking (process can’t continue)
Process A
receive (B);
send (B);
Process B
receive (A);
send(A);
deadlock no process can proceed because it is waiting for a
message from the other no easy solution
Deadlocks 13 © 2000 Franz Kurfess
Deadlock on ResourcesDeadlock on Resources
usually based on design or programming errors these errors may be difficult or impossible to avoid
interaction between different modules interaction between independent programs
very difficult to detect may not occur during testing may occur very infrequently in the field
Deadlocks 14 © 2000 Franz Kurfess
Resource Allocation GraphsResource Allocation Graphs processes
hold, request and release resources resources
resource types specify a class of resources individual instances are identical from the process’ perspective
resource assignment an instance of a resource is currently held by a process indicated by an assignment edge
resource request a process wants to acquire another instance of a resource indicated by a request edge
Deadlocks 15 © 2000 Franz Kurfess
Resource Allocation GraphsResource Allocation Graphs
R1 R2 R3
R4 R5 R6 R7
P1 P2 P3 P4 P5
AssignmentEdge
RequestEdge
Deadlocks 16 © 2000 Franz Kurfess
Resource Allocation Graph with Cycle
Resource Allocation Graph with Cycle
R1
R2 R3
P1 P2
there is at least one cycle in the graph
processes and resources involved e.g.
P1 -> R1 -> P2 -> R1, P1 -> R1 -> P2 -> R3 -> P1
is there a deadlock? no, we have enough resources
to satisfy all requests
Deadlocks 17 © 2000 Franz Kurfess
Resource Allocation Graph with Deadlock
Resource Allocation Graph with Deadlock
R1
R2 R3
P1 P2
there must be at least one cycle in the graph cycles are candidates for
deadlock is there a deadlock? yes, there are not enough
resources in R2 to satisfy all requests by P1 and P2
processes and resources involved P1 -> R2 -> P2 -> R2
Deadlocks 18 © 2000 Franz Kurfess
Cycles and DeadlocksCycles and Deadlocks
if there is a cycle in the resource allocation graph, there may be a deadlock cycles without deadlock are possible
a deadlock can only occur if there is a cycle no deadlock without a cycle
Deadlocks 19 © 2000 Franz Kurfess
Safe StatesSafe States
a system is in a safe state if resources can be allocated to processes without getting into a deadlock the order in which resources are allocated must be flexible
a sequence of processes is a safe sequence if the resources that a process may request are satisfiable by the currently available resources plus resources held by other processes this must hold for all processes in the system the process may have to wait until other processes are
finished
Deadlocks 20 © 2000 Franz Kurfess
Safe State SpaceSafe State Space
if a system is in a safe state there are no deadlocks in an unsafe state, there is a possibility of deadlocks
Deadlockunsafe
safe
Deadlocks 21 © 2000 Franz Kurfess
Dining Philosophers, AgainDining Philosophers, Again
R3
P0
P1
P2P3
P4
R2
R1
R4R0
var chopstick: array [0..4] of semaphore; repeat wait(chopstick[i]); wait(chopstick[i + 1 mod 5]); ... -- eat ... signal(chopstick[i]); signal(chopstick[i + 1 mod 5]); ... -- think ...until false;
Deadlocks 22 © 2000 Franz Kurfess
Why Deadlock?Why Deadlock? reasons for potential deadlock
mutual exclusion two philosophers cannot use a chopstick simultaneously
use a protocol that prevents philosophers from picking up their chopsticks simultaneously picking up one chopstick if the other is not available
supervisor philosopher an authority that can tell philosophers what to do may use preemption of resources
seating arrangement don’t seat philosophers around a round table
may require more n+1 chopsticks for n philosophers
several conditions must hold simultaneously
Deadlocks 23 © 2000 Franz Kurfess
Conditions for DeadlockConditions for Deadlock
all four conditions must hold simultaneously mutual exclusion hold and wait no preemption circular wait
the term deadlock is often used sloppily in situations that don’t constitute a formal deadlock as described above
Deadlocks 24 © 2000 Franz Kurfess
Mutual ExclusionMutual Exclusion
only one process can hold a resource at one point at least one resource may be used in a mutually
exclusive way only requests by other processes are delayed usually determined by the needs of the resource, not
under the control of the OS examples: CPU, printer, write on files
Deadlocks 25 © 2000 Franz Kurfess
Hold and WaitHold and Wait
one process holds a resource and waits to acquire other resources currently held by other processes
the processes doesn’t release its current resources may be necessary, or a consequence of bad
program design examples: memory segments, I/O devices,
chopsticks
Deadlocks 26 © 2000 Franz Kurfess
No PreemptionNo Preemption
a resource is released by a process only voluntarily no intervention by the OS or other processes examples: “cooperative multitasking” (MacOS)
Deadlocks 27 © 2000 Franz Kurfess
Circular WaitCircular Wait
each process holds at least one resources needed by another process
the set of processes together with the request and hold links forms a cycle there must be a set of processes
P0, P1, ..., PN such that Pi is waiting on Pi+1
and PN is waiting on P0
examples: dining philosophers, memory requests
Deadlocks 28 © 2000 Franz Kurfess
Deadlock ExamplesDeadlock Examples
examples studying students traffic intersection narrow tunnel single-track railroad airline reservation system
evaluation four conditions: mutual exclusion, hold and wait, no
preemption, circular wait
Deadlocks 29 © 2000 Franz Kurfess
Studying StudentsStudying Students
Student A
get coursenotes
get textbook
study
release textbook
release coursenotes
Student B
get textbook
get coursenotes
study
release coursenotes
release textbook
studying students: both students need the textbook and the course notes to study, but there is only one copy of each
consider the following situation:
Deadlocks 30 © 2000 Franz Kurfess
Students EvaluationStudents Evaluation
mutual exclusion books and course notes can be used only by one student
hold and wait a student who has the book waits for the course notes, or
vice versa no preemption
there is no authority to take away book or course notes from a student
circular wait student A waits for resources held by student B, who waits
for resources held by A
Deadlocks 31 © 2000 Franz Kurfess
Traffic IntersectionTraffic Intersection
at a four-way intersection, four cars arrive simultaneously
if all proceed, they will be stuck in the middle
Deadlocks 32 © 2000 Franz Kurfess
Traffic EvaluationTraffic Evaluation
mutual exclusion cars can’t pass each other in the intersection
hold and wait vehicles proceed to the center, and wait for their path to
be clear
no preemption there is no authority to remove some vehicles
circular wait vehicle 1 waits for vehicle 2 to move, which waits for 3,
which waits for 4, which waits for 1
Deadlocks 33 © 2000 Franz Kurfess
Narrow TunnelNarrow Tunnel
in a narrow tunnel, there is enough room for most vehicles to pass each other
if two large trucks arrive simultaneously, they won’t be able to pass each other
Deadlocks 34 © 2000 Franz Kurfess
Tunnel EvaluationTunnel Evaluation
mutual exclusion trucks can’t pass each other in the tunnel
hold and wait trucks occupy part of the tunnel and wait for their path to
become clear
no preemption there is no authority to remove the trucks
circular wait truck 1 waits for truck 2 to move, which waits for 1
Deadlocks 35 © 2000 Franz Kurfess
Single-Track RailroadSingle-Track Railroad
two trains arrive from different directions on a single-track section
railroad evaluation mutual exclusion
trains can’t pass each other on the single track
hold and wait trains occupy part of the track and wait for their path to become clear
no preemption there is no authority to remove the trains
circular wait train 1 waits for train 2 to move, which waits for 1
Deadlocks 36 © 2000 Franz Kurfess
Airline Reservation SystemAirline Reservation System
two agents book flights in from EWR to FRA to MUC agent A books flights EWR to FRA first agent B books FRA to MUC first
flight records are locked during booking
Agent A
lock EWR-FRA
lock FRA-MUC
-- finalize booking
release FRA-MUC
release EWR-FRA
Agent B
lock FRA-MUC
lock EWR-FRA
-- finalize booking
release EWR-FRA
release FRA-MUC
Deadlocks 37 © 2000 Franz Kurfess
Reservation EvaluationReservation Evaluation
mutual exclusion flight records are locked
hold and wait one agent locks one segment and waits for the other to
become available
no preemption there is no authority to remove the locks held by an agent
circular wait agent A waits for agent B to unlock a segment, who waits
for A
Deadlocks 38 © 2000 Franz Kurfess
Dealing with DeadlocksDealing with Deadlocks
protocol have strict rules that prevent a deadlock from occurring
recover allow the system to enter a deadlock there must be a way to recover from the deadlock
situation
ignoring the deadlock frequently used in operating systems
Windows, MacOS, Unix, ...
Deadlocks 39 © 2000 Franz Kurfess
Dealing with DeadlocksDealing with Deadlocks
deadlock prevention based on protocols
deadlock avoidance check for safe state
deadlock detection and recovery run into a deadlock, detect it, and recover
Deadlocks 40 © 2000 Franz Kurfess
Deadlock PreventionDeadlock Prevention
set of rules ensures that at least one of the four necessary conditions for deadlock doesn’t hold mutual exclusion hold and wait no preemption circular wait
may result in low resource utilization, reduced system throughput
example: traffic lights, no trucks in the narrow tunnel
Deadlocks 41 © 2000 Franz Kurfess
Denying Mutual ExclusionDenying Mutual Exclusion
if all resources were sharable there would not be any deadlock
some resources can be made sharable for example, spooling printer output
some resources are intrinsically non-sharable e.g. CPU (at the instruction time scale)
Deadlocks 42 © 2000 Franz Kurfess
Denying Hold And WaitDenying Hold And Wait
prevent processes that hold resources from waiting for more resources processes request and are allocated all their resources
before they start executing low resource utilization rate
processes can request resources only if they have none indefinite postponement
Deadlocks 43 © 2000 Franz Kurfess
Denying No PreemptionDenying No Preemption
means that processes may be preempted by the OS should only done when necessary
resources of a process trying to acquire another unavailable resource may be preempted
preempt resources of processes waiting for additional resources, and give some to the requesting process
possible only for some types of resources state must be easily restorable e.g. CPU, memory
frequently used when applicable
Deadlocks 44 © 2000 Franz Kurfess
Denying Circular WaitDenying Circular Wait
break the cycle of waiting processes impose a total ordering of all resource types resources may only requested in increasing order of
enumeration no process can request a resource with a lower number than what
it is already holding
ordering should take typical order of usage into account
Deadlocks 45 © 2000 Franz Kurfess
Safe PhilosophersSafe Philosophers
solution based on monitors deadlock prevention is used by designing the program
with certain restrictions
basic idea a philosopher picks up a chopstick only if the other one is
available as well philosophers inform each other when they are done
Deadlocks 46 © 2000 Franz Kurfess [Silberschatz & Galvin, 1998]
Philosophers with MonitorsPhilosophers with Monitorstype dining-philosophers = monitor var state: array [0..4] of (thinking, hungry, eating);var self: array [0..4] of condition;
procedure entry pickup(i: 0..4); begin state[i] := hungry; test(i); if state[i] <> eating then self[i].wait; end;
procedure entry putdown(i: 0..4); begin state[i] := thinking; test(i-1 mod 5); test(i+1 mod 5);end;
Deadlocks 47 © 2000 Franz Kurfess [Silberschatz & Galvin, 1998]
Philosophers with Monitors (cont.)Philosophers with Monitors (cont.)
procedure test(k: 0..4); begin if (state[k-1] mod 5 <> eating and state[k] = hungry and state[k+1] mod 5 <> eating) then begin state[k] := eating; self[k].signal; end; end;
begin for i := 0 to 4 do state[i] := thinking;end;
Deadlocks 48 © 2000 Franz Kurfess
Deadlock AvoidanceDeadlock Avoidance
makes sure the system stays in a safe state if a request is satisfied
prevents circular waits requires additional information at the beginning of
the execution of a process maximum number of instances per resource type for each
process
Deadlocks 49 © 2000 Franz Kurfess
Safe StateSafe State
A system is in a safe state when given the current allocation the system is able to handle any number of requests, in some order, without getting into a deadlock.
Deadlocks 50 © 2000 Franz Kurfess
ExampleExample Bank gives loans to customers
maximum allocation = credit limit
BANK$10
A B C
$5 $7 $3
Maximum Allocation
Deadlocks 51 © 2000 Franz Kurfess
BANK$2
A $3 B $4 C $1
$5 $7 $3
Maximum Allocation
Current Allocation
Safe State? Will the bank be able to give each customer a loan up to the full credit limit?
not necessarily all customers simultaneously order is not important customers will pay back their loan once their credit limit is reached
Deadlocks 52 © 2000 Franz Kurfess
BANK$1
A $3 B $5 C $1
$5 $7 $3
Maximum Allocation
Current Allocation
Still Safe? after customer B requests and is granted $1, is the bank
still safe?
Deadlocks 53 © 2000 Franz Kurfess
Safe unsafe
deadlock
Safe State SpaceSafe State Space
Deadlocks 54 © 2000 Franz Kurfess
Safeunsafe
deadlock
(3,4,1)x
(3,5,1)x
Bank Safe State SpaceBank Safe State Space
Deadlocks 55 © 2000 Franz Kurfess
Banker’s AlgorithmBanker’s Algorithm
before a request is granted, check the system’s state assume the request is granted if it is still safe, the request can be honored otherwise the process has to wait overly careful
there are cases when the system is unsafe, but not in a deadlock
Deadlocks 56 © 2000 Franz Kurfess
Example Banker’s AlgorithmExample Banker’s Algorithm
Current MaximumProcess Allocation Allocation Available
A B C D E A B C D E A B C D EP0 1 0 1 0 0 2 0 2 1 3 1 0 1 0 1P1 1 1 0 1 0 1 2 1 2 2P2 0 0 0 0 1 2 1 3 1 2P3 0 1 1 1 1 0 1 2 1 2P4 0 0 1 0 0 1 1 1 2 2
initially given: current allocation, maximum allocation, available resources
Deadlocks 57 © 2000 Franz Kurfess
Current NeededProcess Allocation Allocation Available
A B C D E A B C D E A B C D EP0 1 0 1 0 0 1 0 1 1 3 1 0 1 0 1P1 1 1 0 1 0 0 1 1 1 2 1 1 2 1 2P2 0 0 0 0 1 2 1 3 1 1P3 0 1 1 1 1 0 0 1 0 1P4 0 0 1 0 0 1 1 0 2 2
<P3>
needed allocation is calculated by subtracting the current allocation from the maximum allocation
Deadlocks 58 © 2000 Franz Kurfess
<P3, P1>
Current NeededProcess Allocation Allocation Available
A B C D E A B C D E A B C D EP0 1 0 1 0 0 1 0 1 1 3 1 0 1 0 1P1 1 1 0 1 0 0 1 1 1 2 1 1 2 1 2P2 0 0 0 0 1 2 1 3 1 1 2 2 2 2 2P3 0 1 1 1 1 0 0 1 0 1P4 0 0 1 0 0 1 1 0 2 2
available resources are increased because processes that are finished return their resources
Deadlocks 59 © 2000 Franz Kurfess
<P3, P1, P4>
Current NeededProcess Allocation Allocation Available
A B C D E A B C D E A B C D EP0 1 0 1 0 0 1 0 1 1 3 1 0 1 0 1P1 1 1 0 1 0 0 1 1 1 2 1 1 2 1 2P2 0 0 0 0 1 2 1 3 1 1 2 2 2 2 2P3 0 1 1 1 1 0 0 1 0 1 2 2 3 2 2P4 0 0 1 0 0 1 1 0 2 2
Deadlocks 60 © 2000 Franz Kurfess
<P3, P1, P4, P2>
Current NeededProcess Allocation Allocation Available
A B C D E A B C D E A B C D EP0 1 0 1 0 0 1 0 1 1 3 1 0 1 0 1P1 1 1 0 1 0 0 1 1 1 2 1 1 2 1 2P2 0 0 0 0 1 2 1 3 1 1 2 2 2 2 2P3 0 1 1 1 1 0 0 1 0 1 2 2 3 2 2P4 0 0 1 0 0 1 1 0 2 2 2 2 3 2 3
Deadlocks 61 © 2000 Franz Kurfess
<P3, P1, P4, P2, P0>
Current NeededProcess Allocation Allocation Available
A B C D E A B C D E A B C D EP0 1 0 1 0 0 1 0 1 1 3 1 0 1 0 1P1 1 1 0 1 0 0 1 1 1 2 1 1 2 1 2P2 0 0 0 0 1 2 1 3 1 1 2 2 2 2 2P3 0 1 1 1 1 0 0 1 0 1 2 2 3 2 2P4 0 0 1 0 0 1 1 0 2 2 2 2 3 2 3 3 2 4 2 3
if there is a sequence in which all processes get their needed allocation, the system is in a safe state
Deadlocks 62 © 2000 Franz Kurfess
Current NeededProcess Allocation Allocation Available
A B C D E A B C D E A B C D EP0 2 0 1 0 0 0 0 1 1 3 0 0 1 0 1P1 1 1 0 1 0 0 1 1 1 2P2 0 0 0 0 1 2 1 3 1 1P3 0 1 1 1 1 0 0 1 0 1P4 0 0 1 0 0 1 1 0 2 2
What if P0 requests and is granted one instance of resource A?
adjust current and needed allocation, available resources
Deadlocks 63 © 2000 Franz Kurfess
Current NeededProcess Allocation Allocation Available
A B C D E A B C D E A B C D EP0 2 0 1 0 0 0 0 1 1 3 0 0 1 0 1P1 1 1 0 1 0 0 1 1 1 2 0 1 2 1 2P2 0 0 0 0 1 2 1 3 1 1P3 0 1 1 1 1 0 0 1 0 1P4 0 0 1 0 0 1 1 0 2 2
<P3>
Deadlocks 64 © 2000 Franz Kurfess
Current NeededProcess Allocation Allocation Available
A B C D E A B C D E A B C D EP0 2 0 1 0 0 0 0 1 1 3 0 0 1 0 1P1 1 1 0 1 0 0 1 1 1 2 0 1 2 1 2P2 0 0 0 0 1 2 1 3 1 1 1 2 2 2 2P3 0 1 1 1 1 0 0 1 0 1P4 0 0 1 0 0 1 1 0 2 2
<P3, P1>
Deadlocks 65 © 2000 Franz Kurfess
Current NeededProcess Allocation Allocation Available
A B C D E A B C D E A B C D EP0 2 0 1 0 0 0 0 1 1 3 0 0 1 0 1P1 1 1 0 1 0 0 1 1 1 2 0 1 2 1 2P2 0 0 0 0 1 2 1 3 1 1 1 2 2 2 2P3 0 1 1 1 1 0 0 1 0 1 1 2 3 2 2P4 0 0 1 0 0 1 1 0 2 2
<P3, P1, P4>
Deadlocks 66 © 2000 Franz Kurfess
Current NeededProcess Allocation Allocation Available
A B C D E A B C D E A B C D EP0 2 0 1 0 0 0 0 1 1 3 0 0 1 0 1P1 1 1 0 1 0 0 1 1 1 2 0 1 2 1 2P2 0 0 0 0 1 2 1 3 1 1 1 2 2 2 2P3 0 1 1 1 1 0 0 1 0 1 1 2 3 2 2P4 0 0 1 0 0 1 1 0 2 2
<P3, P1, P4> Conflict!
Deadlocks 67 © 2000 Franz Kurfess
Example 1 Banker’s AlgorithmExample 1 Banker’s Algorithm
Current MaximumProcess Allocation Allocation Available
A B C D A B C D A B C D P0 0 2 0 1 1 2 0 1 1 0 0 1 P1 1 1 3 0 1 2 3 2 P2 0 0 1 0 0 0 3 0P3 1 2 0 1 2 3 0 1 P4 0 0 0 0 0 0 0 1P5 1 1 1 1 3 3 4 1
initially given: current allocation, maximum allocation, available resources
Deadlocks 68 © 2000 Franz Kurfess
Ex. 1 Resource Allocation GraphEx. 1 Resource Allocation Graph
C
BA
AssignmentEdge
RequestEdge
P0 P1 P2 P3 P4 P5
D
B
Deadlocks 69 © 2000 Franz Kurfess
Current NeededProcess Allocation Allocation Available
A B C D A B C D A B C D P0 0 2 0 1 1 0 0 0 1 0 0 1 P1 1 1 3 0 0 1 0 2P2 0 0 1 0 0 0 2 0P3 1 2 0 1 1 1 0 0 P4 0 0 0 0 0 0 0 1P5 1 1 1 1 2 2 3 0
Example 1 (cont.)Example 1 (cont.)
needed allocation is calculated by subtracting the current allocation from the maximum allocation all calculations are done for each element of the vector
Deadlocks 70 © 2000 Franz Kurfess
Current NeededProcess Allocation Allocation Available
A B C D A B C D A B C D P0 0 2 0 1 1 0 0 0 1 0 0 1 P1 1 1 3 0 0 1 0 2P2 0 0 1 0 0 0 2 0P3 1 2 0 1 1 1 0 0 P4 0 0 0 0 0 0 0 1P5 1 1 1 1 2 2 3 0
Example 1 (cont.)Example 1 (cont.)
<P0>
select a process whose request (needed allocation) could be satisfied with the available resources there may be several candidates (e.g. P0, P4)
Deadlocks 71 © 2000 Franz Kurfess
Current NeededProcess Allocation Allocation Available
A B C D A B C D A B C D P0 0 2 0 1 1 0 0 0 1 0 0 1 P1 1 1 3 0 0 1 0 2 1 2 0 2P2 0 0 1 0 0 0 2 0P3 1 2 0 1 1 1 0 0 P4 0 0 0 0 0 0 0 1P5 1 1 1 1 2 2 3 0
Example 1 (cont.)Example 1 (cont.)
<P0>
the resources held by this process are added to the available resources because processes that are finished return their resources
Deadlocks 72 © 2000 Franz Kurfess
Current NeededProcess Allocation Allocation Available
A B C D A B C D A B C D P0 0 2 0 1 1 0 0 0 1 0 0 1 P1 1 1 3 0 0 1 0 2 1 2 0 2P2 0 0 1 0 0 0 2 0P3 1 2 0 1 1 1 0 0 P4 0 0 0 0 0 0 0 1P5 1 1 1 1 2 2 3 0
Example 1 (cont.)Example 1 (cont.)
<P0, P3>
the next process is selected the previous process is marked as already checked
Deadlocks 73 © 2000 Franz Kurfess
Current NeededProcess Allocation Allocation Available
A B C D A B C D A B C D P0 0 2 0 1 1 0 0 0 1 0 0 1 P1 1 1 3 0 0 1 0 2 1 2 0 2P2 0 0 1 0 0 0 2 0 2 4 0 3P3 1 2 0 1 1 1 0 0 P4 0 0 0 0 0 0 0 1P5 1 1 1 1 2 2 3 0
Example 1 (cont.)Example 1 (cont.)
<P0, P3>
and its resources are added to the available resources
Deadlocks 74 © 2000 Franz Kurfess
Current NeededProcess Allocation Allocation Available
A B C D A B C D A B C D P0 0 2 0 1 1 0 0 0 1 0 0 1 P1 1 1 3 0 0 1 0 2 1 2 0 2P2 0 0 1 0 0 0 2 0 2 4 0 3P3 1 2 0 1 1 1 0 0 3 5 3 3P4 0 0 0 0 0 0 0 1P5 1 1 1 1 2 2 3 0
Example 1 (cont.)Example 1 (cont.)
<P0, P3, P1>
the next process is selected, and its resources are added to the available resources
Deadlocks 75 © 2000 Franz Kurfess
Current NeededProcess Allocation Allocation Available
A B C D A B C D A B C D P0 0 2 0 1 1 0 0 0 1 0 0 1 P1 1 1 3 0 0 1 0 2 1 2 0 2P2 0 0 1 0 0 0 2 0 2 4 0 3P3 1 2 0 1 1 1 0 0 3 5 3 3P4 0 0 0 0 0 0 0 1 3 5 4 3P5 1 1 1 1 2 2 3 0
Example 1 (cont.)Example 1 (cont.)
<P0, P3, P1, P2>
the next process is selected, and its resources are added to the available resources
Deadlocks 76 © 2000 Franz Kurfess
Current NeededProcess Allocation Allocation Available
A B C D A B C D A B C D P0 0 2 0 1 1 0 0 0 1 0 0 1 P1 1 1 3 0 0 1 0 2 1 2 0 2P2 0 0 1 0 0 0 2 0 2 4 0 3P3 1 2 0 1 1 1 0 0 3 5 3 3P4 0 0 0 0 0 0 0 1 3 5 4 3P5 1 1 1 1 2 2 3 0 3 5 4 3
Example 1 (cont.)Example 1 (cont.)
<P0, P3, P1, P2, P4>
the next process is selected, and its resources are added to the available resources
Deadlocks 77 © 2000 Franz Kurfess
Current NeededProcess Allocation Allocation Available
A B C D A B C D A B C D P0 0 2 0 1 1 0 0 0 1 0 0 1 P1 1 1 3 0 0 1 0 2 1 2 0 2P2 0 0 1 0 0 0 2 0 2 4 0 3P3 1 2 0 1 1 1 0 0 3 5 3 3P4 0 0 0 0 0 0 0 1 3 5 4 3P5 1 1 1 1 2 2 3 0 3 5 4 3 4 6 5 4
Example 1 (cont.)Example 1 (cont.)
<P0, P3, P1, P2, P4, P5>
the last process (P5) is selected, and its resources are added to the available resources
Deadlocks 78 © 2000 Franz Kurfess
Outcome Example 1Outcome Example 1
all processes have been checked successfully there is at least one sequence in which the requests
of the processes can be satisfied safely the final value for the “available” vector indicates the
total number of resources
Deadlocks 79 © 2000 Franz Kurfess
Example 2 Banker’s AlgorithmExample 2 Banker’s Algorithm
Current MaximumProcess Allocation Allocation Available
A B C D A B C D A B C D P0 0 2 0 1 1 2 0 1 0 0 0 1 P1 1 1 3 0 1 2 3 2 P2 0 0 1 0 0 0 3 0P3 2 2 0 1 2 3 0 1 P4 0 0 0 0 0 0 0 1P5 1 1 1 1 3 3 4 1
slight modification of the previous example: P3 holds 2 2 0 1 instead of 1 2 0 1 this changes Current Allocation, Needed Allocation, and Available
Deadlocks 80 © 2000 Franz Kurfess
Ex. 2 Resource Allocation GraphEx. 2 Resource Allocation Graph
C
BA
AssignmentEdge
RequestEdge
P0 P1 P2 P3 P4 P5
D
B
Deadlocks 81 © 2000 Franz Kurfess
Current NeededProcess Allocation Allocation Available
A B C D A B C D A B C D P0 0 2 0 1 1 0 0 0 0 0 0 1P1 1 1 3 0 0 1 0 2P2 0 0 1 0 0 0 2 0P3 2 2 0 1 0 1 0 0 P4 0 0 0 0 0 0 0 1P5 1 1 1 1 2 2 3 0
Example 2 (cont.)Example 2 (cont.)
needed allocation is calculated by subtracting the current allocation from the maximum allocation all calculations are done for each element of the vector
Deadlocks 82 © 2000 Franz Kurfess
Current NeededProcess Allocation Allocation Available
A B C D A B C D A B C D P0 0 2 0 1 1 0 0 0 0 0 0 1P1 1 1 3 0 0 1 0 2P2 0 0 1 0 0 0 2 0P3 2 2 0 1 0 1 0 0 P4 0 0 0 0 0 0 0 1P5 1 1 1 1 2 2 3 0
Example 2 (cont.)Example 2 (cont.)
<P4>
select a process whose request (needed allocation) could be satisfied with the available resources
Deadlocks 83 © 2000 Franz Kurfess
Current NeededProcess Allocation Allocation Available
A B C D A B C D A B C D P0 0 2 0 1 1 0 0 0 0 0 0 1P1 1 1 3 0 0 1 0 2 0 0 0 1P2 0 0 1 0 0 0 2 0P3 2 2 0 1 0 1 0 0 P4 0 0 0 0 0 0 0 1P5 1 1 1 1 2 2 3 0
Example 2 (cont.)Example 2 (cont.)
<P4>
the resources held by this process are added to the available resources because processes that are finished return their resources
Deadlocks 84 © 2000 Franz Kurfess
Current NeededProcess Allocation Allocation Available
A B C D A B C D A B C D P0 0 2 0 1 1 0 0 0 0 0 0 1P1 1 1 3 0 0 1 0 2 0 0 0 1P2 0 0 1 0 0 0 2 0P3 2 2 0 1 0 1 0 0 P4 0 0 0 0 0 0 0 1P5 1 1 1 1 2 2 3 0
Example 2 (cont.)Example 2 (cont.)
<P4>
the next process is selected the previous process is marked as already checked
Conflict!
Deadlocks 85 © 2000 Franz Kurfess
Outcome Example 2Outcome Example 2
not all processes could be checked successfully there is no sequence in which the requests of the
processes can be satisfied safely the system is in an unsafe state involving all
processes except P4
Deadlocks 86 © 2000 Franz Kurfess
Detection and RecoveryDetection and Recovery
the system must provide an algorithm to examine whether a deadlock has occurred recover from the deadlock
requires run-time overhead due to maintaining information and executing a detection
algorithm the potential losses inherent in recovery
Deadlocks 87 © 2000 Franz Kurfess
Detection: Single InstanceDetection: Single Instance
all resource types have only one instance a cycle in the resource allocation graph means there
is a deadlock a cycle detection algorithm requires an order of
n2 operations for n nodes in the graph (processes + resources)
Deadlocks 88 © 2000 Franz Kurfess
Resource Allocation GraphsResource Allocation Graphs
R1 R2 R3
R4 R5 R6 R7
P1 P2 P3 P4 P5
AssignmentEdge
RequestEdge
Deadlocks 89 © 2000 Franz Kurfess
Detection: Multiple InstancesDetection: Multiple Instances
there are several instances for each resource type an algorithm similar to deadlock avoidance (banker’s
algorithm) is used to determine whether the system is deadlocked requires order of m * n2 operations
(m = no. of resource types, n = no. of processes)
how often the algorithm is invoked depends on the likelihood for deadlock to occur the number of processes affected by deadlock
Deadlocks 90 © 2000 Franz Kurfess
RecoveryRecovery
resume operation after a deadlock has been detected preemption rollback killing processes
Deadlocks 91 © 2000 Franz Kurfess
Ignoring the ProblemIgnoring the Problem
simplest approach better performance and less restrictive system problem: possibility of occasional deadlocks example: Unix
Deadlocks 92 © 2000 Franz Kurfess
Deadlock in Operating SystemsDeadlock in Operating Systems
combined approach resources are partitioned into hierarchically ordered
classes a resource-ordering technique is applied to the classes within each class, one particular approach to deadlocks
can be used guarantees that a deadlock cannot involve more than one
class, and the whole system is safe
Deadlocks 93 © 2000 Franz Kurfess
Example Combined ApproachExample Combined Approach
classes of resources system resources
used by the OS
user memory memory used by a user process
process resources resources available to processes
devices, files
swap space space for processes on a background storage device
Deadlocks 94 © 2000 Franz Kurfess
Solution Combined ApproachSolution Combined Approach
resource ordering between the four classes individual approaches within each class
system resources prevention: resource ordering within the class
user memory preemption: swap a process to secondary storage
process resources deadlock avoidance: processes inform the OS in advance about
resources to be requested
swap space prevention: all required resources are allocated at the same time
(preallocation)
Deadlocks 95 © 2000 Franz Kurfess
Important Concepts and TermsImportant Concepts and Terms Banker’s algorithm circular wait condition deadlock deadlock avoidance deadlock detection deadlock prevention dining philosophers hold and wait condition instance monitor mutual exclusion condition
no preemption condition process preemption recovery resource resource allocation resource allocation graph resource type safe state starvation synchronization termination
Deadlocks 96 © 2000 Franz Kurfess
Summary DeadlocksSummary Deadlocks
deadlock is a specific situation characterized by four criteria mutual exclusion, hold and wait, no preemption, circular wait
it prevents affected processes from continuing their execution
deadlock can be dealt with by prevention, avoidance, detection, or by ignoring the problem
various algorithms based on resource allocation graphs e.g. banker’s algorithm