Jerry Breecher
7: Deadlocks
1
OPERATING SYSTEM Deadlocks
What Is In This Chapter?
• What is a deadlock? • Staying Safe: Preventing and Avoiding Deadlocks • Living Dangerously: Let the deadlock happen, then detect it and recover from it.
7: Deadlocks
2
DEADLOCKS
EXAMPLES:
• • "It takes money to make money". You can't get a job without experience; you can't get experience without a job.
BACKGROUND:
The cause of deadlocks: Each process needing what another process has. This results from sharing resources such as memory, devices, links. Under normal operation, a resource allocations proceed like this:: 1. 2. 3. Request a resource (suspend until available if necessary ). Use the resource. Release the resource.
7: Deadlocks
3
DEADLOCKS
Bridge Crossing Example
• • • • •
Traffic only in one direction. Each section of a bridge can be viewed as a resource. If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback). Several cars may have to be backed up if a deadlock occurs. Starvation is possible.
7: Deadlocks
4
DEADLOCKS
DEADLOCK CHARACTERISATION
NECESSARY CONDITIONS ALL of these four must happen simultaneously for a deadlock to occur: Mutual exclusion One or more than one resource must be held by a process in a non-sharable (exclusive) mode. Hold and Wait A process holds a resource while waiting for another resource. No Preemption There is only voluntary release of a resource - nobody else can make a process give up a resource. Circular Wait Process A waits for Process B waits for Process C .... waits for Process A.
7: Deadlocks 5
DEADLOCKS
RESOURCE ALLOCATION GRAPH
A visual ( mathematical ) way to determine if a deadlock has, or may occur. G = ( V, E ) The graph contains nodes and edges. V Nodes consist of processes = { P1, P2, P3, ...} and resource types { R1, R2, ...} Edges are ( Pi, Rj ) or ( Ri, Pj )
E
An arrow from the process