Jump to content

Banker's algorithm: Difference between revisions

m
→‎{{header|Perl 6}}: replace loop/last, further tidying
m (→‎{{header|Perl 6}}: curly out of place)
m (→‎{{header|Perl 6}}: replace loop/last, further tidying)
Line 964:
 
=={{header|Perl 6}}==
# basedBased on the Python3 solution by Shubham Singh(SHUBHAMSINGH10) found
<lang perl6># Reference:
# [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 Rresources that can be allocated to processes
 
my \P@allot = <1 2 2 1>, <1 0 3 3>, <1 2 1 0>; # NumberResources allocated ofto 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
sub isSafe(\availwork is copy, \maxm, \allot) {
my @need\P = @maxm »-« @ = allot.elems; # theNumber needof matrixprocesses
my @finish\R = 0 xx P; = work.elems; # Mark# allNumber processesof asresources unfinished
my @safeSeq\need = 0 xx P; = maxm »-« allot; # To storethe safeneed sequencematrix
my @workunfinished = True xx = availP; # Make aMark copyall ofprocesses availableas resourcesunfinished
my @safe-sequence;
 
# 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 checkCheck if a process isnow finished,can ifbe no, go for next conditionfinished
if @finishunfinished[p] ==and 0all need[p] »≤« work {
mywork $satisfied»+=« allot[p;^R]; # Free the resources
#say Check if for all'available resources: of current P need is less' than~ work;
LOOP@safe-sequence.push: forp; ^R -># Add this process to \jsafe {sequence
@unfinished[p] = False; $satisfied# =Mark j;this process as finished
$count += last LOOP if @need[p1;j] > @work[j]
}$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.
return False, "System is not in safe state." unless $found;
}
# If system is in safe state then safe sequence will be as below
return True, "Safe sequence is: " ~ @safeSeqsafe-sequence
}
 
# Check if system is in a safe state or not
my ($safe-state,$status-message) = isSafe @avail, @maxm, @allot;
say "Safe state? $safe-state";
say "Message: $status-message";</lang>
{{out}}
<pre>Safeavailable state?resources: True4 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>
 
2,392

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.