Remove pipe && Fix cat and huge_write.

This commit is contained in:
Yifan Wu 2022-01-18 03:33:58 -08:00
parent 57f7debbc6
commit 5389b7adca
10 changed files with 7 additions and 332 deletions

View file

@ -21,14 +21,12 @@ pub fn main(argc: usize, argv: &[&str]) -> i32 {
panic!("Error occured when opening file");
}
let fd = fd as usize;
let mut buf = [0u8; 16];
let mut s = String::new();
let mut buf = [0u8; 256];
loop {
let size = read(fd, &mut buf) as usize;
if size == 0 { break; }
s.push_str(core::str::from_utf8(&buf[..size]).unwrap());
println!("{}", core::str::from_utf8(&buf[..size]).unwrap());
}
println!("{}", s);
close(fd);
0
}
}

View file

@ -18,7 +18,7 @@ pub fn main() -> i32 {
for i in 0..buffer.len() {
buffer[i] = i as u8;
}
let f = open("testf", OpenFlags::CREATE | OpenFlags::WRONLY);
let f = open("testf\0", OpenFlags::CREATE | OpenFlags::WRONLY);
if f < 0 {
panic!("Open test file failed!");
}
@ -33,4 +33,4 @@ pub fn main() -> i32 {
let speed_kbs = size_mb * 1000000 / time_ms;
println!("{}MiB written, time cost = {}ms, write speed = {}KiB/s", size_mb, time_ms, speed_kbs);
0
}
}

View file

@ -1,69 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
extern crate alloc;
use user_lib::{fork, close, pipe, read, write, wait, get_time};
use alloc::format;
const LENGTH: usize = 3000;
#[no_mangle]
pub fn main() -> i32 {
// create pipes
// parent write to child
let mut down_pipe_fd = [0usize; 2];
// child write to parent
let mut up_pipe_fd = [0usize; 2];
pipe(&mut down_pipe_fd);
pipe(&mut up_pipe_fd);
let mut random_str = [0u8; LENGTH];
if fork() == 0 {
// close write end of down pipe
close(down_pipe_fd[1]);
// close read end of up pipe
close(up_pipe_fd[0]);
assert_eq!(read(down_pipe_fd[0], &mut random_str) as usize, LENGTH);
close(down_pipe_fd[0]);
let sum: usize = random_str.iter().map(|v| *v as usize).sum::<usize>();
println!("sum = {}(child)", sum);
let sum_str = format!("{}", sum);
write(up_pipe_fd[1], sum_str.as_bytes());
close(up_pipe_fd[1]);
println!("Child process exited!");
0
} else {
// close read end of down pipe
close(down_pipe_fd[0]);
// 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;
}
// send it
assert_eq!(write(down_pipe_fd[1], &random_str) as usize, random_str.len());
// close write end of down pipe
close(down_pipe_fd[1]);
// calculate sum(parent)
let sum: usize = random_str.iter().map(|v| *v as usize).sum::<usize>();
println!("sum = {}(parent)", sum);
// recv sum(child)
let mut child_result = [0u8; 32];
let result_len = read(up_pipe_fd[0], &mut child_result) as usize;
close(up_pipe_fd[0]);
// check
assert_eq!(
sum,
str::parse::<usize>(
core::str::from_utf8(&child_result[..result_len]).unwrap()
).unwrap()
);
let mut _unused: i32 = 0;
wait(&mut _unused);
println!("pipe_large_test passed!");
0
}
}

View file

@ -1,44 +0,0 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use user_lib::{fork, close, pipe, read, write, wait};
static STR: &str = "Hello, world!";
#[no_mangle]
pub fn main() -> i32 {
// create pipe
let mut pipe_fd = [0usize; 2];
pipe(&mut pipe_fd);
// read end
assert_eq!(pipe_fd[0], 3);
// write end
assert_eq!(pipe_fd[1], 4);
if fork() == 0 {
// child process, read from parent
// close write_end
close(pipe_fd[1]);
let mut buffer = [0u8; 32];
let len_read = read(pipe_fd[0], &mut buffer) as usize;
// close read_end
close(pipe_fd[0]);
assert_eq!(core::str::from_utf8(&buffer[..len_read]).unwrap(), STR);
println!("Read OK, child process exited!");
0
} else {
// parent process, write to child
// close read end
close(pipe_fd[0]);
assert_eq!(write(pipe_fd[1], STR.as_bytes()), STR.len() as isize);
// close write end
close(pipe_fd[1]);
let mut child_exit_code: i32 = 0;
wait(&mut child_exit_code);
assert_eq!(child_exit_code, 0);
println!("pipetest passed!");
0
}
}

View file

@ -71,7 +71,6 @@ bitflags! {
pub fn dup(fd: usize) -> isize { sys_dup(fd) }
pub fn open(path: &str, flags: OpenFlags) -> isize { sys_open(path, flags.bits) }
pub fn close(fd: usize) -> isize { sys_close(fd) }
pub fn pipe(pipe_fd: &mut [usize]) -> isize { sys_pipe(pipe_fd) }
pub fn read(fd: usize, buf: &mut [u8]) -> isize { sys_read(fd, buf) }
pub fn write(fd: usize, buf: &[u8]) -> isize { sys_write(fd, buf) }
pub fn exit(exit_code: i32) -> ! { sys_exit(exit_code); }

View file

@ -3,7 +3,6 @@ use core::arch::asm;
const SYSCALL_DUP: usize = 24;
const SYSCALL_OPEN: usize = 56;
const SYSCALL_CLOSE: usize = 57;
const SYSCALL_PIPE: usize = 59;
const SYSCALL_READ: usize = 63;
const SYSCALL_WRITE: usize = 64;
const SYSCALL_EXIT: usize = 93;
@ -40,10 +39,6 @@ pub fn sys_close(fd: usize) -> isize {
syscall(SYSCALL_CLOSE, [fd, 0, 0])
}
pub fn sys_pipe(pipe: &mut [usize]) -> isize {
syscall(SYSCALL_PIPE, [pipe.as_mut_ptr() as usize, 0, 0])
}
pub fn sys_read(fd: usize, buffer: &mut [u8]) -> isize {
syscall(SYSCALL_READ, [fd, buffer.as_mut_ptr() as usize, buffer.len()])
}