Banker's algorithm: Difference between revisions

Added Perl example
m (→‎{{header|Perl 6}}: fewer parens, separate boolean/string in function return)
(Added Perl example)
Line 895:
}
BankerAlgo</lang>
 
=={{header|Perl}}==
{{trans|Perl 6}}
<lang perl>use strict;
use warnings;
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 @maxm = ([3, 3, 2, 2], [1, 2, 3, 4], [1, 3, 5, 0]); # Maximum R 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 {
my($avail, $maxm, $allot) = @_;
my $satisfied;
my @need; # the need matrix
for my $i (0..$P-1) { # Calculating need of each $P
for my $j (0..$R-1) { # Need of instance = 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
my $count = 0;
while ($count < $P) { # Find a process which is not finish and whose needs
# can be satisfied with current @work resources.
my $found = 0;
for my $p (0..$P-1) {
# First check if a process is finished, if no, go for next condition
if ($finish[$p] == 0) {
# Check if for all resources of current P need is less than work
my $satisfied;
LOOP:
for my $j (0..$R-1) {
$satisfied = $j;
last LOOP if $need[$p][$j] > $work[$j]
}
# If all needs of p were satisfied.
if ($satisfied == $R-1) {
$work[$_] += @$allot[$p]->[$_] for 0..$R-1; # free the resources
$safeSeq[$count] = $p; # Add this process to safe sequence.
$finish[$p] = 1; # Mark this p as finished
$count += 1;
$found = 1
}
}
}
# If we could not find a next process in safe sequence.
return 0, "System is not in safe state." unless $found;
}
# If system is in safe state then safe sequence will be as below
return 1, "Safe sequence is: " . join ' ', @safeSeq
}
 
# Check system is in safe state or not
my($safe_state,$status_message) = isSafe(\@avail, \@maxm, \@allot);
say "Safe state? " . ($safe_state ? 'True' : 'False');
say "Message: $status_message";</lang>
{{out}}
<pre>Safe state? True
Message: Safe sequence is: 0 1 2</pre>
 
=={{header|Perl 6}}==
2,392

edits