Академический Документы
Профессиональный Документы
Культура Документы
Safe State
When a process requests an available resource, system must decide if immediate
Basic Facts
If system is in safe state no deadlock If system is in unsafe state possibility of deadlock Avoidance ensure that system will never enter an unsafe state
Example
Allocation Max
P0
P1 P2
5
2 2
10
4 9
Assume the system has 12 instances of the particular resource Is the system currently in a safe or in an unsafe state?
Example
Suppose that P2 requests and is allocated one more instance of the resource, still assume the system has 12 instances of the resource
Allocation P0 P1 P2 5 2 3 Max 10 4 9
Deadlock Avoidance
Requires that the system has some additional a priori information available:
Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes
Avoidance algorithms
Single instance of a resource type
Use a resource-allocation graph
Bankers Algorithm
Multiple instances
Each process must a priori claim maximum use
Banker s Algorithm
Toward right idea:
State maximum resource needs in advance Allow particular process to proceed if:
(available resources - #requested) max remaining that might be needed by any process
Algorithm allows the sum of maximum resource needs of all current processes to be greater than total resources
instances)
Snapshot at initial time: Allocation R0 R1 R2 P0 P1 P2 P3 P4 0 2 3 2 0 1 0 0 1 0 0 0 2 1 2 Max R0 R1 R2 7 3 9 2 4 5 2 0 2 3 3 2 2 2 3 Available R0 R1 R2 3 3 2
Need R0 R1 R2 7 1 4 2 3 2
6
0 4
0
1 3
0
1 1
The system is in a safe state since the sequence <P1, P3, P4, P2, P0> satisfies safety criteria
Allocation
R0 R1 R2 P0 P1 P2 P3 P4 0 2 3 2 0 1 0 0 1 0 0 0 2 1 2
Max
R0 R1 R2 7 3 9 2 4 5 2 0 2 3 3 2 2 2 3
Available
R 0 R1 R2 3 3 2
Find the new state that the system would be in if this request is granted. If the new state is safe, then it is safe to grant this request. Otherwise, it is not safe to honor this request (safety algorithm)
P0 P1 P2 P3 P4
Allocation R0 R1 R2 0 1 0 3 0 2 3 0 2 2 1 1 0 0 2
Max R0 R1 R2 7 5 3 3 2 2 9 0 2 2 2 2 4 3 3
Available R0 R1 R2 2 3 0
The sequence <P1, P3, P4, P0, P2> satisfies safety requirement, so it is safe to honor P1s request Can request for (3,3,0) by P4 be granted? Can request for (0,2,0) by P0 be granted?
1.
2. 3.
If Requesti Needi go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim If Requesti Available, go to step 3. Otherwise Pi must wait, since resources are not available Pretend to allocate requested resources to Pi by modifying the state as follows: Available = Available - Requesti Allocationi = Allocationi + Requesti Needi = Needi Requesti
Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively. Initialize:
Work = Available Finish[i] = false for i = 0, 1, , n
3.
go to step 2 4. If Finish[i]
== true
Example
Consider a computer system having 4 serially-reusable resource classes R0 (4 instances), R1 (3 instances), R2 (11 instances), and R3 (2 instances). There are 4 processes in the system P0 through P3. Assume that the maximum claim approach to deadlock avoidance is used. The current state is given by: Allocation Max R0 R1 R2 R3 R0 R1 R2 R3 P0 2 1 2 0 3 1 5 1 P1 1 0 4 1 2 2 6 2 P2 0 1 1 0 1 2 1 0 P3 0 0 3 0 1 1 7 1 Which of the following outstanding requests can be safely satisfied? Request R0 R1 R2 R3 P0 1 0 2 1 P1 0 1 0 1 P2 1 0 0 0 P3 0 0 1 1