Deadlock Avoidance

Banker's Algorithm for MIMR systems
 

The concept of  Safe State

Data Structures used:

Available [R]
Max [N] [R]
Allocated [N] [R]
Need [N] [R]
Request [N] [R]

Main Algorithm:

Check whether a request is valid
Check if resources are available
Pretend allocation by modifying the state
Check whether the new state is a safe state
If so, allocate; else restore the old state

Algorithm: Test for Safe State:

temporary [0..R-1] = Copy of Available[]
finished [0..N-1] = false
(Mark all processes as unfinished)

Simulate run:
    go on marking processes as finished till you can 
    allocate the resources they need (0 or more),
     If a process is marked finished, increment the temporary
At the end, if all processes finish, the initial state is a safe state