redirect()
, twd2 认为能够往 xv6 中加一个 dup2 函数,我表示赞成。须要进一步研究 xv6 结构,肯定如何添加。遇到的其余问题包括:c++
uint
类型了。经检查发现用户态程序须要手动 #include "kernel/types.h"
,否则会爆炸。测试结果:git
$ make grade # ... lines omitted Score: 100/100
实验指导链接bash
上来直接:ide
$ cd xv6-riscv-fall19 $ git checkout util
实验指导简要介绍了如何把 xv6 跑起来(make
then make qemu
),如何交做业(make handin
),如何测试成绩(make grade
)。函数
下面介绍各个子任务如何写。测试
顾名思义写一个 sleep 例程,休眠必定的 tick 数,tick 的定义是时间中断。ui
Hints:
Look at some of the other programs in user/ to see how you can obtain the command-line arguments passed to a program. If the user forgets to pass an argument, sleep should print an error message.
The command-line argument is passed as a string; you can convert it to an integer using atoi (see user/ulib.c).
Use the system call sleep (see user/usys.S and kernel/sysproc.c).
Make sure main calls exit() in order to exit your program.
Add the program to UPROGS in Makefile and compile user programs by typing make fs.img.
Look at Kernighan and Ritchie's book The C programming language (second edition) (K&R) to learn about C.code
首先 make clean
,而后照猫画虎写一下 user/sleep.c
:htm
#include "kernel/types.h" #include "user/user.h" int main(int argc, char *argv[]) { if (argc != 2) write(2, "Error message", strlen("Error message")); int x = atoi(argv[1]); sleep(x); exit(); }
按照说明修改 Makefile ,给 UPROG
变量追加一个项目,运行便可。
如法炮制。
#include "kernel/types.h" #include "user/user.h" int main(int argc, char *argv[]) { int parent_fd[2], child_fd[2]; pipe(parent_fd); pipe(child_fd); char buf[64]; if (fork()) { // Parent write(parent_fd[1], "ping", strlen("ping")); read(child_fd[0], buf, 4); printf("%d: received %s\n", getpid(), buf); } else { // Child read(parent_fd[0], buf, 4); printf("%d: received %s\n", getpid(), buf); write(child_fd[1], "pong", strlen("pong")); } exit(); }
照猫画虎。
负责个人课程助教 twd2 认为,redirect(int, int[])
这个函数有问题,缘由在于 xv6 没有 dup2(int, int)
,这个函数是网上抄的。
#include "kernel/types.h" #include "user/user.h" void source() { int i; for (i = 2; i < 36; i++) { write(1, &i, sizeof(i)); } } void cull(int p) { int n; while (read(0, &n, sizeof(n))) { if (n % p != 0) { write(1, &n, sizeof(n)); } } } void redirect(int k, int pd[]) { close(k); dup(pd[k]); close(pd[0]); close(pd[1]); } void sink() { int pd[2]; int p; if (read(0, &p, sizeof(p))) { printf("prime %d\n", p); pipe(pd); if (fork()) { redirect(0, pd); sink(); } else { redirect(1, pd); cull(p); } } } int main(int argc, char *argv[]) { int pd[2]; pipe(pd); if (fork()) { redirect(0, pd); sink(); } else { redirect(1, pd); source(); } exit(); }
Bonus 是加 regex 支持,好,抄 user/grep.c
便可。
#include "kernel/types.h" #include "kernel/stat.h" #include "user/user.h" #include "kernel/fs.h" char* fmtname(char *path) { static char buf[DIRSIZ+1]; char *p; // Find first character after last slash. for(p=path+strlen(path); p >= path && *p != '/'; p--) ; p++; // Return blank-padded name. if(strlen(p) >= DIRSIZ) return p; memmove(buf, p, strlen(p)); memset(buf+strlen(p), ' ', DIRSIZ-strlen(p)); return buf; } // Regexp matcher from Kernighan & Pike, // The Practice of Programming, Chapter 9. int matchhere(char*, char*); int matchstar(int, char*, char*); int match(char *re, char *text) { if(re[0] == '^') return matchhere(re+1, text); do{ // must look at empty string if(matchhere(re, text)) return 1; }while(*text++ != '\0'); return 0; } // matchhere: search for re at beginning of text int matchhere(char *re, char *text) { if(re[0] == '\0') return 1; if(re[1] == '*') return matchstar(re[0], re+2, text); if(re[0] == '$' && re[1] == '\0') return *text == '\0'; if(*text!='\0' && (re[0]=='.' || re[0]==*text)) return matchhere(re+1, text+1); return 0; } // matchstar: search for c*re at beginning of text int matchstar(int c, char *re, char *text) { do{ // a * matches zero or more instances if(matchhere(re, text)) return 1; }while(*text!='\0' && (*text++==c || c=='.')); return 0; } void find(char *path, char *re) { char buf[512], *p; int fd; struct dirent de; struct stat st; if((fd = open(path, 0)) < 0){ fprintf(2, "find: cannot open %s\n", path); return; } if(fstat(fd, &st) < 0){ fprintf(2, "find: cannot stat %s\n", path); close(fd); return; } switch(st.type){ case T_FILE: if(match(re, fmtname(path))) printf("%s\n", path); break; case T_DIR: if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){ printf("find: 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; memmove(p, de.name, DIRSIZ); p[DIRSIZ] = 0; if(stat(buf, &st) < 0){ printf("find: cannot stat %s\n", buf); continue; } if(strlen(de.name) == 1 && de.name[0] == '.') continue; if(strlen(de.name) == 2 && de.name[0] == '.' && de.name[1] == '.') continue; find(buf, re); } break; } close(fd); } int main(int argc, char *argv[]) { if(argc <= 2) fprintf(2, "find: not enough params provided"); find(argv[1], argv[2]); exit(); }
须要参考 user/sh.c
、user/cat.c
两个例程。
#include "kernel/types.h" #include "user/user.h" int main(int argc, char *argv[]) { char buf2[512]; char buf[32][32]; char *pass[32]; for (int i = 0; i < 32; i++) pass[i] = buf[i]; int i; for (i = 1; i < argc; i++) strcpy(buf[i - 1], argv[i]); int n; while ((n = read(0, buf2, sizeof(buf2))) > 0) { int pos = argc - 1; char *c = buf[pos]; for (char *p = buf2; *p; p++) { if (*p == ' ' || *p == '\n') { *c = '\0'; pos++; c = buf[pos]; } else *c++ = *p; } *c = '\0'; pos++; pass[pos] = 0; if (fork()) { wait(); } else exec(pass[0], pass); } if (n < 0) { printf("xargs: read error\n"); exit(); } exit(); }