AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa
This commit is contained in:
93
src/main.rs
93
src/main.rs
@@ -33,7 +33,8 @@ fn main() {
|
|||||||
num += 1;
|
num += 1;
|
||||||
}
|
}
|
||||||
println!("sorting!");
|
println!("sorting!");
|
||||||
let sorted = round_robin(processes, vec![], 0, 2);
|
let sorted = round_robin(processes, 10);
|
||||||
|
// let sorted = round_robin(processes, vec![], 0, 2);
|
||||||
println!("You've entered: ");
|
println!("You've entered: ");
|
||||||
for proc in sorted {
|
for proc in sorted {
|
||||||
println!("{:#?}", proc)
|
println!("{:#?}", proc)
|
||||||
@@ -50,15 +51,15 @@ pub struct Process {
|
|||||||
completion_time: i32,
|
completion_time: i32,
|
||||||
}
|
}
|
||||||
impl Process {
|
impl Process {
|
||||||
pub fn quan_zap(self, q: i32, current: i32) -> (Process, i32, i32) {
|
pub fn quan_zap(self, q: i32, current: i32) -> (i32, i32) {
|
||||||
if q < 0 {
|
if q < 0 {
|
||||||
println!("Why would you ever do this?");
|
println!("Why would you ever do this?");
|
||||||
return (self, 0, 0);
|
return (0, 0);
|
||||||
}
|
}
|
||||||
if self.remaining >= q {
|
if self.remaining >= q {
|
||||||
(self, self.remaining - q, current + q)
|
(self.remaining - q, current + q)
|
||||||
} else {
|
} else {
|
||||||
(self, 0, current + self.remaining)
|
(0, current + self.remaining)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn calc_turn(self) -> i32 {
|
pub fn calc_turn(self) -> i32 {
|
||||||
@@ -72,6 +73,13 @@ impl Process {
|
|||||||
self.remaining = 0;
|
self.remaining = 0;
|
||||||
(self, self.completion_time)
|
(self, self.completion_time)
|
||||||
}
|
}
|
||||||
|
pub fn robin_zap(self, q: i32) -> i32 {
|
||||||
|
if self.remaining >= q {
|
||||||
|
self.remaining - q
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn srt(procs: Vec<Process>, q: i32) -> Vec<Process> {
|
// pub fn srt(procs: Vec<Process>, q: i32) -> Vec<Process> {
|
||||||
@@ -121,37 +129,46 @@ pub fn sjf(mut procs: Vec<Process>, mut completed: Vec<Process>, mut clock: i32)
|
|||||||
sjf(procs, completed, clock + 1)
|
sjf(procs, completed, clock + 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// pub fn round_robin(
|
pub fn round_robin(procs: Vec<Process>, q: i32) -> Vec<Process> {
|
||||||
// mut procs: Vec<Process>,
|
let mut buffer = procs;
|
||||||
// mut completed: Vec<Process>,
|
let mut current_time = 0;
|
||||||
// mut clock: i32,
|
let mut in_cpu: Vec<Process> = vec![];
|
||||||
// q: i32,
|
let mut done: Vec<Process> = vec![];
|
||||||
// ) -> Vec<Process> {
|
while !buffer.is_empty() || !in_cpu.is_empty() {
|
||||||
// if procs.is_empty() {
|
let mut counter = 0;
|
||||||
// completed
|
if !buffer.is_empty() {
|
||||||
// } else {
|
for proc in buffer.to_owned() {
|
||||||
// let mut done_proc: Process;
|
if buffer.is_empty() {
|
||||||
// let mut i = 0;
|
break;
|
||||||
// while i < procs.len() {
|
}
|
||||||
// if procs[i].arrival <= clock {
|
if proc.arrival <= current_time {
|
||||||
// (done_proc, done_proc.remaining, clock) = procs[i].quan_zap(q, clock);
|
in_cpu.push(proc);
|
||||||
// if done_proc.remaining == 0 {
|
buffer.remove(counter);
|
||||||
// done_proc.completion_time = clock;
|
}
|
||||||
// procs.remove(i);
|
counter += 1;
|
||||||
// done_proc.turnaround = done_proc.calc_turn();
|
}
|
||||||
// done_proc.waiting = done_proc.calc_wait();
|
}
|
||||||
// completed.push(done_proc);
|
if !in_cpu.is_empty() {
|
||||||
// return round_robin(procs, completed, clock, q);
|
let mut current_proc = in_cpu.remove(0);
|
||||||
// } else {
|
if current_proc.remaining < q {
|
||||||
// procs.remove(i);
|
current_time += current_proc.remaining;
|
||||||
// procs.push(done_proc);
|
} else {
|
||||||
// return round_robin(procs, completed, clock, q);
|
current_time += q;
|
||||||
// }
|
}
|
||||||
// } else {
|
current_proc.remaining = current_proc.robin_zap(q);
|
||||||
// i += 1
|
if current_proc.remaining == 0 {
|
||||||
// }
|
current_proc.completion_time = current_time;
|
||||||
// }
|
current_proc.turnaround = current_proc.calc_turn();
|
||||||
// round_robin(procs, completed, clock + 1, q)
|
current_proc.waiting = current_proc.calc_wait();
|
||||||
// }
|
done.push(current_proc);
|
||||||
// }
|
} else {
|
||||||
|
in_cpu.push(current_proc);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
current_time += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
// This code sucks!
|
// This code sucks!
|
||||||
|
|||||||
Reference in New Issue
Block a user