Recover set_priority and fork

master
WangRunji 6 years ago
parent 5db908b1c5
commit 80b161db98

@ -71,6 +71,11 @@ impl ProcessManager {
self.scheduler.lock().tick(pid) self.scheduler.lock().tick(pid)
} }
/// Set the priority of process `pid`
pub fn set_priority(&self, pid: Pid, priority: u8) {
self.scheduler.lock().set_priority(pid, priority);
}
/// Called by Processor to get a process to run. /// Called by Processor to get a process to run.
/// The manager first mark it `Running`, /// The manager first mark it `Running`,
/// then take out and return its Context. /// then take out and return its Context.

@ -78,6 +78,10 @@ impl Processor {
self.inner().proc.as_ref().unwrap().0 self.inner().proc.as_ref().unwrap().0
} }
pub fn context(&self) -> &Context {
&*self.inner().proc.as_ref().unwrap().1
}
pub fn manager(&self) -> &ProcessManager { pub fn manager(&self) -> &ProcessManager {
&*self.inner().manager &*self.inner().manager
} }

@ -12,13 +12,8 @@ pub struct ContextImpl {
impl Context for ContextImpl { impl Context for ContextImpl {
unsafe fn switch_to(&mut self, target: &mut Context) { unsafe fn switch_to(&mut self, target: &mut Context) {
use core::raw::TraitObject;
use core::mem::transmute; use core::mem::transmute;
let (target, _): (&mut ContextImpl, *const ()) = transmute(target);
// Cast &mut Context -> &mut ContextImpl
let raw: TraitObject = transmute(target);
let target = &mut *(raw.data as *mut ContextImpl);
self.arch.switch(&mut target.arch); self.arch.switch(&mut target.arch);
} }
} }

@ -19,7 +19,7 @@ pub fn init() {
extern fn idle(_arg: usize) -> ! { extern fn idle(_arg: usize) -> ! {
loop { cpu::halt(); } loop { cpu::halt(); }
} }
for i in 0..MAX_CPU_NUM { for i in 0..4 {
manager.add(ContextImpl::new_kernel(idle, i)); manager.add(ContextImpl::new_kernel(idle, i));
} }

@ -58,7 +58,11 @@ fn sys_close(fd: usize) -> i32 {
/// Fork the current process. Return the child's PID. /// Fork the current process. Return the child's PID.
fn sys_fork(tf: &TrapFrame) -> i32 { fn sys_fork(tf: &TrapFrame) -> i32 {
unimplemented!(); use core::mem::transmute;
let (context, _): (&ContextImpl, *const ()) = unsafe { transmute(processor().context()) };
let pid = processor().manager().add(context.fork(tf));
info!("fork: {} -> {}", thread::current().id(), pid);
pid as i32
} }
/// Wait the process exit. /// Wait the process exit.
@ -85,7 +89,7 @@ fn sys_getpid() -> i32 {
/// Exit the current process /// Exit the current process
fn sys_exit(exit_code: usize) -> i32 { fn sys_exit(exit_code: usize) -> i32 {
let pid = processor().pid(); let pid = thread::current().id();
processor().manager().set_status(pid, Status::Exited(exit_code)); processor().manager().set_status(pid, Status::Exited(exit_code));
processor().yield_now(); processor().yield_now();
0 0
@ -102,7 +106,8 @@ fn sys_get_time() -> i32 {
} }
fn sys_lab6_set_priority(priority: usize) -> i32 { fn sys_lab6_set_priority(priority: usize) -> i32 {
unimplemented!(); let pid = thread::current().id();
processor().manager().set_priority(pid, priority as u8);
0 0
} }

Loading…
Cancel
Save