MIT6.S081 XV6实验环境搭建
Docker 搭建ubuntu环境
获取ubuntu镜像:
1 | docker pull ubuntu |
启动一个容器:
1 | docker run -itw /root --name ubuntu ubuntu bash |
连接ubuntu系统交互环境:
1 | docker start -i ubuntu |
进入系统后更新以及安装vim:
1 | apt update |
更换国内源:
1 | vim /etc/apt/source.list |
将内容替换为(由于没有https,所以用http),注意安装ubuntu版本和源版本是否匹配,如果不匹配就无法自动安装软件相关的依赖。这里docker直接拉取ubuntu:latest,所以直接用最后一个版本的源即可:
1 | deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish main restricted universe multiverse |
替换之后更新:
1 | apt update |
安装实验必需的软件
直接安装即可:
1 | apt install git build-essential gdb-multiarch qemu-system-misc gcc-riscv64-linux-gnu binutils-riscv64-linux-gnu |
下载xv6的实验源码:
1 | git clone git://g.csail.mit.edu/xv6-labs-2021 |
速度较慢,可以我传到gitee上的库:
1 | git clone git@gitee.com:Ansore/xv6-labs-2021.git |
进入目录,切换分支:
1 | cd xv6-labs-2021 |
编译:
1 | make |
ctrl + p可以查看当前进程
ctrl + a x可以退出sh
代码同步
- 可以采用docker映射目录
- 使用git同步
不多赘述
lab1 sleep实验
官方实验描述:
Implement the UNIX program
sleepfor xv6; yoursleepshould pause for a user-specified number of ticks. A tick is a notion of time defined by the xv6 kernel, namely the time between two interrupts from the timer chip. Your solution should be in the fileuser/sleep.c.
Some hints:
- Before you start coding, read Chapter 1 of the xv6 book.
- Look at some of the other programs in
user/(e.g.,user/echo.c,user/grep.c, anduser/rm.c) to see how you can obtain the command-line arguments passed to a program.- If the user forgets to pass an argument, sleep should print an error message.
- The command-line argument is passed as a string; you can convert it to an integer using
atoi(see user/ulib.c).- Use the system call
sleep.- See
kernel/sysproc.cfor the xv6 kernel code that implements thesleepsystem call (look forsys_sleep),user/user.hfor the C definition ofsleepcallable from a user program, anduser/usys.Sfor the assembler code that jumps from user code into the kernel forsleep.- Make sure
maincallsexit()in order to exit your program.- Add your
sleepprogram toUPROGSin Makefile; once you’ve done that,make qemuwill compile your program and you’ll be able to run it from the xv6 shell.- Look at Kernighan and Ritchie’s book The C programming language (second edition) (K&R) to learn about C.
目标:调用系统函数(定义在user/user.h中,实现在kernel/sysproc.c中),实现休眠功能
较为简单,直接调用系统函数即可
代码如下:
1 |
|
此外还需要修改Makefile,URPOGS添加编译:
1 | $U/_sleep\ |
在xv6的shell中执行:
1 | $ make qemu |
执行以下脚本测试是否通过:
1 | ./grade-lab-util sleep |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Ansore!




