1. 1 Deadlocks Chapter 6
2. 2 What is a deadlock?
e.g., two processes A, B want to record a scanned document on a FD.
3. 3 6.1 Resources What is a resource?
A resource is anything that can be used by a single process at any instant of time.
ľ provide as service, e.g., processor and network adapter.
ľ system capabilities that is required by active resources, e.g., memory.
ľ only one process at a time can use it. e.g., speaker, processor.
ľ can be used by multiple processes, e.g., memory.
ľ exists only one in the system, e.g., speaker.
ľ e.g., processors in a multi-processor system.
4. 4 6.1.1 Preemptable and Nonpreemptable Resources Preemptable resource
can be taken away from a process owning it with no ill effect, e.g. memory (when a process exceeds its time quantum and is swapped out)
cannot be taken away from its current owner without causing the computation to fail. e.g. CD recorder (If a process has begun to burn a CD, taking the CD recorder away from it will result in a garbled CD)
Sequence of events required to use a resource
request the resource
use the resource
release the resource
5. 5 6.2 Introduction to Deadlocks Deadlock can be defined Formally as follows: A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause.
All processes are waiting.
None of them will ever cause any of the events that could wake up any of the other members of the set.
All processes continue waiting for ever.
In general, the event that each process is waiting for is the release of some resource currently possessed by another member of the set.
6. 6 6.2.1 Conditions for Deadlock Coffman et al. (1971)
1. Mutual exclusion condition. Each resource is either currently assigned to exactly one process or is available.
2. Hold and wait condition. Processes currently holding resources granted earlier can request new resources.
3. No preemption condition. Resources previously granted cannot be forcibly taken away from a process. They must be explicitly released by the process holding them.
4. Circular wait condition. There must be a circular chain of two or more processes, each of which is waiting for a resource held by the next member of the chain.
All 4 conditions must hold for a deadlock to occur. If one of them is absent, no deadlock is possible.
7. 7 6.2.2 Deadlock Modeling Modeled with directed graphs
8. 8 How deadlock occurs? e.g. Process A requests R S, B requests S, T, C requests T,R; scheduling: round robin
9. 9 How deadlock can be avoided? If granting a particular request might lead to deadlock, the operating system can simply suspend the process without granting request.
10. 10 Strategies for dealing with Deadlocks Just ignore the problem altogether. Maybe if you ignore it, it will ignore you.
Detection and recovery. Let deadlocks occurs, detect them, and take action.
Dynamic avoidance by careful resource allocation.
Prevention, by structurally negating one of the four conditions necessary to cause a deadlock.
11. 11 6.3 The Ostrich Algorithm Pretend there is no problem
It is reasonable if
deadlocks occur very rarely
cost of prevention is high
It is a trade-off between
12. 12 6.4 Deadlock Detection and Recovery
14. 14 Cycle detection in resource graph with depth-first search algorithm
For each node, N in the graph, perform the following 5 steps with N as the starting node.
Initialize L to the empty list, and designate all the arcs as unmarked.
Add the current node to the end of L and check to see if the node now appears in L two times. If it does, the graph contains a cycle (listed in L) and the algrithm terminates.
From the given node, see if there are any unmarked outgoing arcs. If so, goto step 5; if not, go to step 6.
Pick an unmarked outgoing arc at random and mark it. Then follow it to the new current node and go to step 3.
We have reached a dead end. Go back to the previous node, that is, the one that was current just before this one, make that one the current node, and go to step 3. If this node is the initial node, the graph does not contain any cycles and algorithm terminates.
15. 15 What this algorithm does?
Takes each node in turn as the root and a depth-first search on it.
If it ever comes back to a node it has already encountered , then it has found a cycle.
If it exhausts all the arcs from any given node, it backtracks to the previous node.
If it backtracks to the root and cannot go further, the subgraph reachable from the current node does not contain any cycles.
If this property holds for all node, no deadlock!!
16. 16 6.4.2 Detection with Multiple Resource of Each Type ľ using vectors and matrices multiple resources
17. 17 Initialize all processes as unmarked.
Look for an unmarked process, Pi, for which the i-th row of R is less than or equal to A.
If such a process is found, add the i-th row of C to A, mark the process, and go back to step 1.
If no such process exists, the algorithm terminates.
When the algorithm finishes, all unmarked processes, if any, are deadlocked.
What the algorithm does in step 1 is looking for a process that can be run to completion.
Such a process is characterized as having resources demands that can be met by the currently available resources.
The selected process run till completion and releases the resources
If all processes are able to run, no deadlock.
If some can never run, deadlock!!
18. 18 Example: A system has three processes, P1, P2, and P3, four resource classes, tape drives (4), plotters (2), scanners (3), and CR-ROMs (1). Current allocation matrix is shown by C, and request matrix is shown by R. Will this this system meet any deadlock? P1 cannot run, because there is no CD-ROM available.
P2 cannot run too, because there is no scanner available.
P3 can run. When it finishes, it return all its resource, giving A=(2, 2, 2, 0).
At this point P2 can run. When it finishes, it return all its resource, giving A=(4, 2, 2, 1).
Then, P1 can run. NO DEADLOCK!!
19. 19 6.4.3 Recovery from Deadlock Recovery through preemption
take a resource from some other process
depends on nature of the resource
Recovery through rollback
Using checkpoints. A process is checked periodically. The checkpoints contain the memory image, the resource state. New checkpoints should be written to new files.
When a deadlock is detected, it is easy to identify which resources are used by which process, a process that owns the needed resource is rolled back to a point in time before it acquired the resource.
20. 20 Recovery through killing processes
crudest but simplest way to break a deadlock
kill one of the processes in the deadlock cycle
the other processes get its resources
choose process carefully that can be rerun from the beginning
e.g., killing a process that is updating database may not be always run safely.
21. 21 6.5 Deadlock Avoidance 6.5.1 Resource Trajectories Two process resource trajectories
22. 22 6.5.2 Safe and Unsafe States Demonstration that the state in (a) is safe
23. 23 Demonstration that the sate in (b) is not safe
24. 24 6.5.3 The Banker's Algorithm for a Single Resource Three resource allocation states
25. 25 6.5.4 Banker's Algorithm for Multiple Resources Example of banker's algorithm with multiple resources
26. 26 6.6 Deadlock Prevention Conditions for Deadlocks
Mutual exclusion condition
Hold and wait condition
No preemption condition
Circular wait condition
6.6.1 Attacking the Mutual Exclusion Condition
Donĺt assign resource exclusively to a process.
Feasible: spooling printer output
Not feasible: spooling of process table
Avoid assigning a resource if not absolutely necessary
As few processes as possible actually claim the resources
27. 27 6.6.2 Attacking the Hold and Wait Condition
Prevent processes that hold resources from waiting for more resources
Only one way: processes request all resources before starting execution.
If all resources are available, a process can start otherwise it must wait.
Processes often do not know how many resources they will need until they have started running.
Resources will not be optimally used in this method.
Variation (different way to break the hold-and-wait condition)
A process must release all resources it holds, then request everything it needs all at a time.
28. 28 6.6.3 Attacking the No Preemption Condition
Forcibly taking away the resource that a process is holding and assign to another.
Some resources are nonpreemptable, e.g., printer.
6.6.4 Attacking the Circular wait condition
To have a rule: that a process is entitled only to a single resource at any moment. If it needs a second one, it must release the first one. Unacceptable, e.g., copy a huge file from tape to printer.
Another way: global numbering all resources. Processes can request resources whenever they want to, but all requests must be made in numerical order. It might be impossible to find an ordering that satisfies everyone.
30. 30 Summary of approaches to deadlock prevention
31. 31 Example. 1 The following shows a resource graph. Do illegal graphs exist, that is graphs that structurally violate the model introduced in section 3.2 ? If so, mark them with ôXö.
32. 32 Example. 2 Process A holds resource W and requests for resource U, Process B holds resource U and requests for resource V, Process C holds nothing and request for V, Process D holds nothing and requests for W, Process E holds resource V and requests for resource W. Draw the resource graph and identify whether the system is deadlocked or not. And if deadlocked, which processes are involved?
34. 34 Example. 4 A system has three processes, P1, P2, and P3, and four resource classes, which are 4 tape drives, 2 plotters, 3 scanners, and 1 CD-ROM, respectively.
Find the existing resource vector, E.
At an instant t1, scanner is assigned to P1, 2 tape drives and 1 CD-ROM are assigned to P2, and 1 plotter and 2 scanners are assigned to P3. Find the current allocation matrix, C, and available resource vector, A.
After a quantum of time passed, P1 further requests 2 tape drives and 1 CD-ROM, P2 requests for 1 tape drive and 1 scanner, P3 request for 2 tape drives and 1plotters. Find the request matrix, R.
In above situation, is the entire system deadlocked? If yes, what resources are involved? If not, what is the possible sequence to run these processes?
35. 35 Example. 5 A system has four processes, P1, ů, P4, and five allocatable resources. The current allocation and maximum needs are as follows:
Allocated Maximum Available resource vector
P1 1 0 2 1 1 1 1 2 1 3 A=(0 0 x 1 1)
P2 2 0 1 1 0 2 2 2 1 0
P3 1 1 0 1 0 2 1 3 1 0
P4 1 1 1 1 0 1 1 2 2 1
What is the smallest value of x for which this is a safe state?
36. 36 Example. 6 A computer system has six tape drives, with n processes competing for them. Each process may need two drives. For which values of n is the system deadlock free?
37. 37 Example. 7 Two processes, A and B, each needs three records, a, b, and c, in a database. If A asks for them in the order a, b, and c, and B asks for them in the same order, deadlock is not possible. However, if B asks for them in the order c, b, and a, then the deadlock is possible. What fraction of all the combinations are guaranteed to be deadlock free?