Banker's algorithm: Difference between revisions
m
→{{header|Perl}}: tidying
SqrtNegInf (talk | contribs) m (→{{header|Perl 6}}: replace loop/last, further tidying) |
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: tidying) |
||
Line 901:
use warnings;
use feature 'say';
my $P = 3; # Number of processes▼
my @avail = (3, 1, 1, 2); # Available instances of resource
Line 911 ⟶ 908:
# Function to find the system is in safe state or not
sub isSafe {
my($
my $R = @
my(@safeSeq,@need);
for my $j (0..$R-1) { # Need of instance = maxm instance - allocated instance▼
for my $i (0..$P-1) {
$need[$i][$j] = $$maxm[$i][$j] - $$allot[$i][$j]
}
}
# While all processes are not finished
my $count = 0;
while ($count < $P) {
my $found = 0;
for my $p (0..$P-1) {
#
if ($
# Check if for all resources of current P need is less than work
my $satisfied;
LOOP: for my $j (0..$R-1) {
$satisfied = $j;
last LOOP if $need[$p][$j] > $$work[$j]
}
# If all needs of p were satisfied
if ($satisfied == $R-1) {
$$work[$_] +=
$unfinished[$p] = 1; # Mark this process as finished
$count += 1;
$found = 1
Line 960 ⟶ 955:
say "Message: $status_message";</lang>
{{out}}
<pre>
available resources: 5 3 6 6
available resources: 6 5 7 6
Safe state? True
Message: Safe sequence is: 0 1 2</pre>
|