当前位置: 首页 > news >正文

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

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

  • 启动
make queue
  • 输入ls
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
#include "kernel/types.h"
#include "user/user.h" // for sleep and exitint 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
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\
  • 重新编译~
make claen;make qemu
  • 查看我们的sleep可执行文件
xv6 kernel is bootinghart 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
sleep 10
  • 运行测试用例 ./grade-lab-util sleep
./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了

  • 代码已提交
    xv6-learn

http://www.mrgr.cn/news/100417.html

相关文章:

  • 基于云原生架构的后端微服务治理实战指南
  • 【Linux】Centos7 在 Docker 上安装 mysql8.0(最新详细教程)
  • 【C++ 类和数据抽象】消息处理示例(2)
  • SHCTF-REVERSE
  • 6.图的OJ题(1-10,未完)
  • 【Pandas】pandas DataFrame rfloordiv
  • 文心一言开发指南06——千帆大模型平台新手指南
  • 《代码整洁之道》第8章 边界 - 笔记
  • Python 自动化办公:Excel 数据处理的“秘密武器”
  • 技能点总结
  • 【MCP】从一个天气查询服务带你了解MCP
  • 软考:软件设计师考试数据结构知识点详解
  • Redis使用总结
  • linux:进程的替换
  • 剑指Offer(数据结构与算法面试题精讲)C++版——day21
  • git回退commit
  • w~嵌入式C语言~合集4
  • 【上位机——MFC】文档
  • Redis远程链接应用案例
  • CMCC RAX3000M CH EC 算力版刷机(中国移动 RAX3000M 算力版)刷机