Skip to content

Commit

Permalink
Add Notes
Browse files Browse the repository at this point in the history
  • Loading branch information
ToniXWD committed Nov 2, 2023
1 parent 1551d0e commit ac4afad
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions os/src/fs/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,22 @@ const RING_BUFFER_SIZE: usize = 32;

#[derive(Copy, Clone, PartialEq)]
enum RingBufferStatus {
/// 缓冲区已满不能再继续写入
Full,
/// 缓冲区为空无法从里面读取
Empty,
/// 除了 FULL 和 EMPTY 之外的其他状态
Normal,
}

pub struct PipeRingBuffer {
arr: [u8; RING_BUFFER_SIZE],
/// 循环队列队头的下标
head: usize,
/// 为循环队列队尾的下标
tail: usize,
status: RingBufferStatus,
// 写端的一个弱引用计数
write_end: Option<Weak<Pipe>>,
}

Expand Down Expand Up @@ -95,6 +101,7 @@ impl PipeRingBuffer {
}
}
pub fn all_write_ends_closed(&self) -> bool {
// 如果升级失败的话,说明管道写端的强引用计数为 0
self.write_end.as_ref().unwrap().upgrade().is_none()
}
}
Expand Down Expand Up @@ -127,6 +134,7 @@ impl File for Pipe {
if ring_buffer.all_write_ends_closed() {
return already_read;
}
// 需要手动释放管道自身的锁,因为切换任务时候的 __switch 并不是一个正常的函数调用
drop(ring_buffer);
suspend_current_and_run_next();
continue;
Expand Down
6 changes: 6 additions & 0 deletions os/src/task/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ impl TaskControlBlockInner {
self.get_status() == TaskStatus::Zombie
}
pub fn alloc_fd(&mut self) -> usize {
// 在进程控制块中分配一个最小的空闲文件描述符来访问一个新打开的文件
if let Some(fd) = (0..self.fd_table.len()).find(|fd| self.fd_table[*fd].is_none()) {
fd
} else {
// 如果没有的话就需要拓展文件描述符表的长度并新分配一个
self.fd_table.push(None);
self.fd_table.len() - 1
}
Expand Down Expand Up @@ -184,6 +186,7 @@ impl TaskControlBlock {
// push arguments on user stack
user_sp -= (args.len() + 1) * core::mem::size_of::<usize>();
let argv_base = user_sp;
// argv是一个指针数组, 指向了存放在栈上的argv[i]
let mut argv: Vec<_> = (0..=args.len())
.map(|arg| {
translated_refmut(
Expand Down Expand Up @@ -220,7 +223,10 @@ impl TaskControlBlock {
self.kernel_stack.get_top(),
trap_handler as usize,
);
// 用户栈结构参考: http://learningos.cn/rCore-Tutorial-Guide-2023A/_images/user-stack-cmdargs.png
// 让 a0 表示命令行参数的个数
trap_cx.x[10] = args.len();
// a1 则表示图中 argv_base 即蓝色区域的起始地址
trap_cx.x[11] = argv_base;
*inner.get_trap_cx() = trap_cx;
// **** release current PCB
Expand Down
1 change: 1 addition & 0 deletions user
Submodule user added at 1163ee

0 comments on commit ac4afad

Please sign in to comment.