XV6学习(1) Lab util

2023-07-31,,

正在学习MIT的6.S081,把做的实验写一写吧。

实验的代码放在了Github上。

第一个实验是Lab util,算是一个热身的实验,没有涉及到系统的底层,就是使用系统调用来完成几个用户模式的小程序。

Boot xv6 (easy)

启动XV6,按照文档执行就ok了。

$ git clone git://g.csail.mit.edu/xv6-labs-2020
$ cd xv6-labs-2020
$ git checkout util
$ make qemu

在XV6中没有ps命令,而是使用Ctrl+p来查看正在运行的进程。

sleep (easy)

这一个就是仿照已有的程序调用一下sleep的系统调用就行了。

在Unix系统里面,默认情况下0代表stdin1代表stdout2代表stderr。这3个文件描述符在进程创建时就已经打开了的(从父进程复制过来的),可以直接使用。而分配文件描述符的时候是从当前未使用的最小的值来分配,因此可以关闭某个文件描述符再通过opendup将该文件描述符分配给其他文件或pipe来实现输入输出重定向。

有一个小坑就是程序执行结束后要用exit(0)来退出,而不是return 0

#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h" int
main(int argc, char *argv[])
{
if(argc < 2){
fprintf(2, "Usage: sleep [time]\n");
exit(1);
} int time = atoi(argv[1]);
sleep(time);
exit(0);
}

pingpong (easy)

这一个实验就是用pipe打开两个管道,然后fork出一个子进程,完成要求的操作就行了。

fork函数是一次调用两次返回的函数,在父进程中返回子进程的pid,在子进程中返回0。

#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h" int
main(int argc, char *argv[]){
int p2c[2];
int c2p[2];
if(pipe(p2c) < 0){
printf("pipe");
exit(-1);
}
if(pipe(c2p) < 0){
printf("pipe");
exit(-1);
}
int pid = fork();
if(pid == 0){
// child
char buf[10];
read(p2c[0], buf, 10);
printf("%d: received ping\n", getpid());
write(c2p[1], "o", 2);
}else if(pid > 0){
// parent
write(p2c[1], "p", 2);
char buf[10];
read(c2p[0], buf, 10);
printf("%d: received pong\n", getpid());
}
close(p2c[0]);
close(p2c[1]);
close(c2p[0]);
close(c2p[1]);
exit(0);
}

primes (moderate)/(hard)

这一个是实现管道的发明者Doug McIlroy提出的计算素数的方法,该方法类似于筛法,不过是用管道和多线程实现的。

main函数中父进程创建了一个管道,输入2~35。之后通过prime函数来实现输出,如果读取到了超过两个的数,就创建一个新进程来进行后续处理。

#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h" void prime(int rd){
int n;
read(rd, &n, 4);
printf("prime %d\n", n);
int created = 0;
int p[2];
int num;
while(read(rd, &num, 4) != 0){
if(created == 0){
pipe(p);
created = 1;
int pid = fork();
if(pid == 0){
close(p[1]);
prime(p[0]);
return;
}else{
close(p[0]);
}
}
if(num % n != 0){
write(p[1], &num, 4);
}
}
close(rd);
close(p[1]);
wait(0);
} int
main(int argc, char *argv[]){
int p[2];
pipe(p); int pid = fork();
if(pid != 0){
// first
close(p[0]);
for(int i = 2; i <= 35; i++){
write(p[1], &i, 4);
}
close(p[1]);
wait(0);
}else{
close(p[1]);
prime(p[0]);
close(p[0]);
}
exit(0);
}

find (moderate)

这一个就是仿照ls.c中的方法,对当前目录排除掉...后进行递归遍历,同时对路径名进行匹配,匹配到了就输出。不过库中没有提供strstr函数,只能自己写了个\(O(n^2)\)的子串匹配算法。

#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
#include "kernel/fs.h" void match(const char* path, const char* name){
//printf("%s %s", path, name);
int pp = 0;
int pa = 0;
while(path[pp] != 0){
pa = 0;
int np = pp;
while(name[pa] != 0){
if (name[pa] == path[np]){
pa++;
np++;
}
else
break;
}
if(name[pa] == 0){
printf("%s\n", path);
return;
}
pp++;
}
} void find(char *path, char *name){
char buf[512], *p;
int fd;
struct dirent de;
struct stat st; if((fd = open(path, 0)) < 0){
fprintf(2, "ls: cannot open %s\n", path);
return;
} if(fstat(fd, &st) < 0){
fprintf(2, "ls: cannot stat %s\n", path);
close(fd);
return;
}
switch(st.type){
case T_FILE:
// printf("%s %d %d %l\n", path, st.type, st.ino, st.size);
match(path, name);
break; case T_DIR:
if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){
printf("ls: path too long\n");
break;
}
strcpy(buf, path);
p = buf+strlen(buf);
*p++ = '/';
while(read(fd, &de, sizeof(de)) == sizeof(de)){
if(de.inum == 0)
continue;
if(de.name[0] == '.' && de.name[1] == 0) continue;
if(de.name[0] == '.' && de.name[1] == '.' && de.name[2] == 0) continue;
memmove(p, de.name, DIRSIZ);
p[DIRSIZ] = 0;
if(stat(buf, &st) < 0){
printf("ls: cannot stat %s\n", buf);
continue;
}
find(buf, name);
}
break;
}
close(fd);
} int
main(int argc, char *argv[]){
if (argc < 3){
printf("Usage: find [path] [filename]\n");
exit(-1);
}
find(argv[1], argv[2]);
exit(0);
}

xargs (moderate)

这一个就是从输入中构造出新的argc数组,然后用forkexec执行就行了。大部分时间都用在输入的处理上面了。。库里面没有提供readline函数和split,只能自己写。

#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h" char* readline() {
char* buf = malloc(100);
char* p = buf;
while(read(0, p, 1) != 0){
if(*p == '\n' || *p == '\0'){
*p = '\0';
return buf;
}
p++;
}
if(p != buf) return buf;
free(buf);
return 0;
} int
main(int argc, char *argv[]){
if(argc < 2) {
printf("Usage: xargs [command]\n");
exit(-1);
}
char* l;
argv++;
char* nargv[16];
char** pna = nargv;
char** pa = argv;
while(*pa != 0){
*pna = *pa;
pna++;
pa++;
}
while((l = readline()) != 0){
//printf("%s\n", l);
char* p = l;
char* buf = malloc(36);
char* bh = buf;
int nargc = argc - 1;
while(*p != 0){
if(*p == ' ' && buf != bh){
*bh = 0;
nargv[nargc] = buf;
buf = malloc(36);
bh = buf;
nargc++;
}else{
*bh = *p;
bh++;
}
p++;
}
if(buf != bh){
nargv[nargc] = buf;
nargc++;
}
nargv[nargc] = 0;
free(l);
int pid = fork();
if(pid == 0){
// printf("%s %s\n", nargv[0], nargv[1]);
exec(nargv[0], nargv);
}else{
wait(0);
}
}
exit(0);
}

XV6学习(1) Lab util的相关教程结束。

《XV6学习(1) Lab util.doc》

下载本文的Word格式文档,以方便收藏与打印。