Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Deadlock Detection
Recovery from Deadlock
To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks
To present a number of different methods for preventing or avoiding deadlocks in a computer system.
Suppose system has 2 tape drives. P1 and P2 each hold one tape drive and each needs another one.
Process 1 Process 2
holds
holds
Deadlock Occurs
Tape Drive 1
Tape Drive 2
Deadlock Occurs
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
A process must request a resource before using it, and must release the resource after using it. A process may request as many resources as it requires to carry out its designated task
The number of resources requested may not exceed the total number of resources available in the system
E.g a process cannot request three printers if the system only has two)
No preemption condition.
10
1st Condition
Only one process can have exclusive control of a shared Mutual exclusion resource and if any other process request that very condition particular resource then it has to wait till the time the resource is released
2nd Condition
A process holding one resource and waiting to acquire another resource that are currently held by other process
SPRING 2011 - 102
11
3rd Condition
Resources cannot be preempted; A resource can be No preemption released only voluntarily by the process holding it, after condition. that process has completed its task.
4rth Condition
A circular chain of processes exist Circular wait in which each process holds condition. number of resources which are requested by the next process in the chain
P = {P1, P2, , Pn}, the set consisting of all the processes in the system. R = {R1, R2, , Rm}, the set consisting of all resource types in the system.
Requesting a resource
Holding a resource
cycles
no deadlock
No cycles
if several instances per resource type, possibility of deadlock
18
cycles
no deadlock
No cycles
if several instances per resource type, possibility of deadlock
20
21
2nd Strategy
3rd Strategy
4th Strategy
Detection and recovery. Let deadlocks occur, detect them, take action.
SPRING 2011 - 102
convenience correctness
Deadlock prevention is the process of making it logically impossible for one of the 4 deadlock conditions to hold.
To ensure that the hold-and-wait never occurs in the system, we must guarantee that, whenever a process requests a resource, it does not hold any other resources.
2 Ways
Require a process to request and be allocated all its resources before it begins execution
Allow a process to request resources only when the process has none.
Problems
No useful work possible with some resources while waiting for others Also ties up resources other processes could be using May not know required resources at start of run Process could starve
A process that needs several popular resources may have to wait indefinitely, because at least one of the resources that it needs is always allocated to some other process
Preempted resources are added to the list of resources for which the process is waiting. Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting.
30
This approach to the deadlock problem anticipates deadlock before it actually occurs. This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly. This method differs from deadlock prevention, which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock.
If the necessary conditions for a deadlock are in place, it is still possible to avoid deadlock by being careful when resources are allocated
SPRING 2011 - 102
Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need.
Avoid deadlock by acquiring additional information on how the resources are to be requested.
For Example:
Process A uses resource 1 and 2 and then releases them Process B uses resource 2 and then 1.
If system have this information before hand than prior to resource allocation a deadlock avoidance algorithm can determine that which process should wait so that a deadlock can be avoided in future
Avoidance
When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state.
System is in safe state if there exists a safe sequence of all processes. Sequence <P1, P2, , Pn> is safe if for each Pi, the resources that Pi can still request can be satisfied by currently available resources
If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished. When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate.
When Pi terminates, Pi+1 can obtain its needed resources, and so on.
A state is known as SAFE state, if the system can allocate resources to each process in SOME order and still avoid deadlock
A state is known as SAFE state, if the system can allocate resources to each process in SOME order and still avoid deadlock
1
5 7 1 0
A state is known as SAFE state, if the system can allocate resources to each process in SOME order and still avoid deadlock
1
5 1 0
A state is known as SAFE state, if the system can allocate resources to each process in SOME order and still avoid deadlock
1 2
5 7 0 1
A state is known as SAFE state, if the system can allocate resources to each process in SOME order and still avoid deadlock
1 2
5 7 1 0
A state is known as SAFE state, if the system can allocate resources to each process in SOME order and still avoid deadlock
3 1 2
5 7 1 0
A state is known as SAFE state, if the system can allocate resources to each process in SOME order and still avoid deadlock
3 1 2
5 7 10 0
When a process requests a resource it may have to wait. When a process gets all its resources it must return them
in a finite amount of time.
Available:
Vector of length m. If available [j] = k, there are k instances of resource type Rj available.
Max:
n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances of resource type Rj.
Allocation
n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj.
Need
n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task
Max
ABC 753
Available
ABC 332
P1 P2
P3 P4
200 302
211 002
322 902
222 433
P1 P2
P3 P4
122 600
011 431
The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria.