Docker 搭建ubuntu环境

获取ubuntu镜像:

1
docker pull ubuntu

启动一个容器:

1
docker run -itw /root --name ubuntu ubuntu bash

连接ubuntu系统交互环境:

1
docker start -i ubuntu

进入系统后更新以及安装vim:

1
2
apt update
apt install vim

更换国内源:

1
vim /etc/apt/source.list

将内容替换为(由于没有https,所以用http),注意安装ubuntu版本和源版本是否匹配,如果不匹配就无法自动安装软件相关的依赖。这里docker直接拉取ubuntu:latest,所以直接用最后一个版本的源即可:

1
2
3
4
5
6
7
8
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-updates main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-backports main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-security main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-security 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
2
cd xv6-labs-2021
git checkout util

编译:

1
2
make
make qemu

ctrl + p可以查看当前进程

ctrl + a x可以退出sh

代码同步

  1. 可以采用docker映射目录
  2. 使用git同步

不多赘述

lab1 sleep实验

官方实验描述:

Implement the UNIX program sleep for xv6; your sleep should 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 file user/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, and user/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.c for the xv6 kernel code that implements the sleep system call (look for sys_sleep), user/user.h for the C definition of sleep callable from a user program, and user/usys.S for the assembler code that jumps from user code into the kernel for sleep.
  • Make sure main calls exit() in order to exit your program.
  • Add your sleep program to UPROGS in Makefile; once you’ve done that, make qemu will 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
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 <= 1) {
fprintf(2, "usage: sleep seconds\n");
exit(1);
}
sleep(atoi(argv[1]));
exit(0);
return 0;
}

此外还需要修改Makefile,URPOGS添加编译:

1
$U/_sleep\

在xv6的shell中执行:

1
2
3
4
5
6
$ make qemu
...
init: starting sh
$ sleep 10
(nothing happens for a little while)
$

执行以下脚本测试是否通过:

1
./grade-lab-util sleep