MIT 6.S081 实验1 笔记与心得

6S081 OS-LAB

前言:一名研一打工仔,自学MIT操作系统的实验与笔记,留下个脚印吧!
使用英语的目的是使我们提前适应这种阅读代码的环境,不喜勿喷!
后面的实验笔记会陆续上传,共勉!

Lab 1: Xv6 and Unix utilities

Before carry out these labs , you must read materials on the official websites carefully, otherwise, you will be in great trouble.

Materials are the fallowings:
[1]: https://pdos.csail.mit.edu/6.828/2020/xv6/book-riscv-rev1.pdf
[2]: https://pdos.csail.mit.edu/6.828/2020/labs/util.html
[3]: https://swtch.com/~rsc/thread/

sleep(easy)

Implement the UNIX program sleep for xv6

Example:

Run the program from the xv6 shell:

      $ make qemu
      ...
      init: starting sh
      $ sleep 10
      (nothing happens for a little while)
      $
Preperation:

Here , the number behind the function sleep is a user-specified number of ticks, which is not the really time like seconds, minutes, hours. Just attention this.

Reference Items:
/*************************************************************************
    > File Name: sleep.c
    > Author: cbn
    > Mail: cbn@hust.edu.cn 
    > Created Time: 2022年03月24日 星期四 19时56分24秒
 ************************************************************************/

#include "kernel/types.h"
#include "user/user.h"
int main(int argc, char *argv[]){
   
	if(argc != 2){
   
		printf("%s: need one argument!",argv[0]);
		exit(1);
	}
	int time = atoi(argv[1]);
	sleep(time);
	exit(0);
}

pingpong(easy)

Write a program that uses UNIX system calls to ‘‘ping-pong’’ a byte between two processes over a pair of pipes, one for each direction.

Example:

Run the program from the xv6 shell and it should produce the following output:

 	$ make qemu
    ...
    init: starting sh
    $ pingpong
    4: received ping
    3: received pong
    $
preperation:

Create a pair of double-direction pipes.

You should know how to use basic syscalls.

Reference Items:
/*************************************************************************
    > File Name: pingpong.c
    > Author: cbn
    > Mail: cbn@hust.edu.cn 
    > Created Time: 2022年03月24日 星期四 23时35分48秒
 ************************************************************************/

#include "kernel/types.h"
#include "user/user.h"
int main(int argc, char *argv[]){
   
	int p1[2], p2[2];
	pipe(p1);
	pipe(p2);
	if(fork() == 0){
   
		close(p1[1]);
		close(p2[0]);
		char buf;
		if(read(p1[0], &buf, 1)){
   
			printf("%d: received ping\n", getpid());
		}
		write(p2[1], 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值