Deadlocks
Deadlocks
Deadlocks
Learning Objectives
Several causes of system deadlock The difference between preventing and avoiding deadlocks How to detect and recover from deadlocks The concept of process starvation and how to detect and recover from it The concept of a race and how to prevent it The difference between deadlock, starvation, and race
Deadlock
Resource sharing
Memory management and processor sharing
Many programs competing for limited resources Lack of process synchronization consequences
Deadlock: deadly embrace
Two or more jobs placed in HOLD state Jobs waiting for unavailable vital resource System comes to standstill Resolved via external intervention
Starvation
Infinite postponement of job
Understanding Operating Systems, Fifth Edition 3
Deadlock (continued)
More serious than starvation Affects entire system
Affects more than one job
Not just a few programs
Example: traffic jam (Figure 5.1) More prevalent in interactive systems Real-time systems
Deadlocks quickly become critical situations
Deadlock (continued)
Deadlock remains
Until one program withdrawn or Until one program forcibly removed and file released
11
Deadlock sequence
P1 requests tape drive 1 and gets it P2 requests tape drive 2 and gets it P1 requests tape drive 2 but blocked P2 requests tape drive 1 but blocked
12
14
Spooling
Process
Disk accepts output from several users Acts as temporary storage for output Output resides in disk area until printer accepts job data
Understanding Operating Systems, Fifth Edition 15
16
Direction of arrows
Indicates message flow
Deadlock sequence
All available buffer space fills
17
18
Deadlock sequence
Neither I/O request satisfied Device puts request on hold while attempting to fulfill other request for each request
Livelock results
Understanding Operating Systems, Fifth Edition 19
20
Resolving deadlock
Removal of one condition
Understanding Operating Systems, Fifth Edition 21
Resource holding
Holding resource and not releasing it Waiting for other job to retreat
No preemption
Lack of temporary reallocation of resources
22
All four required for deadlock occurrence Deadlock remains until one condition removed
23
Modeling Deadlocks
Directed graphs
Circles represent processes Squares represent resources Solid arrow from resource to process
Process holding resource
25
26
27
28
29
30
31
32
33
34
Avoidance
Avoid deadlock if it becomes probable
Detection
Detect deadlock when it occurs Recover gracefully
Recovery
Resume system normalcy quickly and gracefully
Understanding Operating Systems, Fifth Edition 35
Resource holding
Bypassed if jobs request every necessary resource at creation time Multiprogramming degree significantly decreased Idle peripheral devices
Understanding Operating Systems, Fifth Edition 36
Circular wait
Bypassed if operating system prevents circle formation Use hierarchical ordering scheme Requires jobs to anticipate resource request order Difficult to satisfy all users
37
39
40
41
42
43
Scheduling suffers
Result of poor utilization Jobs kept waiting for resource allocation
Understanding Operating Systems, Fifth Edition 44
Detection algorithm
Remove process using current resource and not waiting for one Remove process waiting for one resource class
Not fully allocated
Go back to step 1
Repeat steps 1 and 2 until all connecting lines removed
Understanding Operating Systems, Fifth Edition 45
46
47
49
50
Starvation
Job execution prevented
Waiting for resources that never become available Results from conservative resource allocation
Example
The dining philosophers by Dijkstra
Starvation avoidance
Implement algorithm tracking how long each job waiting for resources (aging) Block new jobs until starving jobs satisfied
51
Starvation (continued)
52
Starvation (continued)
53
Summary
Operating system
Dynamically allocates resources Avoids deadlock and starvation
Prevention
Remove simultaneous occurrence of one or more conditions System will become deadlock-free Prevention algorithms
Complex algorithms and high execution overhead
Understanding Operating Systems, Fifth Edition 54
Summary (continued)
Avoid deadlocks
Clearly identify safe and unsafe states Keep reserve resources to guarantee job completion Disadvantage
System not fully utilized
No prevention support
System must detect and recover from deadlocks
Detection relies on selection of victim
55