Banker's algorithm: Difference between revisions

(Banker's algorithm en Python)
Line 895:
}
BankerAlgo</lang>
 
=={{header|Nim}}==
{{trans|Kotlin}}
<lang Nim>import sequtils, strformat, strutils, sugar
 
stdout.write "Enter the number of resources: "
let r = stdin.readLine().parseInt()
 
stdout.write "Enter the number of processes: "
stdout.flushFile()
let p = stdin.readLine().parseInt()
 
stdout.write "Enter Claim Vector: "
let maxRes = stdin.readLine().splitWhitespace().map(parseInt)
 
echo "Enter Allocated Resource Table:"
var curr = newSeqWith(p, newSeq[int](r))
for i in 0..<p:
stdout.write &"Row {i + 1}: "
curr[i] = stdin.readLine().splitWhitespace().map(parseInt)
 
echo "Enter Maximum Claim Table:"
var maxClaim = newSeqWith(p, newSeq[int](r))
for i in 0..<p:
stdout.write &"Row {i + 1}: "
maxClaim[i] = stdin.readLine().splitWhitespace().map(parseInt)
 
var alloc = newSeq[int](r)
for i in 0..<p:
for j in 0..<r:
alloc[j] += curr[i][j]
echo &"\nAllocated Resources: {alloc.join(\" \")}"
 
var avl = collect(newSeq, for i in 0..<r: maxRes[i] - alloc[i])
echo &"Available Resources: {avl.join(\" \")}"
 
var running = repeat(true, p)
var count = p
while count > 0:
var safe = false
for i in 0..<p:
if running[i]:
var exec = true
for j in 0..<r:
if maxClaim[i][j] - curr[i][j] > avl[j]:
exec = false
break
if exec:
echo &"\nProcess {i + 1} is executing."
running[i] = false
dec count
safe = true
for j in 0..<r: avl[j] += curr[i][j]
break
 
if not safe:
echo "The processes are in an unsafe state."
break
 
echo "\nThe process is in a safe state."
echo &"Available Vector: {avl.join(\" \")}"</lang>
 
{{out}}
<pre>Enter the number of resources: 4
Enter the number of processes: 5
Enter Claim Vector: 8 5 9 7
Enter Allocated Resource Table:
Row 1: 2 0 1 1
Row 2: 0 1 2 1
Row 3: 4 0 0 3
Row 4: 0 2 1 0
Row 5: 1 0 3 0
Enter Maximum Claim Table:
Row 1: 3 2 1 4
Row 2: 0 2 5 2
Row 3: 5 1 0 5
Row 4: 1 5 3 0
Row 5: 3 0 3 3
 
Allocated Resources: 7 3 7 5
Available Resources: 1 2 2 2
 
Process 3 is executing.
 
The process is in a safe state.
Available Vector: 5 2 2 5
 
Process 1 is executing.
 
The process is in a safe state.
Available Vector: 7 2 3 6
 
Process 2 is executing.
 
The process is in a safe state.
Available Vector: 7 3 5 7
 
Process 4 is executing.
 
The process is in a safe state.
Available Vector: 7 5 6 7
 
Process 5 is executing.
 
The process is in a safe state.
Available Vector: 8 5 9 7</pre>
 
=={{header|Perl}}==
Anonymous user