Banker's algorithm: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl 6}}: curly out of place)
m (→‎{{header|Perl 6}}: replace loop/last, further tidying)
Line 964: Line 964:


=={{header|Perl 6}}==
=={{header|Perl 6}}==
Based on the Python3 solution by Shubham Singh found
<lang perl6># Reference:
# https://www.geeksforgeeks.org/program-bankers-algorithm-set-1-safety-algorithm/
[https://www.geeksforgeeks.org/program-bankers-algorithm-set-1-safety-algorithm/ here]
<lang perl6>my @avail = <3 1 1 2>; # Available instances of resource
# based on the Python3 solution by Shubham Singh(SHUBHAMSINGH10)
my @maxm = <3 3 2 2>, <1 2 3 4>, <1 3 5 0>; # Maximum resources that can be allocated to processes

my \P = 3 ; # Number of processes
my @allot = <1 2 2 1>, <1 0 3 3>, <1 2 1 0>; # Resources allocated to 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
# Function to find the system is in safe state or not
sub isSafe(\avail, \maxm, \allot) {
sub isSafe(\work is copy, \maxm, \allot) {
my @need = @maxm »-« @allot; # the need matrix
my \P = allot.elems; # Number of processes
my @finish = 0 xx P; # Mark all processes as unfinished
my \R = work.elems; # Number of resources
my @safeSeq = 0 xx P; # To store safe sequence
my \need = maxm »-« allot; # the need matrix
my @work = avail; # Make a copy of available resources
my @unfinished = True xx P; # Mark all processes as unfinished
my @safe-sequence;


# While all processes are not finished or system is not in safe state
# While all processes are not finished or system is not in safe state
my $count = 0;
my $count = 0;
while $count < P { # Find a process which is not finish and whose needs
while $count < P {
# can be satisfied with current @work resources.
my $found = False;
my $found = False;
for ^P -> \p {
for ^P -> \p {
# First check if a process is finished, if no, go for next condition
# Check if process now can be finished
if @finish[p] == 0 {
if @unfinished[p] and all need[p] »≤« work {
my $satisfied;
work »+=« allot[p;^R]; # Free the resources
# Check if for all resources of current P need is less than work
say 'available resources: ' ~ work;
LOOP: for ^R -> \j {
@safe-sequence.push: p; # Add this process to safe sequence
$satisfied = j;
@unfinished[p] = False; # Mark this process as finished
last LOOP if @need[p;j] > @work[j]
$count += 1;
}
$found = True
if $satisfied == R - 1 { # If all needs of p were satisfied.
@work »+=« allot[p;^R]; # Free the resources
@safeSeq[$count] = p; # Add this process to safe sequence.
@finish[p] = 1; # Mark this p as finished
$count += 1;
$found = True
}
}
}
}
}
# If we could not find a next process in safe sequence.
# If we could not find a next process in safe sequence
return False, "System is not in safe state." 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
# If system is in safe state then safe sequence will be as below
return True, "Safe sequence is: " ~ @safeSeq
return True, "Safe sequence is: " ~ @safe-sequence
}
}


# Check system is in safe state or not
# Check if system is in a safe state
my ($safe-state,$status-message) = isSafe @avail, @maxm, @allot;
my ($safe-state,$status-message) = isSafe @avail, @maxm, @allot;
say "Safe state? $safe-state";
say "Safe state? $safe-state";
say "Message: $status-message";</lang>
say "Message: $status-message";</lang>
{{out}}
{{out}}
<pre>Safe state? True
<pre>available resources: 4 3 3 3
available resources: 5 3 6 6
available resources: 6 5 7 6
Safe state? True
Message: Safe sequence is: 0 1 2</pre>
Message: Safe sequence is: 0 1 2</pre>