本文记录阅读book-riscv-rec3.pdf的笔记

1 operatiing system interface

System Call Descriptor
int fork() Create a process, return child’s PID.
int exit(int status) Terminate the current process; status reported to wait(). No return.
int wait(int *status) Wait for a child to exit; exit status in *status; returns child PID.
int kill(int pid) Terminate process PID. Returns 0, or -1 for error.
int getpid() Return the current process’s PID.
int sleep(int n) Pause for n clock ticks.
int exec(char *file, char *argv[]) Load a file and execute it with arguments; only returns if error.
char *sbrk(int n) Grow process’s memory by n bytes. Returns start of new memory.
int open(char *file, int flags) Open a file; flags indicate read/write; returns an fd (file descriptor).
int write(int fd, char *buf, int n) Write n bytes from buf to file descriptor fd; returns n.
int read(int fd, char *buf, int n) Read n bytes into buf; returns number read; or 0 if end of file.
int close(int fd) Release open file fd.
int dup(int fd) Return a new file descriptor referring to the same file as fd.
int pipe(int p[]) Create a pipe, put read/write file descriptors in p[0] and p[1].
int chdir(char *dir) Change the current directory.
int mkdir(char *dir) Create a new directory.
int mknod(char *file, int, int) Create a device file.
int fstat(int fd, struct stat *st) Place info about an open file into *st.
int stat(char *file, struct stat *st) Place info about a named file into *st.
int link(char *file1, char *file2) Create another name (file2) for the file file1.
int unlink(char *file) Remove a file.

xv6使用fork()系统调用创建子进程。

fork()调用一次,返回两次。父进程接受到的fork()的返回值是子进程的pid。子进程接收到的返回值是0。

因此根据fork()的返回值就可以判断出fork()的返回点。

wait()系统调用作用是等待子进程结束。

1.2 I/O and File descriptors

xv6 kernel中使用正整数作为文件描述符。系统中通过文件描述符来统一管理系统中的文件、块设备等。进程启动时默认已打开如下三个文件描述符:

  • 0表示标准输入

  • 1表示标准输出

  • 2表示标准错误输出

操作文件最关键的几个系统调用有:open(), read(), write(), close().

dup()系统调用的作用是复制一个已经存在的文件描述符并且共享文件描述对应的offset信息,操作dup()复制的文件描述符等同于操作原始文件描述符。

1.3 pipes

1.4 File system

1.5 Real world