MIT XV6 - 1.1 Lab: Xv6 and Unix utilities - sleep

接上文 MIT XV6 - 1. 环境准备

实验正文

打开实验链接 Lab: Xv6 and Unix utilities ,可以看到他一共有6个小实验(头大,让我们争取日拱一卒吧...):

  • Boot xv6 (easy)
  • sleep (easy)
  • pingpong (easy)
  • primes (moderate)/(hard)
  • find (moderate)
  • xargs (moderate)

Boot xv6

第一个直接跳过吧,其实上一篇文章已经做过了,就是

  • 启动
bash 复制代码
make queue
  • 输入ls
bash 复制代码
ls
  • 然后就是两个额外的小技巧

xv6 has no ps command, but, if you type Ctrl-p, the kernel will print information about each process. If you try it now, you'll see two lines: one for init, and one for sh.
To quit qemu type: Ctrl-a x (press Ctrl and a at the same time, followed by x).

sleep

Implement a user-level sleep program for xv6, along the lines of the UNIX sleep command. 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.

实验要求第一条,上来就让你读教材。。我反正老老实实去读了。。虽然读完发现他让我读第一章,我读的是序章???我建议有兴趣你也跟着去读吧 xv6 book

Before you start coding, read Chapter 1 of the xv6 book.

然后就是其他要求或者说步骤吧

  • Put your code in user/sleep.c. Look at some of the other programs in user/ (e.g., user/echo.c, user/grep.c, and user/rm.c) to see how command-line arguments are passed to a 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.
  • 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.
  • sleep's main should call exit(0) when it is done.
  • Look at Kernighan and Ritchie's book The C programming language (second edition) (K&R) to learn about C.

我就不做翻译了直接上修改

  • 新建 user/sleep.c
c 复制代码
#include "kernel/types.h"
#include "user/user.h" // for sleep and exit

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        fprintf(2, "Usage: sleep <seconds>\n");
        exit(1);
    }

    if (sleep(atoi(argv[1])) != 0)
    {
        fprintf(2, "sleep: failed to sleep\n");
        exit(1);
    }

    exit(0);
}

查看他的 user/user.h 可以看到预定义了一些系统调用方法和libc的函数,还是很熟悉的看起来,但是看到他的commit记录,让我有点郁闷

  • 修改Makefile支持编译sleep.c
makefile 复制代码
UPROGS=\
	$U/_cat\
	$U/_echo\
	$U/_forktest\
	$U/_grep\
	$U/_init\
	$U/_kill\
	$U/_ln\
	$U/_ls\
	$U/_mkdir\
	$U/_rm\
	$U/_sh\
	$U/_stressfs\
	$U/_usertests\
	$U/_grind\
	$U/_wc\
	$U/_zombie\
	$U/_sleep\
  • 重新编译~
bash 复制代码
make claen;make qemu
  • 查看我们的sleep可执行文件
bash 复制代码
xv6 kernel is booting

hart 2 starting
hart 1 starting
init: starting sh
$ ls
.              1 1 1024
..             1 1 1024
README         2 2 2305
xargstest.sh   2 3 93
cat            2 4 33520
echo           2 5 32336
forktest       2 6 16136
grep           2 7 36960
init           2 8 32792
kill           2 9 32280
ln             2 10 32088
ls             2 11 35616
mkdir          2 12 32336
rm             2 13 32328
sh             2 14 55112
stressfs       2 15 33200
usertests      2 16 183160
grind          2 17 48544
wc             2 18 34424
zombie         2 19 31680
sleep          2 20 32160
console        3 21 0
$ 

果然有了,至于为什么编译后就自动挂载到了系统中,引用jyy一句话"计算机的世界里没有魔法,冤有头债有主",后面的课程中一定可以发现他是如何实现的

  • 运行 sleep 10
bash 复制代码
sleep 10
  • 运行测试用例 ./grade-lab-util sleep
bash 复制代码
./grade-lab-util sleep
make: `kernel/kernel' is up to date.
== Test sleep, no arguments == sleep, no arguments: OK (0.6s) 
== Test sleep, returns == sleep, returns: OK (0.8s) 
== Test sleep, makes syscall == sleep, makes syscall: OK (0.9s) 

虽然通过了他的测试用例,但不能称为一个好的应用,至少我在xv6系统控制台中随便输入点或者加点管道、重定向什么的,就gg了

相关推荐
dadaobusi10 小时前
学习:RISC-V 虚拟化/H 扩展 语境 VTW 和 VTVM
risc-v
潜创微科技11 小时前
CH591:成本优化的青稞 RISC-V 无线 MCU
c语言·单片机·risc-v·低延迟·沁恒·青稞
2CM_Embed11 小时前
μC/OS-II 内核结构零基础详解(二):任务控制块、就绪表、调度器与中断切换
操作系统
MicrosoftCloud11 小时前
操作系统 02|进程到底被谁推着走?
操作系统·线程·进程·pcb·408·进程状态·调度
坐看风云变幻13 小时前
《系统架构师成长之路》第 2 期|计算机基础:从一次 malloc 看懂内存管理
操作系统
2401_8724187814 小时前
操作系统|进程管理、线程、系统调用、进程间通信与信号
操作系统·pcb·os
艾iYYY1 天前
【无标题】
linux·开发语言·网络·操作系统
打不了嗝 ᥬ᭄3 天前
计算机系统基础:异常、中断与 I/O 的完整脉络
操作系统·计算机系统基础·linux系统调用·异常与中断·io与dma
doujiaozh3 天前
我在 Win7 老电脑上从零手搓了一个操作系统内核:32位分页、中断接管与 Shell 终端
操作系统·汇编语言
玄芯散人3 天前
【元婴·94】ARM vs RISC-V大战
arm开发·risc-v