Banker's algorithm: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) (Added Perl example) |
SqrtNegInf (talk | contribs) (→{{header|Perl 6}}: use hypers for array operations, tidying) |
||
Line 970: | Line 970: | ||
my \P = 3 ; # Number of processes |
my \P = 3 ; # Number of processes |
||
my \R = 4 ; # Number of resources |
my \R = 4 ; # Number of resources |
||
my @processes = <0 1 2>; |
|||
my @avail = <3 1 1 2>; # Available instances of resource |
my @avail = <3 1 1 2>; # Available instances of resource |
||
⚫ | |||
my @allot = <1 2 2 1>, <1 0 3 3>, <1 2 1 0>; # Resources allocated to processes |
|||
⚫ | |||
# Resources allocated to processes |
|||
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( |
sub isSafe(\avail, \maxm, \allot) { |
||
⚫ | |||
my $satisfied; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
for ^P -> \i { # Calculating Need of each P |
|||
⚫ | |||
for ^R -> \j { # Need of instance = maxm instance - allocated instance |
|||
@need[i;j] = @maxm[i;j] - @allot[i;j] |
|||
⚫ | |||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
for ^R -> \i { @work[i] = avail[i] } |
|||
# 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 { |
||
my $satisfied; |
|||
# Check if for all resources of current P need is less than work |
|||
LOOP: for ^R -> \j { |
|||
$satisfied = j; |
|||
last LOOP if @need[p;j] > @work[j] |
|||
} |
|||
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 } |
||
# Mark this p as finished |
|||
@finish[p] = 1; |
|||
$found = True |
|||
} |
} |
||
} |
|||
# 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 |
|||
} |
|||
return True, "Safe sequence is: " ~ @safeSeq |
|||
True, "Safe sequence is: " ~ @safeSeq |
|||
} |
} |
||
# Check system is in safe state or not |
# Check system is in safe state or not |
||
my ($safe-state,$status-message) = isSafe |
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> |