From 056c28c0175a3ed129179f5e26930d3fb1a61317 Mon Sep 17 00:00:00 2001 From: GZTimeWalker Date: Wed, 18 May 2022 20:11:54 +0800 Subject: [PATCH] wip: tiny change --- Cargo.lock | 2 +- pkg/app/dining/src/main.rs | 10 ++++----- pkg/app/mq/src/main.rs | 12 ++++++----- pkg/kernel/src/process/manager.rs | 6 +++--- pkg/kernel/src/process/process.rs | 2 +- pkg/kernel/src/process/sync.rs | 35 +++++++++++++------------------ 6 files changed, 31 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b1e7c99..f97d334 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -217,7 +217,7 @@ dependencies = [ [[package]] name = "ggos_kernel" -version = "0.9.0" +version = "0.9.1" dependencies = [ "bit_field", "bitflags", diff --git a/pkg/app/dining/src/main.rs b/pkg/app/dining/src/main.rs index 13be881..f8a8e69 100644 --- a/pkg/app/dining/src/main.rs +++ b/pkg/app/dining/src/main.rs @@ -15,7 +15,6 @@ fn main() -> usize { CHOPSTICK[i].init(1); } - for i in 0..5 { let pid = sys_fork(); if pid == 0 { @@ -30,7 +29,7 @@ fn main() -> usize { println!("#{} holds threads: {:?}", cpid, &pids); sys_stat(); - + for i in 0..5 { println!("#{} Waiting for #{}...", cpid, pids[i]); sys_wait_pid(pids[i]); @@ -40,18 +39,19 @@ fn main() -> usize { } fn philosopher(id: usize) -> ! { + let pid = sys_get_pid(); for _ in 0..20 { if id == 0 { - println!("philosopher #{} is sleeping...", id); + println!("philosopher #{}({}) is sleeping...", id, pid); core::hint::spin_loop(); } - println!("philosopher #{} is thinking...", id); + println!("philosopher #{}({}) is thinking...", id, pid); CHOPSTICK[id].acquire(); CHOPSTICK[(id + 1) % 5].acquire(); - println!("philosopher #{} is eating...", id); + println!("philosopher #{}({}) is eating...", id, pid); CHOPSTICK[(id + 1) % 5].release(); CHOPSTICK[id].release(); diff --git a/pkg/app/mq/src/main.rs b/pkg/app/mq/src/main.rs index 754e6f0..104c517 100644 --- a/pkg/app/mq/src/main.rs +++ b/pkg/app/mq/src/main.rs @@ -39,7 +39,7 @@ fn main() -> usize { sys_stat(); - + for i in 0..QUEUE_COUNT { println!("#{} Waiting for #{}...", cpid, pids[i]); sys_wait_pid(pids[i]); @@ -53,12 +53,13 @@ fn main() -> usize { } unsafe fn producer(id: usize) -> ! { - println!("New producer #{}", id); + let pid = sys_get_pid(); + println!("New producer #{}({})", id, pid); for _ in 0..20 { IS_NOT_FULL.acquire(); MUTEX.acquire(); COUNT += 1; - println!("Produced by #{} count={}", id, &COUNT); + println!("Produced by #{}({}) count={}", id, pid, &COUNT); MUTEX.release(); IS_NOT_EMPTY.release(); } @@ -66,12 +67,13 @@ unsafe fn producer(id: usize) -> ! { } unsafe fn consumer(id: usize) -> ! { - println!("New consumer #{}", id); + let pid = sys_get_pid(); + println!("New consumer #{}({})", id, pid); for _ in 0..20 { IS_NOT_EMPTY.acquire(); MUTEX.acquire(); COUNT -= 1; - println!("Consumed by #{} count={}", id, &COUNT); + println!("Consumed by #{}({}) count={}", id, pid, &COUNT); MUTEX.release(); IS_NOT_FULL.release(); } diff --git a/pkg/kernel/src/process/manager.rs b/pkg/kernel/src/process/manager.rs index 3aba436..d924738 100644 --- a/pkg/kernel/src/process/manager.rs +++ b/pkg/kernel/src/process/manager.rs @@ -200,7 +200,7 @@ impl ProcessManager { } pub fn print_process_list(&self) { - let mut output = String::from(" PID | PPID | Name | Ticks | Status\n"); + let mut output = String::from(" PID | PPID | Name | Ticks | Status\n"); for p in self.processes.iter() { output = output + format!("{}\n", p).as_str(); } @@ -234,8 +234,6 @@ impl ProcessManager { } pub fn kill(&mut self, pid: ProcessId, ret: isize) { - trace!("Killing process #{} with ret code: {}", pid, ret); - let p = self.processes.iter().find(|x| x.pid() == pid); if p.is_none() { @@ -245,6 +243,8 @@ impl ProcessManager { let p = p.unwrap(); + debug!("Killing process {}#{} with ret code: {}", p.name(), pid, ret); + let parent = p.parent(); let children = p.children(); let cur_page_table_addr = p.page_table_addr().start_address().as_u64(); diff --git a/pkg/kernel/src/process/process.rs b/pkg/kernel/src/process/process.rs index 47924d9..bc1bd08 100644 --- a/pkg/kernel/src/process/process.rs +++ b/pkg/kernel/src/process/process.rs @@ -435,7 +435,7 @@ impl core::fmt::Display for Process { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { write!( f, - " #{:-3} | #{:-3} | {:12} | {:8} | {:?}", + " #{:-3} | #{:-3} | {:13} | {:8} | {:?}", u16::from(self.pid), u16::from(self.parent), self.name, diff --git a/pkg/kernel/src/process/sync.rs b/pkg/kernel/src/process/sync.rs index 9debc45..7ef25ad 100644 --- a/pkg/kernel/src/process/sync.rs +++ b/pkg/kernel/src/process/sync.rs @@ -1,5 +1,4 @@ use alloc::{collections::BTreeMap, vec::Vec}; -use spin::Mutex; use super::ProcessId; once_mutex!(pub SEMAPHORES: BTreeMap); @@ -17,44 +16,38 @@ impl SemaphoreId { } } +/// Mutex is provided by the sem manager, +/// We need not to protect the count again pub struct Semaphore { - count: Mutex, + count: usize, wait_queue: Vec, } impl Semaphore { pub fn new(value: usize) -> Self { Self { - count: Mutex::new(value), + count: value, wait_queue: Vec::new() } } pub fn down(&mut self, pid: ProcessId) -> Result<(),()> { - if let Some(mut count) = self.count.try_lock() { - if *count == 0 { - self.wait_queue.push(pid); - Err(()) - } else { - *count -= 1; - // trace!("Semaphore down: {}", count); - Ok(()) - } + if self.count == 0 { + self.wait_queue.push(pid); + Err(()) } else { + self.count -= 1; + // trace!("Semaphore down: {}", count); Ok(()) } } pub fn up(&mut self) -> Option { - if let Some(mut count) = self.count.try_lock() { - // trace!("Semaphore up: {}", count); - if !self.wait_queue.is_empty() { - Some(self.wait_queue.pop().unwrap()) - } else { - *count += 1; - None - } + // trace!("Semaphore up: {}", count); + if !self.wait_queue.is_empty() { + Some(self.wait_queue.pop().unwrap()) } else { + self.count += 1; None } } @@ -62,6 +55,6 @@ impl Semaphore { impl core::fmt::Display for Semaphore { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!(f, "Semaphore({}) {:?}", self.count.lock(), self.wait_queue) + write!(f, "Semaphore({}) {:?}", self.count, self.wait_queue) } }