2021 MIT6.S081 LAB1

这篇博客介绍了MIT6.S081操作系统的实验内容,包括Xv6系统下的sleep指令实现、pingpong进程交互、寻找素数的多进程协作、find命令的递归目录搜索以及xargs指令的参数处理。实验强调理解系统调用、进程通信以及Unix工具的使用。
摘要由CSDN通过智能技术生成

实验一


原实验网址: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对从用户程序中调用sleepC函数的声明,以及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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值