Upload
joan-cross
View
217
Download
0
Tags:
Embed Size (px)
Citation preview
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously:
1. Mutual Exclusion2. Hold and Wait3. No Preemption4. Circular Wait
Mutual Exclusion
only one process at a time can use a resource.
If another process requests that resource, the requesting process must be delayed until the resource has been released
Hold and Wait
a process that holding at least one resource is waiting to acquire additional resources held by other processes.
No preemption
a resource can be released only voluntarily by the process holding it, after that process has completed its task.
Circular Wait
there exists a set {P0, P1, …, P0} of waiting processes such that
P0 is waiting for a resource that is held by P1,
P1is waiting for a resource that is held by P2, …, Pn–1is waiting for a resource that is held by Pn, and P0is waiting for a resource that is held by P0.
Resource-Allocation Graph
A set of vertices V and a set of edges E.
V is partitioned into two types: 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.
E is also partitioned into two types: request edge –directed edge P1 →Rj assignment edge –directed edge Rj→Pi
7.4 Deadlock Prevention We try to ensure that one of the four
necessary conditions cannot hold, then we can prevent it
Mutual Exclusion:If it is shareable resource, then we can
break the mutual exclusion (such as: Read-only file)
If it is not a shareable resource, then mutual exclusion must hold (such as: Printer)
7.4 Deadlock Prevention Hold and wait: two methods
1. Require process to request and be allocated all its resources before it begins execution.
2. allow process to request resources only when the process has none.
Hold and Wait method example We consider a process that copies data from
DVD drive to a picture file on disk and then prints the picture to a printer
Method1: request DVD drive, Disk, and Printer before it execute. It will hold the Printer for entire execution, even though it needs the printer only at the end
Method2: request only for DVD drive and Disk initially. It finishes the copy step and release both resource. The process must then again ask for disk and printer to finish the job
Hold and Wait method example
Low resource utilization;
starvation possible, (if a process needs several popular resources)
7.4 Deadlock Prevention NO preemption:
If a process that is holding some resources requestsanother resource that cannot be immediately to allocated it, then all resources currently being held are released.
Preempted resources are added to the list of resourcesfor which the process is waiting.
Process will be restarted only when it can regain its oldresources, as well as the new ones that it is requesting.
7.4 Deadlock Prevention
Circular wait:impose a total ordering of all
resourcetypes, and require that each process requests resources in an increasing
order of enumeration.
Circular wait First of all we create a map function that
maps each resource, for example: F(tape drive) = 1, F(disk drive) = 5, F(Printer) =12
Then we have two rules for Processes to request Resources:
1. Each process can request resources only in an increasing order
2. Whenever a process requests an instance of resource Rj, it has released any resources Ri such that F(Ri) >= F(Rj)
Circular wait Let the set of processes involved in the circular
wait condition be {P0,P1,..,Pn}, where Pi is waiting for a resource Ri, which is held by Pi+1 (Pn is waiting for Rn which held by P0)
Since Pi+1 is holding Ri while requesting resource Ri+1, we must have F(Ri) < F(Ri+1) for all I
But this condition means that F(R0)<F(R1)<…<F(Rn)<F(R0)
By transitivity F(R0) < F(R0) which is impossible
7.5 Deadlock Avoidance Possible side effects of preventing
deadlock are low device utilization and reduce system throughput
An alternative method for avoiding deadlocks is to require additional information about how resources are to be required.
7.5 Deadlock Avoidance With this knowledge of the complete
sequence of requests and releases for each process the system can decide for each request whether or not the process should wait in order to avoid possible future deadlock
A deadlock avoidance algorithm dynamically examines the resource-allocation state to ensure that a circular wait condition can never happen
Safe State
A state is safe if the system can allocate resources to each processes in some order (safe sequence) and still avoid a deadlock
If no such sequence exists, then the system state is said to be unsafe
Safe State To illustrate, we consider a system has 12 magnetic
tape drives and 3 processes. P0 needs 10 tapes, P1 needs 4 and P2 needs 9.
Currently, P0 has 5, P1 has 2 and P2 has 2
Max needs Current needsP0 10 5 P1 4 2P2 9 2
At T0, the system is in safe state, since <P1,P0,P2> satisfied safe state condition
What if P2 currently ask for one more tape and has that one?
Safe, Unsafe, Deadlock
7.5 Deadlock Avoidance The algorithm is simply to ensure
that the system will always remain in safe state.
Therefore, if a process requests a resource that is currently available, it may still have to wait.
Thus, resource utilization may be lower
Avoidance algorithms
Single instance of a resource type. Use a resource allocation graph
Multiple instances of a resource type. Use the banker’s algorithm
Resource-Allocation Graph Scheme Claim edge Pi → Rj indicated that process Pj
may request resource Rj in the future; represented by a dashed line.
Claim edge converts to request edge when a process requests a resource.
Request edge converted to an assignment edge when the resource is allocated to the process.
When a resource is released by a process, assignment edge reconverts to a claim edge.
Resource-Allocation Graph
Unsafe State In Resource-Allocation Graph
Resource-Allocation Graph Algorithm Suppose that process Pi requests a
resource Rj
The request can be granted only if converting the request edge to an assignment edge does not result in the formation of a cycle in the resource allocation graph
Banker’s Algorithm Multiple instances. Each process must a claim
maximum use in advance. 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.
Banker’s Algorithm
Two algorithms need to be discussed:
1. Safety state check algorithm
2. Resource request algorithm
Data Structures for the Banker’s Algorithm
Safety Algorithm
Resource-Request Algorithm for Process Pi
Example of Banker’s Algorithm
Example of Banker’s Algorithm
Example of Banker’s Algorithm