Compare commits
1 Commits
rusting-ti
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| c4d3d94042 |
83
src/main.rs
83
src/main.rs
@@ -1,5 +1,4 @@
|
|||||||
use std::io;
|
use std::io;
|
||||||
pub mod rr;
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Take user input
|
// Take user input
|
||||||
let proc_num: i32;
|
let proc_num: i32;
|
||||||
@@ -34,7 +33,8 @@ fn main() {
|
|||||||
num += 1;
|
num += 1;
|
||||||
}
|
}
|
||||||
println!("sorting!");
|
println!("sorting!");
|
||||||
let sorted = rr::round_robin(processes, 1);
|
let completed: Vec<Process> = vec![];
|
||||||
|
let sorted = sjf(processes, completed, 100);
|
||||||
// let sorted = round_robin(processes, vec![], 0, 2);
|
// let sorted = round_robin(processes, vec![], 0, 2);
|
||||||
println!("You've entered: ");
|
println!("You've entered: ");
|
||||||
for proc in sorted {
|
for proc in sorted {
|
||||||
@@ -108,7 +108,7 @@ pub fn fifo(mut procs: Vec<Process>) -> Vec<Process> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn sjf(mut procs: Vec<Process>, mut completed: Vec<Process>, mut clock: i32) -> Vec<Process> {
|
pub fn sjf(mut procs: Vec<Process>, mut completed: Vec<Process>, mut clock: i32) -> Vec<Process> {
|
||||||
procs.sort_unstable_by_key(|proc| (proc.burst, proc.arrival));
|
// procs.sort_unstable_by_key(|proc| (proc.arrival, proc.burst));
|
||||||
|
|
||||||
if procs.is_empty() {
|
if procs.is_empty() {
|
||||||
completed
|
completed
|
||||||
@@ -131,64 +131,43 @@ pub fn sjf(mut procs: Vec<Process>, mut completed: Vec<Process>, mut clock: i32)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn round_robin(procs: Vec<Process>, q: i32) -> Vec<Process> {
|
pub fn round_robin(procs: Vec<Process>, q: i32) -> Vec<Process> {
|
||||||
let mut buffer = procs.to_owned();
|
let mut buffer = procs;
|
||||||
buffer.sort_by(|a, b| a.arrival.cmp(&b.arrival));
|
|
||||||
let mut current_time = 0;
|
let mut current_time = 0;
|
||||||
let mut in_cpu: Vec<Process> = vec![];
|
let mut in_cpu: Vec<Process> = vec![];
|
||||||
let mut done: Vec<Process> = vec![];
|
let mut done: Vec<Process> = vec![];
|
||||||
let mut last: Process = Process {
|
while !buffer.is_empty() || !in_cpu.is_empty() {
|
||||||
arrival: 0,
|
let mut counter = 0;
|
||||||
burst: 0,
|
if !buffer.is_empty() {
|
||||||
completion_time: 0,
|
for proc in buffer.to_owned() {
|
||||||
remaining: 0,
|
if buffer.is_empty() {
|
||||||
turnaround: 0,
|
continue;
|
||||||
waiting: 0,
|
}
|
||||||
};
|
if proc.arrival <= current_time {
|
||||||
loop {
|
in_cpu.push(proc);
|
||||||
let (ready, not_ready) = check_arrival(buffer, current_time);
|
buffer.remove(counter);
|
||||||
buffer = not_ready;
|
}
|
||||||
in_cpu = into_cpu(ready.to_owned(), in_cpu);
|
counter += 1;
|
||||||
if last.remaining > 0 {
|
}
|
||||||
in_cpu.push(last)
|
|
||||||
}
|
}
|
||||||
if !in_cpu.is_empty() {
|
if !in_cpu.is_empty() {
|
||||||
last = in_cpu.remove(0);
|
let mut current_proc = in_cpu.remove(0);
|
||||||
(last.remaining, current_time) = last.quan_zap(q, current_time);
|
if current_proc.remaining < q {
|
||||||
done = check_done(last, current_time, done);
|
current_time += current_proc.remaining;
|
||||||
}
|
|
||||||
println!("{}", done.len());
|
|
||||||
println!("{}, {}", buffer.len(), ready.len());
|
|
||||||
if done.len() == procs.len() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
done
|
|
||||||
}
|
|
||||||
pub fn check_arrival(buffer: Vec<Process>, current_time: i32) -> (Vec<Process>, Vec<Process>) {
|
|
||||||
let mut ready: Vec<Process> = vec![];
|
|
||||||
let mut not_ready: Vec<Process> = vec![];
|
|
||||||
for proc in buffer {
|
|
||||||
if proc.remaining <= current_time {
|
|
||||||
ready.push(proc);
|
|
||||||
} else {
|
} else {
|
||||||
not_ready.push(proc);
|
current_time += q;
|
||||||
}
|
}
|
||||||
|
current_proc.remaining = current_proc.robin_zap(q);
|
||||||
|
if current_proc.remaining == 0 {
|
||||||
|
current_proc.completion_time = current_time;
|
||||||
|
current_proc.turnaround = current_proc.calc_turn();
|
||||||
|
current_proc.waiting = current_proc.calc_wait();
|
||||||
|
done.push(current_proc);
|
||||||
|
} else {
|
||||||
|
in_cpu.push(current_proc);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
(ready, not_ready)
|
current_time += 1;
|
||||||
}
|
|
||||||
pub fn into_cpu(ready: Vec<Process>, mut cpu: Vec<Process>) -> Vec<Process> {
|
|
||||||
for proc in ready {
|
|
||||||
cpu.push(proc);
|
|
||||||
}
|
}
|
||||||
cpu
|
|
||||||
}
|
|
||||||
pub fn check_done(mut proc: Process, current_time: i32, mut done: Vec<Process>) -> Vec<Process> {
|
|
||||||
if proc.remaining == 0 {
|
|
||||||
proc.completion_time = current_time;
|
|
||||||
proc.turnaround = proc.calc_turn();
|
|
||||||
proc.waiting = proc.calc_wait();
|
|
||||||
done.push(proc);
|
|
||||||
}
|
}
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|||||||
80
src/rr.rs
80
src/rr.rs
@@ -1,80 +0,0 @@
|
|||||||
use std::collections::VecDeque;
|
|
||||||
|
|
||||||
use crate::Process;
|
|
||||||
pub fn round_robin(procs: Vec<Process>, q: i32) -> VecDeque<Process> {
|
|
||||||
let mut buffer = procs.to_owned();
|
|
||||||
buffer.sort_by(|a, b| a.arrival.cmp(&b.arrival));
|
|
||||||
let mut current_time = 0;
|
|
||||||
let mut in_cpu: VecDeque<Process> = VecDeque::new();
|
|
||||||
let mut done: VecDeque<Process> = VecDeque::new();
|
|
||||||
let mut last: Process = Process {
|
|
||||||
arrival: 0,
|
|
||||||
burst: 0,
|
|
||||||
completion_time: 0,
|
|
||||||
remaining: 0,
|
|
||||||
turnaround: 0,
|
|
||||||
waiting: 0,
|
|
||||||
};
|
|
||||||
loop {
|
|
||||||
if done.len() == procs.len() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
let (ready, not_ready) = check_arrival(buffer.to_owned(), current_time);
|
|
||||||
in_cpu = into_cpu(ready.to_owned(), in_cpu);
|
|
||||||
|
|
||||||
if last.remaining > 0 {
|
|
||||||
in_cpu.push_back(last)
|
|
||||||
}
|
|
||||||
|
|
||||||
match in_cpu.pop_front() {
|
|
||||||
Some(mut proc) => {
|
|
||||||
(proc.remaining, current_time) = proc.quan_zap(q, current_time);
|
|
||||||
done = check_done(proc, current_time, done);
|
|
||||||
if proc.remaining != 0 {
|
|
||||||
last = proc;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => current_time += 1,
|
|
||||||
};
|
|
||||||
println!("current time: {}", current_time);
|
|
||||||
println!("in_cpu: {}", in_cpu.len());
|
|
||||||
// println!("last remaining time: {}", last.remaining);
|
|
||||||
// println!("currently done: {}", done.len());
|
|
||||||
println!("buffer: {}, ready: {}", buffer.len(), ready.len());
|
|
||||||
}
|
|
||||||
done
|
|
||||||
}
|
|
||||||
pub fn check_arrival(buffer: Vec<Process>, current_time: i32) -> (VecDeque<Process>, Vec<Process>) {
|
|
||||||
let mut ready: VecDeque<Process> = VecDeque::new();
|
|
||||||
let mut not_ready: Vec<Process> = vec![];
|
|
||||||
for proc in buffer {
|
|
||||||
if proc.arrival <= current_time {
|
|
||||||
ready.push_back(proc)
|
|
||||||
} else {
|
|
||||||
not_ready.push(proc)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
(ready, not_ready)
|
|
||||||
}
|
|
||||||
pub fn into_cpu(ready: VecDeque<Process>, mut cpu: VecDeque<Process>) -> VecDeque<Process> {
|
|
||||||
for proc in ready {
|
|
||||||
cpu.push_back(proc);
|
|
||||||
}
|
|
||||||
cpu
|
|
||||||
}
|
|
||||||
pub fn check_done(
|
|
||||||
mut proc: Process,
|
|
||||||
current_time: i32,
|
|
||||||
mut done: VecDeque<Process>,
|
|
||||||
) -> VecDeque<Process> {
|
|
||||||
if proc.remaining == 0 {
|
|
||||||
proc.completion_time = current_time;
|
|
||||||
proc.turnaround = proc.calc_turn();
|
|
||||||
proc.waiting = proc.calc_wait();
|
|
||||||
done.push_back(proc);
|
|
||||||
}
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
// This code sucks!
|
|
||||||
Reference in New Issue
Block a user