Banker's algorithm: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: tidying) |
m (→{{header|Perl 6}}: reduce array look up & randomize output sequence) |
||
Line 971: | Line 971: | ||
sub isSafe(\work is copy, \maxm, \allot) { |
sub isSafe(\work is copy, \maxm, \allot) { |
||
my \P = allot.elems; # Number of processes |
my \P = allot.elems; # Number of processes |
||
my \Pool = (^P).SetHash; # Process pool |
|||
my \R = work.elems; # Number of resources |
my \R = work.elems; # Number of resources |
||
my \need = maxm »-« allot; # the need matrix |
my \need = maxm »-« allot; # the need matrix |
||
my @unfinished = True xx P; # Mark all processes as unfinished |
|||
my @safe-sequence; |
my @safe-sequence; |
||
Line 980: | Line 980: | ||
while $count < P { |
while $count < P { |
||
my $found = False; |
my $found = False; |
||
for |
for Pool.keys -> \p { |
||
if all need[p] »≤« work { # now process can be finished |
|||
if @unfinished[p] and all need[p] »≤« work { |
|||
work »+=« allot[p;^R]; # Free the resources |
work »+=« allot[p;^R]; # Free the resources |
||
say 'available resources: ' ~ work; |
say 'available resources: ' ~ work; |
||
@safe-sequence.push: p; # Add this process to safe sequence |
@safe-sequence.push: p; # Add this process to safe sequence |
||
Pool{p}--; # Remove this process from Pool |
|||
$count += 1; |
$count += 1; |
||
$found = True |
$found = True |