Rewrite Stdin/Stdout

This commit is contained in:
Yifan Wu 2020-12-13 15:07:19 +08:00
parent e5bd98973d
commit e93a4a0b76
8 changed files with 338 additions and 42 deletions

View file

@ -4,8 +4,10 @@ use crate::config::{TRAP_CONTEXT};
use super::TaskContext;
use super::{PidHandle, pid_alloc, KernelStack};
use alloc::sync::{Weak, Arc};
use alloc::vec;
use alloc::vec::Vec;
use spin::{Mutex, MutexGuard};
use crate::fs::{File, Stdin, Stdout};
pub struct TaskControlBlock {
// immutable
@ -24,6 +26,7 @@ pub struct TaskControlBlockInner {
pub parent: Option<Weak<TaskControlBlock>>,
pub children: Vec<Arc<TaskControlBlock>>,
pub exit_code: i32,
pub fd_table: Vec<Option<Arc<dyn File + Send + Sync>>>,
}
impl TaskControlBlockInner {
@ -74,6 +77,10 @@ impl TaskControlBlock {
parent: None,
children: Vec::new(),
exit_code: 0,
fd_table: vec![
Some(Arc::new(Stdin)),
Some(Arc::new(Stdout)),
],
}),
};
// prepare TrapContext in user space
@ -136,6 +143,15 @@ impl TaskControlBlock {
let kernel_stack_top = kernel_stack.get_top();
// push a goto_trap_return task_cx on the top of kernel stack
let task_cx_ptr = kernel_stack.push_on_top(TaskContext::goto_trap_return());
// copy fd table
let mut new_fd_table: Vec<Option<Arc<dyn File + Send + Sync>>> = Vec::new();
for fd in parent_inner.fd_table.iter() {
if let Some(file) = fd {
new_fd_table.push(Some(file.clone()));
} else {
new_fd_table.push(None);
}
}
let task_control_block = Arc::new(TaskControlBlock {
pid: pid_handle,
kernel_stack,
@ -148,6 +164,7 @@ impl TaskControlBlock {
parent: Some(Arc::downgrade(self)),
children: Vec::new(),
exit_code: 0,
fd_table: new_fd_table,
}),
});
// add child