This commit is contained in:
LinlyBoi
2023-03-18 17:03:37 +02:00
parent d22259318e
commit cf2c0e5e57

View File

@@ -2,40 +2,41 @@ use std::io;
fn main() { fn main() {
// Take user input // Take user input
let proc_num: i32; let proc_num: i32;
print!("Enter process number!");
let mut processes: Vec<Process> = vec![]; let mut processes: Vec<Process> = vec![];
let mut input: String = String::from(""); let mut input: String = String::from("");
println!("Enter process number!");
_ = io::stdin().read_line(&mut input); _ = io::stdin().read_line(&mut input);
proc_num = input.trim().parse().expect("NOT NUMBER"); proc_num = input.trim().parse().expect("NOT NUMBER");
let mut num: i32 = 0; let mut num: i32 = 0;
while num < proc_num { while num < proc_num {
print!("Enter arrival for process"); println!("Enter arrival for process");
let mut temp_arrival = String::new(); let mut temp_arrival = String::new();
_ = io::stdin().read_line(&mut temp_arrival); _ = io::stdin().read_line(&mut temp_arrival);
let temp_arrival: i32 = temp_arrival.trim().parse().expect("AAAAAA"); let temp_arrival: i32 = temp_arrival.trim().parse().expect("AAAAAA");
print!("Enter burst for process"); println!("Enter burst for process");
let mut temp_burst = String::new(); let mut temp_burst = String::new();
_ = io::stdin().read_line(&mut temp_burst); _ = io::stdin().read_line(&mut temp_burst);
let temp_burst: i32 = temp_burst.trim().parse().expect("AAAAAA"); let temp_burst: i32 = temp_burst.trim().parse().expect("AAAAAA");
let mut temp_proc = Process { let temp_proc = Process {
arrival: temp_arrival, arrival: temp_arrival,
burst: temp_burst, burst: temp_burst,
turnaround: 0, turnaround: 0,
waiting: 0, waiting: 0,
remaining: temp_burst, remaining: temp_burst,
completion: 0, completion_time: 0,
completed: false, completed: false,
}; };
processes.push(temp_proc); processes.push(temp_proc);
num += 1; num += 1;
} }
println!("sorting!");
let sorted = fifo(processes);
println!("You've entered: "); println!("You've entered: ");
srt(&processes, 1); for proc in sorted {
for proc in processes {
println!("{:#?}", proc) println!("{:#?}", proc)
} }
} }
@@ -47,7 +48,7 @@ pub struct Process {
turnaround: i32, turnaround: i32,
waiting: i32, waiting: i32,
remaining: i32, remaining: i32,
completion: i32, completion_time: i32,
completed: bool, completed: bool,
} }
impl Process { impl Process {
@@ -55,29 +56,45 @@ impl Process {
self.remaining -= q; self.remaining -= q;
if self.remaining == 0 { if self.remaining == 0 {
self.completed = true; self.completed = true;
self.completion = current + q; self.completion_time = current + q;
} }
current + q current + q
} }
pub fn calc_turn(mut self) { pub fn calc_turn(self) -> i32 {
self.turnaround = self.completion - self.arrival self.completion_time - self.arrival
} }
pub fn calc_wait(mut self) { pub fn calc_wait(self) -> i32 {
self.waiting = self.turnaround - self.burst self.turnaround - self.burst
}
pub fn one_shot(mut self, current: i32) -> (Process, i32) {
self.completion_time = current + self.remaining;
self.completed = true;
self.remaining = 0;
self.calc_turn();
self.calc_wait();
(self, self.completion_time)
} }
} }
pub fn srt(procs: &Vec<Process>, q: i32) { // pub fn srt(procs: Vec<Process>, q: i32) -> Vec<Process> {
let mut smallest = &procs[0]; // let mut smallest = procs[0];
let mut current = 0; // let mut current = 0;
for value in procs { // let mut completed: Vec<Process> = vec![];
if value.arrival <= current && value.remaining < smallest.remaining && !value.completed { // todo!()
smallest = value; // }
current = value.proc(q, current); pub fn fifo(mut procs: Vec<Process>) -> Vec<Process> {
} procs.sort_by(|a, b| a.arrival.cmp(&b.arrival));
if value.completed { let mut clock = procs[0].arrival;
value.calc_turn(); let mut completed: Vec<Process> = vec![];
value.calc_wait(); for proc in procs {
if proc.arrival <= clock {
let mut done_proc: Process;
(done_proc, clock) = proc.one_shot(clock);
done_proc.turnaround = done_proc.calc_turn();
done_proc.waiting = done_proc.calc_wait();
println!("{}", proc.remaining);
completed.push(done_proc);
} }
} }
completed
} }