前言
该Lab通过实现几个命令来熟悉 xv6 及其系统调用
- sleep
- pingpong
- primes
- find
- xargs
官方实验指导:https://pdos.csail.mit.edu/6.S081/2021/labs/util.html
个人代码实现仓库:https://github.com/kerolt/xv6-labs-2023
环境搭建
使用docker创建ubuntu20.04容器,后执行:
1
| apt install git build-essential gdb-multiarch qemu-system-misc gcc-riscv64-linux-gnu binutils-riscv64-linux-gnu
|
之后测试一下:
- 首先下载xv6源码:
git clone https://github.com/mit-pdos/xv6-riscv.git
- 运行:
make qemu
,如果结果如下,说明成功,按下ctrl + a
和x
退出qemu
测试
对于完成的程序,如果想要测试,则在Makefile
中的UPROGS中添加:
其中的xxx即为程序的名称,如sleep,则为$U/_sleep\
。
之后,可使用如下方法进行测试:
sleep (easy)
练前开胃菜,使用sleep系统调用。
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include "kernel/types.h" #include "kernel/stat.h" #include "user/user.h"
int main(int argc, char* argv[]) { if (argc != 2) { printf("Usage: sleep <seconds>\n"); exit(1); } int time = atoi(argv[1]); sleep(time); return exit(0); }
|
pingpong (easy)
编写一个程序,使用 UNIX 系统调用在两个进程之间通过一对管道 "乒乓 "传送一个字节,每个管道一个方向。
该程序需要注意的就是对于两个管道的操作,何时关,关哪个?读写顺序又如何?
- 对于父进程,应该先写再读
- 对于子进程,应该先读再写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #include "kernel/types.h" #include "kernel/stat.h" #include "user/user.h"
#define R 0 #define W 1
int main() { int p2c[2], c2p[2]; pipe(p2c); pipe(c2p);
int pid = fork(); if (pid == 0) { char buf[32] = {0}; close(c2p[R]); close(p2c[W]);
read(p2c[R], buf, sizeof(buf)); close(p2c[R]); printf("%d: received ping\n", getpid()); write(c2p[W], "pong", 4); close(c2p[W]);
exit(0); } else { char buf[32] = {0}; close(p2c[R]); close(c2p[W]);
write(p2c[W], "ping", 4); close(p2c[W]); read(c2p[R], buf, sizeof(buf)); printf("%d: received pong\n", getpid()); close(c2p[R]);
exit(0); } }
|
primes (moderate/hard)
使用管道编写并发版质数筛。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #include "kernel/types.h" #include "kernel/stat.h" #include "user/user.h"
void Filter(int pipe_fd[2]) { close(pipe_fd[1]); int prime; read(pipe_fd[0], &prime, 4); printf("prime %d\n", prime);
int num; if (read(pipe_fd[0], &num, 4) == 0) { exit(0); }
int new_pipe_fd[2]; pipe(new_pipe_fd); int pid = fork(); if (pid == -1) { printf("Fork error!\n"); exit(1); } else if (pid == 0) { Filter(new_pipe_fd); } else { close(new_pipe_fd[0]); if (num % prime != 0) { write(new_pipe_fd[1], &num, 4); } while (read(pipe_fd[0], &num, 4) > 0) { if (num % prime != 0) { write(new_pipe_fd[1], &num, 4); } } close(new_pipe_fd[1]); close(pipe_fd[0]); wait(0); } }
int main() { int pipe_fd[2]; pipe(pipe_fd);
int pid = fork(); if (pid == -1) { printf("Fork error!\n"); exit(1); } else if (pid == 0) { Filter(pipe_fd); } else { close(pipe_fd[0]); for (int i = 2; i <= 35; i++) { write(pipe_fd[1], &i, 4); } close(pipe_fd[1]); wait(0); } exit(0); }
|
find (moderate)
实现Unix下的find命令,利用递归处理,要注意关闭文件描述符的时机。
最关键的是要理解目录(文件夹)也是一种文件,其目录项就是这个文件的内容,所以我们可以通过read系统调用来读取目录项,进而当读取的内容的类型是一个目录时,即可递归调用Find。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| #include "kernel/types.h" #include "kernel/stat.h" #include "user/user.h" #include "kernel/fs.h"
char* GetName(char* path) { char* p; for (p = path + strlen(path); p >= path && *p != '/'; p--) {} p++; return p; }
void Find(char* dir, char* name) { char buf[512]; char* p; int fd = open(dir, 0); struct dirent de; struct stat st;
if (fd < 0) { printf("Error: open\n"); return; } if (fstat(fd, &st) < 0) { printf("Error: stat\n"); close(fd); return; } if (st.type != T_DIR) { printf("the current the file is not dictionary\n", dir); close(fd); return; }
strcpy(buf, dir); p = buf + strlen(buf); *p++ = '/';
while (read(fd, &de, sizeof(de)) == sizeof(de)) { if (de.inum == 0) continue; if (strcmp(de.name, ".") == 0) continue; if (strcmp(de.name, "..") == 0) continue;
char* cur = p; memmove(cur, de.name, DIRSIZ); cur[DIRSIZ] = 0;
if (stat(buf, &st) < 0) { printf("Error: stat\n"); continue; }
switch (st.type) { case T_FILE: if (strcmp(GetName(buf), name) == 0) { printf("%s\n", buf); } break; case T_DIR: if (strlen(dir) + 1 + DIRSIZ + 1 > sizeof(buf)) { printf("Error: path too long\n"); break; } Find(buf, name); break; } }
close(fd); }
int main(int argc, char* argv[]) { if (argc != 3) { printf("Usage: find <dir> <file_name>\n"); exit(1); } Find(argv[1], argv[2]); exit(0); }
|
xargs (moderate)
简单实现Unix上的xargs命令。简单介绍xargs的用法,就是将标准输入作为xargs的参数。更多的介绍,可以看阮一峰老师的博客:https://ruanyifeng.com/blog/2019/08/xargs-tutorial.html
该命令的可使用fork和exec来实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| #include "kernel/types.h" #include "kernel/stat.h" #include "kernel/param.h" #include "user/user.h"
int main(int argc, char* argv[]) { if (argc < 2) { printf("Usage: xargs <params>\n"); exit(1); }
char* child_argv[MAXARG]; char buf[512] = {'\0'}; int index = 0; for (int i = 1; i < argc; i++) { child_argv[index++] = argv[i]; }
sleep(10);
int n; while ((n = read(0, buf, sizeof(buf))) > 0) { char* p = buf; for (int i = 0; i < n; i++) { if (buf[i] != '\n') continue; if (fork() == 0) { buf[i] = '\0'; child_argv[index] = p; exec(child_argv[0], child_argv); exit(0); } else { p = &buf[i + 1]; wait(0); } } }
exit(0); }
|