Banker's algorithm: Difference between revisions
Content added Content deleted
(→{{header|M2000 Interpreter}}: Fixed Algorithm and added two examples.) |
(Add Swift) |
||
Line 1,227: | Line 1,227: | ||
println!(); |
println!(); |
||
} |
} |
||
}</lang> |
|||
} |
|||
</lang> |
|||
{{out|Input and Output}} |
{{out|Input and Output}} |
||
Line 1,278: | Line 1,277: | ||
The available vector is: 8 5 9 7 |
The available vector is: 8 5 9 7 |
||
</pre> |
</pre> |
||
=={{header|Swift}}== |
|||
<lang swift>import Foundation |
|||
print("Enter the number of resources: ", terminator: "") |
|||
guard let resources = Int(readLine(strippingNewline: true)!) else { |
|||
fatalError() |
|||
} |
|||
print("Enter the number of processes: ", terminator: "") |
|||
guard let processes = Int(readLine(strippingNewline: true)!) else { |
|||
fatalError() |
|||
} |
|||
var running = Array(repeating: true, count: processes) |
|||
var curr = Array(repeating: [Int](), count: processes) |
|||
var alloc = Array(repeating: 0, count: resources) |
|||
var available = Array(repeating: 0, count: resources) |
|||
var maxClaims = Array(repeating: [Int](), count: processes) |
|||
var count = processes |
|||
print("Enter the \(resources)-item claim vector: ", terminator: "") |
|||
guard let maxRes = readLine(strippingNewline: true)?.components(separatedBy: " ").compactMap(Int.init), |
|||
maxRes.count == resources else { |
|||
fatalError() |
|||
} |
|||
print("Enter the \(processes)-line \(resources)-column allocated-resource table:") |
|||
for i in 0..<processes { |
|||
print("Row \(i + 1): ", terminator: "") |
|||
guard let allc = readLine(strippingNewline: true)?.components(separatedBy: " ").compactMap(Int.init), |
|||
maxRes.count == resources else { |
|||
fatalError() |
|||
} |
|||
curr[i] = allc |
|||
} |
|||
print("Enter the \(processes)-line \(resources)-column maximum-claim table:") |
|||
for i in 0..<processes { |
|||
print("Row \(i + 1): ", terminator: "") |
|||
guard let clms = readLine(strippingNewline: true)?.components(separatedBy: " ").compactMap(Int.init), |
|||
maxRes.count == resources else { |
|||
fatalError() |
|||
} |
|||
maxClaims[i] = clms |
|||
} |
|||
for i in 0..<processes { |
|||
for j in 0..<resources { |
|||
alloc[j] += curr[i][j] |
|||
} |
|||
} |
|||
for i in 0..<resources { |
|||
available[i] = maxRes[i] - alloc[i] |
|||
} |
|||
print("The claim vector is: \(maxRes)") |
|||
print("The allocated resources table is: \(curr)") |
|||
print("The maximum claims table is: \(maxClaims)") |
|||
print("The allocated resources are: \(alloc)") |
|||
print("The available resources are: \(available)") |
|||
while count != 0 { |
|||
var safe = false |
|||
for i in 0..<processes where running[i] { |
|||
var exec = true |
|||
for j in 0..<resources where maxClaims[i][j] - curr[i][j] > available[j] { |
|||
exec = false |
|||
break |
|||
} |
|||
if exec { |
|||
print("Process \(i + 1) is executing.") |
|||
running[i] = false |
|||
count -= 1 |
|||
safe = true |
|||
for j in 0..<resources { |
|||
available[j] += curr[i][j] |
|||
} |
|||
break |
|||
} |
|||
} |
|||
if safe { |
|||
print("The process is in safe state.") |
|||
} else { |
|||
print("The processes are in unsafe state.") |
|||
break |
|||
} |
|||
print("The available vector is: \(available)") |
|||
}</lang> |
|||
{{out|Input and Output}} |
|||
<pre style="scroll=overflow; height: 20em">Enter the number of resources: 4 |
|||
Enter the number of processes: 5 |
|||
Enter the 4-item claim vector: 8 5 9 7 |
|||
Enter the 5-line 4-column 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 the 5-line 4-column 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 |
|||
The claim vector is: [8, 5, 9, 7] |
|||
The allocated resources table is: [[2, 0, 1, 1], [0, 1, 2, 1], [4, 0, 0, 3], [0, 2, 1, 0], [1, 0, 3, 0]] |
|||
The maximum claims table is: [[3, 2, 1, 4], [0, 2, 5, 2], [5, 1, 0, 5], [1, 5, 3, 0], [3, 0, 3, 3]] |
|||
The allocated resources are: [7, 3, 7, 5] |
|||
The available resources are: [1, 2, 2, 2] |
|||
Process 3 is executing. |
|||
The process is in safe state. |
|||
The available vector is: [5, 2, 2, 5] |
|||
Process 1 is executing. |
|||
The process is in safe state. |
|||
The available vector is: [7, 2, 3, 6] |
|||
Process 2 is executing. |
|||
The process is in safe state. |
|||
The available vector is: [7, 3, 5, 7] |
|||
Process 4 is executing. |
|||
The process is in safe state. |
|||
The available vector is: [7, 5, 6, 7] |
|||
Process 5 is executing. |
|||
The process is in safe state. |
|||
The available vector is: [8, 5, 9, 7]</pre> |
|||
{{omit from|Blast}} |
{{omit from|Blast}} |