前言
该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容器,后执行:
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
# ... lots of output ...
init: starting sh
$
测试
对于完成的程序,如果想要测试,则在Makefile
中的UPROGS中添加:
$U/_<xxx>\
其中的xxx即为程序的名称,如sleep,则为$U/_sleep\
。
之后,可使用如下方法进行测试:
./grade-lab-util xxx
# or make GRADEFLAGS=xxx grade
sleep (easy)
练前开胃菜,使用sleep系统调用。
#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 系统调用在两个进程之间通过一对管道 “乒乓 “传送一个字节,每个管道一个方向。
该程序需要注意的就是对于两个管道的操作,何时关,关哪个?读写顺序又如何?
- 对于父进程,应该先写再读
- 对于子进程,应该先读再写
#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) { // chile: read from the parent
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 { // parent: read from the child
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)
使用管道编写并发版质数筛。
#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); // 一个int为4 byte
}
close(pipe_fd[1]);
wait(0);
}
exit(0);
}
find (moderate)
实现Unix下的find命令,利用递归处理,要注意关闭文件描述符的时机。
最关键的是要理解目录(文件夹)也是一种文件,其目录项就是这个文件的内容,所以我们可以通过read系统调用来读取目录项,进而当读取的内容的类型是一个目录时,即可递归调用Find。
#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来实现。
#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);
// 从标准输入中读取命令到buf中
// 若执行 echo 1 2 3,则标准输入为 1 2 3
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';
// 例如:echo 1 | xargs echo 2
// 在xargs中,标准输入为1,buf中内容为"1\n",child_argv为["echo", "2"],index为2
// 程序执行到此处时,buf中内容变为了"1\0",child_argv变为了["echo", "2", "1"]
child_argv[index] = p;
// exec的第一个参数是要执行的可执行文件的路径
// 第二个参数是作为新命令的参数数组,数组的第一项为新命令的名称
exec(child_argv[0], child_argv);
exit(0);
} else {
// 在父进程中,跳过buf中的\n后,是一条新命令的开始
p = &buf[i + 1];
wait(0);
}
}
}
exit(0);
}