引言

本章导读

本章将基于文件描述符实现父子进程之间的通信机制——管道。 我们还将扩展 exec 系统调用,使之能传递运行参数,并进一步改进 shell 程序,使其支持重定向符号 ><

实践体验

获取本章代码:

$ git clone https://github.com/LearningOS/rCore-Tutorial-Code-2023A.git
$ cd rCore-Tutorial-Code-2023A
$ git checkout ch7

在 qemu 模拟器上运行本章代码:

$ cd os
$ make run

进入shell程序后,可以运行管道机制的简单测例 ch7b_pipetestch7b_pipetest 需要保证父进程通过管道传输给子进程的字符串不会发生变化。

测例输出大致如下:

>> ch7b_pipetest
Read OK, child process exited!
pipetest passed!
Shell: Process 2 exited with code 0
>>

同样的,也可以运行较为复杂的测例 ch7b_pipe_large_test,体验通过两个管道实现双向通信。

此外,在本章我们为shell程序支持了输入/输出重定向功能,可以将一个应用的输出保存到一个指定的文件。例如,下面的命令可以将 ch7b_yield 应用的输出保存在文件 fileb 当中,并在应用执行完毕之后确认它的输出:

>> ch7b_yield > fileb
Shell: Process 2 exited with code 0
>> ch7b_cat fileb
Hello, I am process 2.
Back in process 2, iteration 0.
Back in process 2, iteration 1.
Back in process 2, iteration 2.
Back in process 2, iteration 3.
Back in process 2, iteration 4.
yield pass.

Shell: Process 2 exited with code 0
>>

本章代码树

 ── os
    └── src
        ├── ...
        ├── fs
        │   ├── inode.rs
        │   ├── mod.rs
        │   ├── pipe.rs(新增:实现了 File Trait 的第三个实现——可用来进程间通信的管道)
        │   └── stdio.rs
        ├── mm
        │   ├── address.rs
        │   ├── frame_allocator.rs
        │   ├── heap_allocator.rs
        │   ├── memory_set.rs
        │   ├── mod.rs
        │   └── page_table.rs
        ├── syscall
        │   ├── fs.rs(修改:添加了sys_pipe和sys_dup)
        │   ├── mod.rs
        │   └── process.rs(修改:sys_exec添加了对参数的支持)
        ├── task
            ├── context.rs
            ├── manager.rs
            ├── mod.rs
            ├── pid.rs
            ├── processor.rs
            ├── switch.rs
            ├── switch.S
            └── task.rs(修改:在exec中将参数压入用户栈中)

cloc easy-fs os
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Rust                            42            317            434           3574
Assembly                         4             53             26            526
make                             1             13              4             48
TOML                             2              4              2             23
-------------------------------------------------------------------------------
SUM:                            49            387            466           4171
-------------------------------------------------------------------------------