150 likes | 488 Views
Conditions for a deadlock. Conditions for DeadlockMutual exclusion: resources cannot be shared. Hold and wait: processes request resources incrementally, and hold on to what they've got. No preemption: resources cannot be forcibly taken from processes. Circular wait: circular chain of waiting, i
E N D
1. Definition of a Deadlock
Deadlock Definition
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 (including itself).
Waiting for an event could be:
waiting for access to a critical section
waiting for a resource Note that it is usually a non-preemptable (resource). pre-emptable resources can be yanked away and given to another.
2. Conditions for a deadlock Conditions for Deadlock
Mutual exclusion: resources cannot be shared.
Hold and wait: processes request resources incrementally, and hold on to what they've got.
No preemption: resources cannot be forcibly taken from processes.
Circular wait: circular chain of waiting, in which each process is waiting for a resource held by the next process in the chain.
3. Startegies for dealing with Deadlocks ignore the problem altogether - it may occur very infrequently, cost of detection/prevention etc may not be worth it ( a show in the Ostrich Algorithm ).
detection and recovery
avoidance by careful resource allocation
prevention by structurally negating one of the four necessary conditions.
4. Deadlock Detection Is there a deadlock currently?
One resource of each type (1 printer, 1 plotter, 1 terminal etc.)
Process:
check if there is a cycle in the resource graph. for each node N in the graph do DFS (depth first search) of the graph with N as the root In the DFS if you come back to a node already traversed, then there is a cycle. }
A Cycle indicates a deadlock
5. One-resource system Deadlock
6. Multiple-resource system deadlock Let there be n processes, P1 through Pn, and m resource classes, E1 through Em.
Let E be the existing resource vector indicating the number of instances of each resource in existence, i.e., E1 is the number of instances of the resource of class 1.
Let A be the available resource vector, which indicates the number of available instances in each resource class, i.e., A1=2 means that there are two resources available of class 1.
7. Multiple-resource system deadlock Using two arrays: C, which is the current allocation matrix, and R, which is the request matrix.
The ith row of C tells how many instances of each resource class Ei process Pi currently holds or allocates. Thus Cij is the number of instances of resource j that are held/allocated by process i.
Similarly, Rij is the number of instances of resource i that Pj wants.
8. Multiple-resource system deadlock Deadlock detection algorithm
Conditions:
All processes are initially unmarked.
As the algorithm progresses, each process will be marked, indicating that they can be run to completion ( resource demands are, or will eventually be met ).
Unmarked processes after the algorithm terminates are deadlocked.
9. Multiple-resource system deadlock Look for an unmarked process, Pi, for which the ith cell/item of R is less than or equal to Ai.
If such a process is found, add the ith row of C to A, mark the process, and repeat step 1. (the process requests are reset to 0)
If no such process exists, the algorithm terminates.
10. Multiple-resource system deadlock Example.
Suppose that a system has 4 tape drives, 2 plotters, 3 scanners, and 1 CD-ROM drive.
The only available resources are 2 tape drives and 1 plotter.
Three processes are requesting for more resources other than what they are currently using.
11. Multiple-resource system algorithm E = ( 4 2 3 1) A = ( 2 1 0 0 )
12. Multiple-resource system algorithm
13. Deadlock Recovery
Recovery
through preemption
rollback
keep checkpointing periodically
when a deadlock is detected, see which resource is needed.
Take away the resource from the process currently having it.
Later on, you can restart this process from a check pointed state where it may need to reacquire the resource.
killing procsses
where possible, kill a process that can be rerun from the beginning without illeffects
14. Illustration Situation In the bridge crossing example wherein only one person (process) may use/cross the bridge (use resources), when two persons coming from different directions meet on the bridge and no one is willing to back off, a deadlock occurs.
In reality, the detection process is much more complex especially in distributed systems. A complex algorithm is required.
15. Illustration Situation – cont’d. There are several ways to perform a recovery:
1. One process is aborted ( 1 person is
thrown off the river).
2. One process is forced to release the
occupied resources ( 1 person backs off).
*The selection of the victim is very important: logically a person (process) who is closest to the destination (completion) should have a higher priority than the one who just stepped onto the bridge (newly arrived process).