Banker's algorithm: Difference between revisions

→‎{{header|Perl 6}}: use hypers for array operations, tidying
(Added Perl example)
(→‎{{header|Perl 6}}: use hypers for array operations, tidying)
Line 970:
my \P = 3 ; # Number of processes
my \R = 4 ; # Number of resources
my @processes = <0 1 2>;
 
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 Maximum@allot R= that<1 can2 2 1>, <1 0 3 3>, <1 2 1 0>; # beResources allocated to processes
my @maxm = <3 3 2 2>, <1 2 3 4>, <1 3 5 0>;
 
# Resources allocated to processes
my @allot = <1 2 2 1>, <1 0 3 3>, <1 2 1 0>;
 
# Function to find the system is in safe state or not
sub isSafe(\processes, \avail, \maxm, \allot) {
my @need = @maxm »-« @allot; # the need matrix
my $satisfied;
my @finish = 0 xx P; # Mark all processes as infinishunfinished
my @need; # the need matrix
my @safeSeq = 0 xx P; # To store safe sequence
for ^P -> \i { # Calculating Need of each P
my @work = 0 xx R= avail; # Make a copy of available resources
for ^R -> \j { # Need of instance = maxm instance - allocated instance
@need[i;j] = @maxm[i;j] - @allot[i;j]
}
}
my @finish = 0 xx P; # Mark all processes as infinish
my @safeSeq = 0 xx P; # To store safe sequence
my @work = 0 xx R; # Make a copy of available resources
for ^R -> \i { @work[i] = avail[i] }
 
# 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 = False;
for ^P -> \p {
# First check if a process is finished, if no, go for next condition
if @finish[p] == 0 {
# Check if for allmy resources of current P need is less than work$satisfied;
LOOP: # Check if for ^Rall resources of current P need is ->less \jthan {work
$satisfied =LOOP: for ^R -> \j; {
last LOOP if @need[p;j] > @work[$satisfied = j];
} last LOOP if @need[p;j] > @work[j]
if $satisfied == R - 1 { # If all needs of p were satisfied.}
# Addif the$satisfied allocated== resourcesR of- current1 { # If all needs of Pp towere thesatisfied.
# available/ @work resources»+=« allot[p;^R]; # i.e.freeFree the resources
for ^R -> \k { @worksafeSeq[k$count] += allot[p;k] } # Add this process to safe sequence.
@finish[p] = 1; # AddMark this process top safeas sequence.finished
@safeSeq[ $count] += p1;
$countfound += 1;True }
# Mark this p as finished
@finish[p] = 1;
$found = True
}
}
# If we could not find a next process in safe sequence.
}
# If wereturn couldFalse, not"System findis a next processnot in safe sequencestate." unless $found;
}
return False, "System is not in safe state." unless $found;
# If system is in safe state then safe sequence will be as below
}
# Ifreturn systemTrue, is in safe state then safe"Safe sequence willis: be" as~ below@safeSeq
True, "Safe sequence is: " ~ @safeSeq
}
 
# Check system is in safe state or not
my ($safe-state,$status-message) = isSafe @processes, @avail, @maxm, @allot;
say "Safe state? $safe-state";
say "Message: $status-message";</lang>
2,392

edits