commit d22259318e4cd5b02eafcfdbd5cbe7852308a644 Author: LinlyBoi Date: Wed Mar 15 22:13:47 2023 +0200 init bruv diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..f859c53 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "scheduling-rs" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..db09f43 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "scheduling-rs" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..43881e8 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,83 @@ +use std::io; +fn main() { + // Take user input + let proc_num: i32; + print!("Enter process number!"); + let mut processes: Vec = vec![]; + + let mut input: String = String::from(""); + _ = io::stdin().read_line(&mut input); + proc_num = input.trim().parse().expect("NOT NUMBER"); + + let mut num: i32 = 0; + while num < proc_num { + print!("Enter arrival for process"); + let mut temp_arrival = String::new(); + _ = io::stdin().read_line(&mut temp_arrival); + let temp_arrival: i32 = temp_arrival.trim().parse().expect("AAAAAA"); + + print!("Enter burst for process"); + let mut temp_burst = String::new(); + _ = io::stdin().read_line(&mut temp_burst); + let temp_burst: i32 = temp_burst.trim().parse().expect("AAAAAA"); + + let mut temp_proc = Process { + arrival: temp_arrival, + burst: temp_burst, + turnaround: 0, + waiting: 0, + remaining: temp_burst, + completion: 0, + completed: false, + }; + processes.push(temp_proc); + num += 1; + } + println!("You've entered: "); + srt(&processes, 1); + for proc in processes { + println!("{:#?}", proc) + } +} + +#[derive(Debug, Copy, Clone)] +pub struct Process { + arrival: i32, + burst: i32, + turnaround: i32, + waiting: i32, + remaining: i32, + completion: i32, + completed: bool, +} +impl Process { + pub fn proc(mut self, q: i32, current: i32) -> i32 { + self.remaining -= q; + if self.remaining == 0 { + self.completed = true; + self.completion = current + q; + } + current + q + } + pub fn calc_turn(mut self) { + self.turnaround = self.completion - self.arrival + } + pub fn calc_wait(mut self) { + self.waiting = self.turnaround - self.burst + } +} + +pub fn srt(procs: &Vec, q: i32) { + let mut smallest = &procs[0]; + let mut current = 0; + for value in procs { + if value.arrival <= current && value.remaining < smallest.remaining && !value.completed { + smallest = value; + current = value.proc(q, current); + } + if value.completed { + value.calc_turn(); + value.calc_wait(); + } + } +}