Banker's algorithm: Difference between revisions

Content added Content deleted
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 ^P -> \p {
for Pool.keys -> \p {
# Check if process now can be finished
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
@unfinished[p] = False; # Mark this process as finished
Pool{p}--; # Remove this process from Pool
$count += 1;
$count += 1;
$found = True
$found = True