实验一
原实验网址:Lab:Xv6 and Unix utilities
1.sleep
为xv6实现UNIX下的sleep指令,你的sleep指令应该暂停一定的tick数,该数由用户指定
tick是由xv6定义的时间概念,即定时器芯片两个中断间的经过的时间。
你的代码应该放在user/sleep.c文件下
提示
-
写代码前,阅读vx6 book书中第一章代码
-
看看其他程序(
user/echo.c user/grep.c user/rm.c
)是怎样获取命令行参数的 -
如果用户使用sleep指令忘记传参,应该打印提示消息
-
命令行参数以字符串的形式传递,可以使用
atoi(user/ulib.c)
函数将参数转换为整数 -
使用系统调用
sleep
,查看kernel/sysproc.c
下xv6内核代码对sleep
系统调用的实现(sys_sleep
),以及user/user.h
对从用户程序中调用sleep
C函数的声明,以及user/usys.S
汇编文件中,从用户代码进入内核的函数sleep
-
确保你写的
sleep.c
主函数调用exit
函数退出 -
把你写好的
sleep.c
源文件编译成可执行文件_sleep
后,加入Makefile
里的UPROGS
。#include "kernel/types.h" #include "user/user.h" #include "kernel/stat.h" int main(int argc,const char* argv[]) { if (2 != argc) { fprintf(2,"Usage:sleep [time]\n"); exit(1); } sleep(atoi(argv[1])); exit(0); }
2.pingpong
用一对管道在父子进程之间传递字节,子进程从管道1中读,当子进程收到父进程往管道1中写入的字节时,打印<pid>: received ping。
父进程从管道2中读,当父进程收到子进程往管道2中写入的字节时,打印<pid>: received pong。
#include "kernel/types.h"
#include "user/user.h"
#include "kernel/stat.h"
int main(int argc,const char* args[])
{
int child;
int pipefd[2];
int pipefd1[2];
if (-1 == pipe(pipefd) || -1 == pipe(pipefd1))
exit(1);
char ch;
if (0 == (child = fork()))
{
close(pipefd[1]);//关闭管道0的写
close(pipefd1[0]);//关闭管道1的读
read(pipefd[0],&ch,1);
printf("%d: received ping\n",getpid());
write(pipefd1[1],"1",1);
close(pipefd[0]);
close(pipefd1[1]);
exit(0);
}
else
{
close(pipefd[0]