Banker's algorithm: Difference between revisions

Content added Content deleted
(→‎{{header|Racket}}: actual implementation added)
(Added Rust language)
Line 331: Line 331:
Available vector: '#(8 5 9 7)</pre>
Available vector: '#(8 5 9 7)</pre>


=={{header|rust}}==


Adapted from the C language version. It crashes for invalid input.

<lang rust>
fn read_numbers<T>() -> Vec<T>
where T: std::str::FromStr {
use std::io::Write;
std::io::stdout().flush().unwrap();

let mut line = String::new();
std::io::stdin().read_line(&mut line).unwrap();
line.split(" ").map(|word| word.trim().parse::<T>().ok().unwrap()).collect()
}

fn main() {
print!("Enter the number of resources: ");
let r = read_numbers()[0];
print!("Enter the number of processes: ");
let p = read_numbers()[0];
let mut running = vec![true; p];
let mut count = p;
print!("Enter the {}-item claim vector: ", r);
let max_res = read_numbers::<u32>();

println!("Enter the {}-line {}-column allocated-resource table:", p, r);
let mut curr = vec![vec![0; 0]; p];
for i in 0..p {
curr[i] = read_numbers::<u32>();
}
println!("Enter the {}-line {}-column maximum-claim table:", p, r);
let mut max_claim = vec![vec![0; 0]; p];
for i in 0..p {
max_claim[i] = read_numbers::<u32>();
}
print!("The claim vector is: ");
for i in 0..r {
print!("{} ", max_res[i]);
}
println!();

println!("The allocated resources table is:");
for i in 0..p {
for j in 0..r {
print!("\t{}", curr[i][j]);
}
println!();
}

println!("The maximum claims table is:");
for i in 0..p {
for j in 0..r {
print!("\t{}", max_claim[i][j]);
}
println!();
}
let mut alloc = vec![0; r];
for i in 0..p {
for j in 0..r {
alloc[j] += curr[i][j];
}
}
print!("The allocated resources are: ");
for i in 0..r {
print!("{} ", alloc[i]);
}
println!();
let mut avl = vec![0; r];
for i in 0..r {
avl[i] = max_res[i] - alloc[i];
}

print!("The available resources are: ");
for i in 0..r {
print!("{} ", avl[i]);
}
println!();

while count != 0 {
let mut safe = false;
for i in 0..p {
if running[i] {
let mut exec = true;
for j in 0..r {
if max_claim[i][j] - curr[i][j] > avl[j] {
exec = false;
break;
}
}

if exec {
println!("Process {} is executing.", i + 1);
running[i] = false;
count -= 1;
safe = true;
for j in 0..r {
avl[j] += curr[i][j];
}
break;
}
}
}

if safe {
println!("The process is in safe state.");
}
else {
println!("The processes are in unsafe state.");
break;
}

print!("The available vector is: ");
for i in 0..r {
print!("{} ", avl[i]);
}
println!();
}
}
</lang>

{{out|Input and Output}}
<pre>
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:
2 0 1 1
0 1 2 1
4 0 0 3
0 2 1 0
1 0 3 0
Enter the 5-line 4-column maximum-claim table:
3 2 1 4
0 2 5 2
5 1 0 5
1 5 3 0
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}}