Beruflich Dokumente
Kultur Dokumente
Deadlock avoidance
Safe State
• When a process requests an available resource, system must decide if immediate
allocation leaves the system in a safe state
• System is in a safe state if there exists a safe sequence of all processes
• Sequence <P0, P1, …, Pn> is safe if for each Pi, the resources that Pi can still request can
be satisfied by currently available resources + resources held by all the Pj with j < i
– If Pi‘s resource needs are not immediately available, then Pi can wait until all Pj have finished
– When Pj finishes, Pi can obtain needed resources, execute, return allocated resources, and
terminate
Allocation Max
P0 5 10
P1 2 4
P2 3 9
• Multiple instances
• 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)
Banker’s Algorithm: Example
• This is the new state that the system would be in if P1’s request is honored
Allocation Max Available
R0 R1 R2 R0 R1 R2 R0 R1 R2
P0 0 1 0 7 5 3 2 3 0
P1 3 0 2 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
– The sequence <P1, P3, P4, P0, P2> satisfies safety requirement, so it is safe to
honor P1’s request
• Can request for (3,3,0) by P4 be granted?
• Can request for (0,2,0) by P0 be granted?
Data Structures for the Banker’s Algorithm
Finish[i] = true
go to step 2