Add user program initproc/user_shell, allow user programs allocate data on heap.

This commit is contained in:
Yifan Wu 2020-12-09 09:56:06 +08:00
parent eddbc8954c
commit 81bef97f09
7 changed files with 170 additions and 15 deletions

View file

@ -4,14 +4,14 @@
#[macro_use]
extern crate user_lib;
use user_lib::{sys_get_time, sys_yield};
use user_lib::{get_time, yield_};
#[no_mangle]
fn main() -> i32 {
let current_timer = sys_get_time();
let current_timer = get_time();
let wait_for = current_timer + 10000000;
while sys_get_time() < wait_for {
sys_yield();
while get_time() < wait_for {
yield_();
}
println!("Test sleep OK!");
0

30
user/src/bin/initproc.rs Normal file
View file

@ -0,0 +1,30 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::{
fork,
wait,
exec,
yield_,
};
#[no_mangle]
fn main() -> i32 {
if fork() == 0 {
exec("user_shell\0");
} else {
loop {
let mut xstatus: i32 = 0;
let pid = wait(&mut xstatus);
if pid == -1 {
yield_();
continue;
}
println!("[initproc] Release a zombie process!");
}
}
0
}

View file

@ -0,0 +1,61 @@
#![no_std]
#![no_main]
extern crate alloc;
#[macro_use]
extern crate user_lib;
const LF: u8 = 0x0au8;
const CR: u8 = 0x0du8;
const DL: u8 = 0x7fu8;
const BS: u8 = 0x08u8;
use alloc::string::String;
use user_lib::{fork, exec, wait};
use user_lib::console::getchar;
#[no_mangle]
pub fn main() -> i32 {
println!("Rust user shell");
let mut line: String = String::new();
print!(">> ");
loop {
let c = getchar();
match c {
LF | CR => {
println!("");
if !line.is_empty() {
line.push('\0');
let pid = fork();
if pid == 0 {
// child process
if exec(line.as_str()) == -1 {
println!("Command not found!");
return 0;
}
unreachable!();
} else {
let mut xstate: i32 = 0;
wait(&mut xstate);
println!("Shell: Process {} exited with code {}", pid, xstate);
}
line.clear();
}
print!(">> ");
}
DL => {
if !line.is_empty() {
print!("{}", BS as char);
print!(" ");
print!("{}", BS as char);
line.pop();
}
}
_ => {
print!("{}", c as char);
line.push(c as char);
}
}
}
}