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],