Banker's algorithm: Difference between revisions
→{{header|Perl 6}}: use hypers for array operations, tidying
SqrtNegInf (talk | contribs) (Added Perl example) |
SqrtNegInf (talk | contribs) (→{{header|Perl 6}}: use hypers for array operations, tidying) |
||
Line 970:
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 @maxm = <3 3 2 2>, <1 2 3 4>, <1 3 5 0>;
# Function to find the system is in safe state or not
sub isSafe(
my @need = @maxm »-« @allot; # the need matrix▼
▲ my @need; # the need matrix
my @safeSeq = 0 xx P; # To store safe sequence▼
}▼
▲ 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
# 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 {
@finish[p] = 1; #
$
}
# If we could not find a next process in safe sequence.
# If system is in safe state then safe sequence will be as below
}
# Check system is in safe state or not
my ($safe-state,$status-message) = isSafe
say "Safe state? $safe-state";
say "Message: $status-message";</lang>
|