user
|  |
| --- |
| This page uses content from **Wikipedia**. The original article was at Banker's algorithm. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) |

The **Banker's algorithm** is a resource allocation and deadlock avoidance algorithm developed by Edsger Dijkstra that tests for safety by simulating the allocation of predetermined maximum possible amounts of all resources, and then makes a "s-state" check to test for possible deadlock conditions for all other pending activities, before deciding whether allocation should be allowed to continue.

Example input

Assuming that the system distinguishes between four types of resources, (A, B, C and D), the following is an example of how those resources could be distributed.

*Note that this example shows the system at an instant before a new request for resources arrives.   Also, the types and number of resources are abstracted.   Real systems, for example, would deal with much larger quantities of each resource.*

```
Total resources in system:
A B C D
6 5 7 6

```

```
Available system resources are:
A B C D
3 1 1 2

```

```
Processes (currently allocated resources):
   A B C D
P1 1 2 2 1
P2 1 0 3 3
P3 1 2 1 0

```

```
Processes (maximum resources):
   A B C D
P1 3 3 2 2
P2 1 2 3 4
P3 1 3 5 0

```

```
Need= maximum resources - currently allocated resources
Processes (need resources):
   A B C D
P1 2 1 0 1
P2 0 2 0 1
P3 0 1 4 0

```
assistant
(formerly Perl 6)
Based on the Python3 solution by Shubham Singh found
here

```
my @avail = <3 1 1 2>;                        # Available instances of resource
my @maxm  = <3 3 2 2>, <1 2 3 4>, <1 3 5 0>;  # Maximum resources that can be allocated to processes
my @allot = <1 2 2 1>, <1 0 3 3>, <1 2 1 0>;  # Resources allocated to processes

# Function to find the system is in safe state or not
sub isSafe(\work is copy, \maxm, \allot) {
    my \P          = allot.elems;     # Number of processes
    my \Pool       = (^P).SetHash;    # Process pool
    my \R          = work.elems;      # Number of resources 
    my \need       = maxm »-« allot;  # the need matrix
    my @safe-sequence;

    # While all processes are not finished or system is not in safe state
    my $count = 0;
    while $count < P {
        my $found = False;
        for Pool.keys -> \p {           
            if all need[p] »≤« work {    # now process can be finished
                work »+=« allot[p;^R];   # Free the resources
                say 'available resources: ' ~ work;
                @safe-sequence.push: p;  # Add this process to safe sequence
                Pool{p}--;               # Remove this process from Pool
                $count += 1;
                $found = True
            }
        }
        # If we could not find a next process in safe sequence
        return False, "System is not in safe state." unless $found;
    }
    # If system is in safe state then safe sequence will be as below
    return True, "Safe sequence is: " ~ @safe-sequence
}

# Check if system is in a safe state
my ($safe-state,$status-message) = isSafe @avail, @maxm, @allot;
say "Safe state? $safe-state";
say "Message:    $status-message";

```

Output:

```
available resources: 4 3 3 3
available resources: 5 3 6 6
available resources: 6 5 7 6
Safe state? True
Message:    Safe sequence is: 0 1 2
```