mirror of
https://github.com/rcore-os/rCore-Tutorial-v3.git
synced 2025-01-18 21:17:14 +04:00
Merge recent update from ch7 && cargo clippy
This commit is contained in:
parent
514c110a28
commit
740730e7f7
@ -61,7 +61,7 @@ fn easy_fs_pack() -> std::io::Result<()> {
|
||||
})));
|
||||
// 16MiB, at most 4095 files
|
||||
let efs = EasyFileSystem::create(
|
||||
block_file.clone(),
|
||||
block_file,
|
||||
16 * 2048,
|
||||
1,
|
||||
);
|
||||
|
@ -17,7 +17,7 @@ pub struct Bitmap {
|
||||
/// Return (block_pos, bits64_pos, inner_pos)
|
||||
fn decomposition(mut bit: usize) -> (usize, usize, usize) {
|
||||
let block_pos = bit / BLOCK_BITS;
|
||||
bit = bit % BLOCK_BITS;
|
||||
bit %= BLOCK_BITS;
|
||||
(block_pos, bit / 64, bit % 64)
|
||||
}
|
||||
|
||||
|
@ -289,33 +289,24 @@ impl DiskInode {
|
||||
.lock()
|
||||
.modify(0, |indirect2: &mut IndirectBlock| {
|
||||
// full indirect1 blocks
|
||||
for i in 0..a1 {
|
||||
v.push(indirect2[i]);
|
||||
get_block_cache(
|
||||
indirect2[i] as usize,
|
||||
Arc::clone(block_device),
|
||||
)
|
||||
for entry in indirect2.iter_mut().take(a1) {
|
||||
v.push(*entry);
|
||||
get_block_cache(*entry as usize, Arc::clone(block_device))
|
||||
.lock()
|
||||
.modify(0, |indirect1: &mut IndirectBlock| {
|
||||
for j in 0..INODE_INDIRECT1_COUNT {
|
||||
v.push(indirect1[j]);
|
||||
//indirect1[j] = 0;
|
||||
for entry in indirect1.iter() {
|
||||
v.push(*entry);
|
||||
}
|
||||
});
|
||||
//indirect2[i] = 0;
|
||||
}
|
||||
// last indirect1 block
|
||||
if b1 > 0 {
|
||||
v.push(indirect2[a1]);
|
||||
get_block_cache(
|
||||
indirect2[a1] as usize,
|
||||
Arc::clone(block_device),
|
||||
)
|
||||
get_block_cache(indirect2[a1] as usize, Arc::clone(block_device))
|
||||
.lock()
|
||||
.modify(0, |indirect1: &mut IndirectBlock| {
|
||||
for j in 0..b1 {
|
||||
v.push(indirect1[j]);
|
||||
//indirect1[j] = 0;
|
||||
for entry in indirect1.iter().take(b1) {
|
||||
v.push(*entry);
|
||||
}
|
||||
});
|
||||
//indirect2[a1] = 0;
|
||||
|
@ -110,12 +110,13 @@ impl Inode {
|
||||
|
||||
pub fn create(&self, name: &str) -> Option<Arc<Inode>> {
|
||||
let mut fs = self.fs.lock();
|
||||
if self.modify_disk_inode(|root_inode| {
|
||||
let op = |root_inode: &mut DiskInode| {
|
||||
// assert it is a directory
|
||||
assert!(root_inode.is_dir());
|
||||
// has the file been created?
|
||||
self.find_inode_id(name, root_inode)
|
||||
}).is_some() {
|
||||
};
|
||||
if self.modify_disk_inode(op).is_some() {
|
||||
return None;
|
||||
}
|
||||
// create a new file
|
||||
|
@ -78,14 +78,12 @@ impl PipeRingBuffer {
|
||||
pub fn available_read(&self) -> usize {
|
||||
if self.status == RingBufferStatus::EMPTY {
|
||||
0
|
||||
} else {
|
||||
if self.tail > self.head {
|
||||
} else if self.tail > self.head {
|
||||
self.tail - self.head
|
||||
} else {
|
||||
self.tail + RING_BUFFER_SIZE - self.head
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn available_write(&self) -> usize {
|
||||
if self.status == RingBufferStatus::FULL {
|
||||
0
|
||||
|
@ -62,15 +62,13 @@ impl FrameAllocator for StackFrameAllocator {
|
||||
fn alloc(&mut self) -> Option<PhysPageNum> {
|
||||
if let Some(ppn) = self.recycled.pop() {
|
||||
Some(ppn.into())
|
||||
} else {
|
||||
if self.current == self.end {
|
||||
} else if self.current == self.end {
|
||||
None
|
||||
} else {
|
||||
self.current += 1;
|
||||
Some((self.current - 1).into())
|
||||
}
|
||||
}
|
||||
}
|
||||
fn dealloc(&mut self, ppn: PhysPageNum) {
|
||||
let ppn = ppn.0;
|
||||
// validity check
|
||||
|
@ -9,8 +9,8 @@ extern crate user_lib;
|
||||
#[no_mangle]
|
||||
pub fn main(argc: usize, argv: &[&str]) -> i32 {
|
||||
println!("argc = {}", argc);
|
||||
for i in 0..argc {
|
||||
println!("argv[{}] = {}", i, argv[i]);
|
||||
for (i, arg) in argv.iter().enumerate() {
|
||||
println!("argv[{}] = {}", i, arg);
|
||||
}
|
||||
0
|
||||
}
|
@ -15,8 +15,8 @@ use user_lib::{
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
let mut buffer = [0u8; 1024]; // 1KiB
|
||||
for i in 0..buffer.len() {
|
||||
buffer[i] = i as u8;
|
||||
for (i, ch) in buffer.iter_mut().enumerate() {
|
||||
*ch = i as u8;
|
||||
}
|
||||
let f = open("testf\0", OpenFlags::CREATE | OpenFlags::WRONLY);
|
||||
if f < 0 {
|
||||
|
@ -1,7 +1,6 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
|
||||
use user_lib::{
|
||||
@ -14,7 +13,7 @@ use user_lib::{
|
||||
#[no_mangle]
|
||||
fn main() -> i32 {
|
||||
if fork() == 0 {
|
||||
exec("user_shell\0", &[0 as *const u8]);
|
||||
exec("user_shell\0", &[core::ptr::null::<u8>()]);
|
||||
} else {
|
||||
loop {
|
||||
let mut exit_code: i32 = 0;
|
||||
@ -23,11 +22,13 @@ fn main() -> i32 {
|
||||
yield_();
|
||||
continue;
|
||||
}
|
||||
/*
|
||||
println!(
|
||||
"[initproc] Released a zombie process, pid={}, exit_code={}",
|
||||
pid,
|
||||
exit_code,
|
||||
);
|
||||
*/
|
||||
}
|
||||
}
|
||||
0
|
||||
|
@ -1,5 +1,6 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![allow(clippy::needless_range_loop)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
|
@ -1,5 +1,6 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![allow(clippy::println_empty_string)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
|
@ -1,5 +1,6 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![allow(clippy::println_empty_string)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
@ -73,17 +74,17 @@ pub fn main() -> i32 {
|
||||
print!("#{}:", id);
|
||||
for j in 0..time_cost/GRAPH_SCALE {
|
||||
let current_time = j * GRAPH_SCALE + start;
|
||||
if (0..ROUND).find(|round| unsafe {
|
||||
if (0..ROUND).any(|round| unsafe {
|
||||
let start_thinking = THINK[id][2 * round];
|
||||
let end_thinking = THINK[id][2 * round + 1];
|
||||
start_thinking <= current_time && current_time <= end_thinking
|
||||
}).is_some() {
|
||||
}) {
|
||||
print!("-");
|
||||
} else if (0..ROUND).find(|round| unsafe {
|
||||
} else if (0..ROUND).any(|round| unsafe {
|
||||
let start_eating = EAT[id][2 * round];
|
||||
let end_eating = EAT[id][2 * round + 1];
|
||||
start_eating <= current_time && current_time <= end_eating
|
||||
}).is_some() {
|
||||
}) {
|
||||
print!("x");
|
||||
} else {
|
||||
print!(" ");
|
||||
|
@ -40,8 +40,8 @@ pub fn main() -> i32 {
|
||||
// close write end of up pipe
|
||||
close(up_pipe_fd[1]);
|
||||
// generate a long random string
|
||||
for i in 0..LENGTH {
|
||||
random_str[i] = get_time() as u8;
|
||||
for ch in random_str.iter_mut() {
|
||||
*ch = get_time() as u8;
|
||||
}
|
||||
// send it
|
||||
assert_eq!(write(down_pipe_fd[1], &random_str) as usize, random_str.len());
|
||||
|
@ -10,7 +10,7 @@ use user_lib::{fork, exec, wait};
|
||||
pub fn main() -> i32 {
|
||||
for i in 0..1000 {
|
||||
if fork() == 0 {
|
||||
exec("pipe_large_test\0", &[0 as *const u8]);
|
||||
exec("pipe_large_test\0", &[core::ptr::null::<u8>()]);
|
||||
} else {
|
||||
let mut _unused: i32 = 0;
|
||||
wait(&mut _unused);
|
||||
|
@ -9,7 +9,7 @@ extern crate alloc;
|
||||
use user_lib::{semaphore_create, semaphore_up, semaphore_down};
|
||||
use user_lib::{thread_create, waittid, sleep};
|
||||
use user_lib::exit;
|
||||
use alloc::vec::Vec;
|
||||
use alloc::vec;
|
||||
|
||||
const SEM_SYNC: usize = 0;
|
||||
|
||||
@ -33,9 +33,10 @@ pub fn main() -> i32 {
|
||||
// create semaphores
|
||||
assert_eq!(semaphore_create(0) as usize, SEM_SYNC);
|
||||
// create threads
|
||||
let mut threads = Vec::new();
|
||||
threads.push(thread_create(first as usize, 0));
|
||||
threads.push(thread_create(second as usize, 0));
|
||||
let threads = vec![
|
||||
thread_create(first as usize, 0),
|
||||
thread_create(second as usize, 0),
|
||||
];
|
||||
// wait for all threads to complete
|
||||
for thread in threads.iter() {
|
||||
waittid(*thread as usize);
|
||||
|
@ -9,7 +9,7 @@ extern crate alloc;
|
||||
use user_lib::{condvar_create, condvar_signal, condvar_wait, mutex_blocking_create, mutex_lock, mutex_unlock};
|
||||
use user_lib::{thread_create, waittid, sleep};
|
||||
use user_lib::exit;
|
||||
use alloc::vec::Vec;
|
||||
use alloc::vec;
|
||||
|
||||
static mut A: usize = 0;
|
||||
|
||||
@ -44,9 +44,10 @@ pub fn main() -> i32 {
|
||||
assert_eq!(condvar_create() as usize, CONDVAR_ID);
|
||||
assert_eq!(mutex_blocking_create() as usize, MUTEX_ID);
|
||||
// create threads
|
||||
let mut threads = Vec::new();
|
||||
threads.push(thread_create(first as usize, 0));
|
||||
threads.push(thread_create(second as usize, 0));
|
||||
let threads = vec![
|
||||
thread_create(first as usize, 0),
|
||||
thread_create(second as usize, 0),
|
||||
];
|
||||
// wait for all threads to complete
|
||||
for thread in threads.iter() {
|
||||
waittid(*thread as usize);
|
||||
|
@ -6,7 +6,7 @@ extern crate user_lib;
|
||||
extern crate alloc;
|
||||
|
||||
use user_lib::{thread_create, waittid, exit};
|
||||
use alloc::vec::Vec;
|
||||
use alloc::vec;
|
||||
|
||||
pub fn thread_a() -> ! {
|
||||
for _ in 0..1000 { print!("a"); }
|
||||
@ -25,10 +25,11 @@ pub fn thread_c() -> ! {
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
let mut v = Vec::new();
|
||||
v.push(thread_create(thread_a as usize, 0));
|
||||
v.push(thread_create(thread_b as usize, 0));
|
||||
v.push(thread_create(thread_c as usize, 0));
|
||||
let v = vec![
|
||||
thread_create(thread_a as usize, 0),
|
||||
thread_create(thread_b as usize, 0),
|
||||
thread_create(thread_c as usize, 0),
|
||||
];
|
||||
for tid in v.iter() {
|
||||
let exit_code = waittid(*tid as usize);
|
||||
println!("thread#{} exited with code {}", tid, exit_code);
|
||||
|
@ -27,8 +27,8 @@ pub fn main() -> i32 {
|
||||
Argument { ch: 'b', rc: 2, },
|
||||
Argument { ch: 'c', rc: 3, },
|
||||
];
|
||||
for i in 0..3 {
|
||||
v.push(thread_create(thread_print as usize, &args[i] as *const _ as usize));
|
||||
for arg in args.iter() {
|
||||
v.push(thread_create(thread_print as usize, arg as *const _ as usize));
|
||||
}
|
||||
for tid in v.iter() {
|
||||
let exit_code = waittid(*tid as usize);
|
||||
|
@ -1,5 +1,7 @@
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![allow(clippy::println_empty_string)]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
@ -10,53 +12,42 @@ const LF: u8 = 0x0au8;
|
||||
const CR: u8 = 0x0du8;
|
||||
const DL: u8 = 0x7fu8;
|
||||
const BS: u8 = 0x08u8;
|
||||
const LINE_START: &str = ">> ";
|
||||
|
||||
use alloc::string::String;
|
||||
use alloc::vec::Vec;
|
||||
use user_lib::{
|
||||
fork,
|
||||
exec,
|
||||
waitpid,
|
||||
open,
|
||||
OpenFlags,
|
||||
close,
|
||||
dup,
|
||||
};
|
||||
use user_lib::console::getchar;
|
||||
use user_lib::{close, dup, exec, fork, open, pipe, waitpid, OpenFlags};
|
||||
|
||||
#[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() {
|
||||
let args: Vec<_> = line.as_str().split(' ').collect();
|
||||
#[derive(Debug)]
|
||||
struct ProcessArguments {
|
||||
input: String,
|
||||
output: String,
|
||||
args_copy: Vec<String>,
|
||||
args_addr: Vec<*const u8>,
|
||||
}
|
||||
|
||||
impl ProcessArguments {
|
||||
pub fn new(command: &str) -> Self {
|
||||
let args: Vec<_> = command.split(' ').collect();
|
||||
let mut args_copy: Vec<String> = args
|
||||
.iter()
|
||||
.filter(|&arg| !arg.is_empty())
|
||||
.map(|&arg| {
|
||||
let mut string = String::new();
|
||||
string.push_str(arg);
|
||||
string.push('\0');
|
||||
string
|
||||
})
|
||||
.collect();
|
||||
|
||||
args_copy
|
||||
.iter_mut()
|
||||
.for_each(|string| {
|
||||
string.push('\0');
|
||||
});
|
||||
|
||||
// redirect input
|
||||
let mut input = String::new();
|
||||
if let Some((idx, _)) = args_copy
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, arg)| arg.as_str() == "<\0") {
|
||||
.find(|(_, arg)| arg.as_str() == "<\0")
|
||||
{
|
||||
input = args_copy[idx + 1].clone();
|
||||
args_copy.drain(idx..=idx + 1);
|
||||
}
|
||||
@ -66,19 +57,79 @@ pub fn main() -> i32 {
|
||||
if let Some((idx, _)) = args_copy
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, arg)| arg.as_str() == ">\0") {
|
||||
.find(|(_, arg)| arg.as_str() == ">\0")
|
||||
{
|
||||
output = args_copy[idx + 1].clone();
|
||||
args_copy.drain(idx..=idx + 1);
|
||||
}
|
||||
|
||||
let mut args_addr: Vec<*const u8> = args_copy
|
||||
let mut args_addr: Vec<*const u8> = args_copy.iter().map(|arg| arg.as_ptr()).collect();
|
||||
args_addr.push(core::ptr::null::<u8>());
|
||||
|
||||
Self {
|
||||
input,
|
||||
output,
|
||||
args_copy,
|
||||
args_addr,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
println!("Rust user shell");
|
||||
let mut line: String = String::new();
|
||||
print!("{}", LINE_START);
|
||||
loop {
|
||||
let c = getchar();
|
||||
match c {
|
||||
LF | CR => {
|
||||
println!("");
|
||||
if !line.is_empty() {
|
||||
let splited: Vec<_> = line.as_str().split('|').collect();
|
||||
let process_arguments_list: Vec<_> = splited
|
||||
.iter()
|
||||
.map(|arg| arg.as_ptr())
|
||||
.map(|&cmd| ProcessArguments::new(cmd))
|
||||
.collect();
|
||||
args_addr.push(0 as *const u8);
|
||||
let mut valid = true;
|
||||
for (i, process_args) in process_arguments_list.iter().enumerate() {
|
||||
if i == 0 {
|
||||
if !process_args.output.is_empty() {
|
||||
valid = false;
|
||||
}
|
||||
} else if i == process_arguments_list.len() - 1 {
|
||||
if !process_args.input.is_empty() {
|
||||
valid = false;
|
||||
}
|
||||
} else if !process_args.output.is_empty() || !process_args.input.is_empty()
|
||||
{
|
||||
valid = false;
|
||||
}
|
||||
}
|
||||
if process_arguments_list.len() == 1 {
|
||||
valid = true;
|
||||
}
|
||||
if !valid {
|
||||
println!("Invalid command: Inputs/Outputs cannot be correctly binded!");
|
||||
} else {
|
||||
// create pipes
|
||||
let mut pipes_fd: Vec<[usize; 2]> = Vec::new();
|
||||
if !process_arguments_list.is_empty() {
|
||||
for _ in 0..process_arguments_list.len() - 1 {
|
||||
let mut pipe_fd = [0usize; 2];
|
||||
pipe(&mut pipe_fd);
|
||||
pipes_fd.push(pipe_fd);
|
||||
}
|
||||
}
|
||||
let mut children: Vec<_> = Vec::new();
|
||||
for (i, process_argument) in process_arguments_list.iter().enumerate() {
|
||||
let pid = fork();
|
||||
if pid == 0 {
|
||||
// input redirection
|
||||
let input = &process_argument.input;
|
||||
let output = &process_argument.output;
|
||||
let args_copy = &process_argument.args_copy;
|
||||
let args_addr = &process_argument.args_addr;
|
||||
// redirect input
|
||||
if !input.is_empty() {
|
||||
let input_fd = open(input.as_str(), OpenFlags::RDONLY);
|
||||
if input_fd == -1 {
|
||||
@ -90,11 +141,11 @@ pub fn main() -> i32 {
|
||||
assert_eq!(dup(input_fd), 0);
|
||||
close(input_fd);
|
||||
}
|
||||
// output redirection
|
||||
// redirect output
|
||||
if !output.is_empty() {
|
||||
let output_fd = open(
|
||||
output.as_str(),
|
||||
OpenFlags::CREATE | OpenFlags::WRONLY
|
||||
OpenFlags::CREATE | OpenFlags::WRONLY,
|
||||
);
|
||||
if output_fd == -1 {
|
||||
println!("Error when opening file {}", output);
|
||||
@ -105,21 +156,47 @@ pub fn main() -> i32 {
|
||||
assert_eq!(dup(output_fd), 1);
|
||||
close(output_fd);
|
||||
}
|
||||
// child process
|
||||
// receive input from the previous process
|
||||
if i > 0 {
|
||||
close(0);
|
||||
let read_end = pipes_fd.get(i - 1).unwrap()[0];
|
||||
assert_eq!(dup(read_end), 0);
|
||||
}
|
||||
// send output to the next process
|
||||
if i < process_arguments_list.len() - 1 {
|
||||
close(1);
|
||||
let write_end = pipes_fd.get(i).unwrap()[1];
|
||||
assert_eq!(dup(write_end), 1);
|
||||
}
|
||||
// close all pipe ends inherited from the parent process
|
||||
for pipe_fd in pipes_fd.iter() {
|
||||
close(pipe_fd[0]);
|
||||
close(pipe_fd[1]);
|
||||
}
|
||||
// execute new application
|
||||
if exec(args_copy[0].as_str(), args_addr.as_slice()) == -1 {
|
||||
println!("Error when executing!");
|
||||
return -4;
|
||||
}
|
||||
unreachable!();
|
||||
} else {
|
||||
children.push(pid);
|
||||
}
|
||||
}
|
||||
for pipe_fd in pipes_fd.iter() {
|
||||
close(pipe_fd[0]);
|
||||
close(pipe_fd[1]);
|
||||
}
|
||||
let mut exit_code: i32 = 0;
|
||||
for pid in children.into_iter() {
|
||||
let exit_pid = waitpid(pid as usize, &mut exit_code);
|
||||
assert_eq!(pid, exit_pid);
|
||||
println!("Shell: Process {} exited with code {}", pid, exit_code);
|
||||
//println!("Shell: Process {} exited with code {}", pid, exit_code);
|
||||
}
|
||||
}
|
||||
line.clear();
|
||||
}
|
||||
print!(">> ");
|
||||
print!("{}", LINE_START);
|
||||
}
|
||||
BS | DL => {
|
||||
if !line.is_empty() {
|
||||
@ -136,3 +213,4 @@ pub fn main() -> i32 {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,7 @@ pub fn main() -> i32 {
|
||||
println!("Usertests: Running {}", test);
|
||||
let pid = fork();
|
||||
if pid == 0 {
|
||||
exec(*test, &[0 as *const u8]);
|
||||
exec(*test, &[core::ptr::null::<u8>()]);
|
||||
panic!("unreachable!");
|
||||
} else {
|
||||
let mut exit_code: i32 = Default::default();
|
||||
|
Loading…
Reference in New Issue
Block a user