Banker's algorithm: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl 6}}: replace loop/last, further tidying)
Line 901: Line 901:
use warnings;
use warnings;
use feature 'say';
use feature 'say';

my $P = 3; # Number of processes
my $R = 4; # Number of resources


my @avail = (3, 1, 1, 2); # Available instances of resource
my @avail = (3, 1, 1, 2); # Available instances of resource
Line 911: Line 908:
# Function to find the system is in safe state or not
# Function to find the system is in safe state or not
sub isSafe {
sub isSafe {
my($avail, $maxm, $allot) = @_;
my($work, $maxm, $allot) = @_;
my $P = @$allot; # Number of processes
my $satisfied;
my @need; # the need matrix
my $R = @$work; # Number of resources
for my $i (0..$P-1) { # Calculating need of each $P
my @unfinished = (1) x $P; # Mark all processes as unfinished
my(@safeSeq,@need);
for my $j (0..$R-1) { # Need of instance = maxm instance - allocated instance
$need[$i][$j] = $$maxm[$i][$j] - $$allot[$i][$j]
for my $i (0..$P-1) { # Calculating need of each process:
for my $j (0..$R-1) { # maxm instance - allocated instance
$need[$i][$j] = $$maxm[$i][$j] - $$allot[$i][$j]
}
}
}
}
my @finish = (0) x $P; # Mark all processes as unfinished
my @safeSeq = (0) x $P; # To store safe sequence
my @work = @$avail; # Make a copy of available resources


# While all processes are not finished or system is not in safe state
# While all processes are not finished or system is not in safe state
my $count = 0;
my $count = 0;
while ($count < $P) { # Find a process which is not finish and whose needs
while ($count < $P) {
# can be satisfied with current @work resources.
my $found = 0;
my $found = 0;
for my $p (0..$P-1) {
for my $p (0..$P-1) {
# First check if a process is finished, if no, go for next condition
# While a process is not finished
if ($finish[$p] == 0) {
if ($unfinished[$p]) {
# Check if for all resources of current P need is less than work
# Check if for all resources of current P need is less than work
my $satisfied;
my $satisfied;
LOOP:
LOOP: for my $j (0..$R-1) {
for my $j (0..$R-1) {
$satisfied = $j;
$satisfied = $j;
last LOOP if $need[$p][$j] > $work[$j]
last LOOP if $need[$p][$j] > $$work[$j]
}
}
# If all needs of p were satisfied.
# If all needs of p were satisfied
if ($satisfied == $R-1) {
if ($satisfied == $R-1) {
$work[$_] += @$allot[$p]->[$_] for 0..$R-1; # free the resources
$$work[$_] += $$allot[$p][$_] for 0..$R-1; # free the resources
$safeSeq[$count] = $p; # Add this process to safe sequence.
say 'available resources: ' . join ' ', @$work;
$finish[$p] = 1; # Mark this p as finished
push @safeSeq, $p; # Add this process to safe sequence
$unfinished[$p] = 1; # Mark this process as finished
$count += 1;
$count += 1;
$found = 1
$found = 1
Line 960: Line 955:
say "Message: $status_message";</lang>
say "Message: $status_message";</lang>
{{out}}
{{out}}
<pre>Safe state? True
<pre>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</pre>
Message: Safe sequence is: 0 1 2</pre>