欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

MIT6s081lab1sleep的实现

时间:2023-06-27

参考材料
https://pdos.csail.mit.edu/6.828/2020/labs/util.html

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.
grep.c echo.c rm.c int main(){} 括号里的参数是int argc, char* argv[]。argc表示参数个数,argv表示内容。

If the user forgets to pass an argument, sleep should print an error message.
要写一个提示错误的语句。printf(“exec errorn”);
exit(1)括号里是1表示出现异常。

The command-line argument is passed as a string; you can convert it to an integer using atoi (see user/ulib.c).
如果传入的数字是10,存储到argv[1]里,调用atoi()把字符类型转换成整型。

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()传入时间参数就好了。他让看sleep的具体实现和汇编代码。

Make sure main calls exit() in order to exit your program.
调用exit(0)正常退出就可以了。

完整代码:

#include "kernel/types.h"#include "user/user.h"int main(int argc, char* argv[]){ if(argc != 2){ printf("exec errorn"); exit(1); } int num=atoi(argv[1]); sleep(num); exit(0);}`

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。