Banker's algorithm: Difference between revisions

Content added Content deleted
(added Perl 6 programming solution)
m (→‎{{header|Perl 6}}: fewer parens, separate boolean/string in function return)
Line 897: Line 897:


=={{header|Perl 6}}==
=={{header|Perl 6}}==
<lang perl6>#!/usr/bin/env perl6
<lang perl6># Reference:

# Reference:
# https://www.geeksforgeeks.org/program-bankers-algorithm-set-1-safety-algorithm/
# https://www.geeksforgeeks.org/program-bankers-algorithm-set-1-safety-algorithm/
# based on the Python3 solution by Shubham Singh(SHUBHAMSINGH10)
# based on the Python3 solution by Shubham Singh(SHUBHAMSINGH10)
Line 910: Line 908:


# Maximum R that can be allocated to processes
# Maximum R that can be allocated to processes
my @maxm = (<3 3 2 2>, <1 2 3 4>, <1 3 5 0>);
my @maxm = <3 3 2 2>, <1 2 3 4>, <1 3 5 0>;


# Resources allocated to processes
# Resources allocated to processes
my @allot = (<1 2 2 1>, <1 0 3 3>, <1 2 1 0>);
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
# Function to find the system is in safe state or not
sub isSafe(\processes, \avail, \maxm, \allot) {
sub isSafe(\processes, \avail, \maxm, \allot) {
my $satisfied;
my $satisfied;
my @need = (); # the need matrix
my @need; # the need matrix
for ^P -> \i { # Calculating Need of each P
for ^P -> \i { # Calculating Need of each P
for ^R -> \j { # Need of instance = maxm instance - allocated instance
for ^R -> \j { # Need of instance = maxm instance - allocated instance
Line 931: Line 929:
# 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 { # Find a process which is not finish and whose needs
# can be satisfied with current @work resources.
# 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
# First check if a process is finished, if no, go for next condition
if (@finish[p] == 0) {
if @finish[p] == 0 {
# Check if for all resources of current P need is less than work
# Check if for all resources of current P need is less than work
LOOP: for ^R -> \j {
LOOP: for ^R -> \j {
$satisfied = j;
$satisfied = j;
last LOOP if (@need[p;j] > @work[j])
last LOOP if @need[p;j] > @work[j]
}
}
if ($satisfied == (R - 1)) { # If all needs of p were satisfied.
if $satisfied == R - 1 { # If all needs of p were satisfied.
# Add the allocated resources of current P to the
# Add the allocated resources of current P to the
# available/work resources i.e.free the resources
# available/work resources i.e.free the resources
Line 956: Line 954:
}
}
# 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;
if ($found == False) {
say "System is not in safe state";
return False
}
}
}
# 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
say "System is in safe state.";
True, "Safe sequence is: " ~ @safeSeq
say "Safe sequence is: ", @safeSeq;
return True
}
}


# Check system is in safe state or not
# Check system is in safe state or not
isSafe(@processes, @avail, @maxm, @allot)</lang>
my ($safe-state,$status-message) = isSafe @processes, @avail, @maxm, @allot;
say "Safe state? $safe-state";
say "Message: $status-message";</lang>
{{out}}
{{out}}
<pre>System is in safe state.
<pre>Safe state? True
Safe sequence is: [0 1 2]
Message: Safe sequence is: 0 1 2</pre>
</pre>




=={{header|Phix}}==
=={{header|Phix}}==