6.S081 Lab 1: Xv6 and Unix utilities
Xv6 Lab Utilities
6.S081 Lab 1: Xv6 and Unix utilities
參考: Lab: Xv6 and Unix utilities
sleep
Implement the UNIX program sleep for xv6; your sleep should pause for a user-specified number of ticks. A tick is a notion of time defined by the xv6 kernel, namely the time between two interrupts from the timer chip. Your solution should be in the file user/sleep.c.
Some hints:
- Before you start coding, read Chapter 1 of the xv6 book.
- Look at some of the other programs in user/ (e.g., user/echo.c, user/grep.c, and user/rm.c) 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 kernel/sysproc.c for the xv6 kernel code that implements the sleep system call (look for sys_sleep), user/user.h for the C definition of sleep callable from a user program, and user/usys.S for the assembler code that jumps from user code into the kernel for sleep.
- Make sure main calls exit() in order to exit your program.
- Add your sleep program to UPROGS in Makefile; once you’ve done that, make qemu will compile your program and you’ll be able to run it from the xv6 shell.
- Look at Kernighan and Ritchie’s book The C programming language (second edition) (K&R) to learn about C.
build & run:
$ make qemu ... init: starting sh $ sleep 10 (nothing happens for a little while) $pingpong
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. The parent should send a byte to the child; the child should print “<pid>: received ping”, where <pid> is its process ID, write the byte on the pipe to the parent, and exit; the parent should read the byte from the child, print “<pid>: received pong”, and exit. Your solution should be in the file user/pingpong.c.
Some hints:
- Use pipe to create a pipe.
- Use fork to create a child.
- Use read to read from the pipe, and write to write to the pipe.
- Use getpid to find the process ID of the calling process.
- Add the program to UPROGS in Makefile.
- User programs on xv6 have a limited set of library functions available to them. You can see the list in user/user.h; the source (other than for system calls) is in user/ulib.c, user/printf.c, and user/umalloc.c.
Result:
$ make qemu ... init: starting sh $ pingpong 4: received ping 3: received pongprimes
Write a concurrent version of prime sieve using pipes. This idea is due to Doug McIlroy, inventor of Unix pipes. The picture halfway down this page and the surrounding text explain how to do it. Your solution should be in the file user/primes.c.
Your goal is to use pipe and fork to set up the pipeline. The first process feeds the numbers 2 through 35 into the pipeline. For each prime number, you will arrange to create one process that reads from its left neighbor over a pipe and writes to its right neighbor over another pipe. Since xv6 has limited number of file descriptors and processes, the first process can stop at 35.
Some hints:
- Be careful to close file descriptors that a process doesn’t need, because otherwise your program will run xv6 out of resources before the first process reaches 35.
- Once the first process reaches 35, it should wait until the entire pipeline terminates, including all children, grandchildren, &c. Thus the main primes process should only exit after all the output has been printed, and after all the other primes processes have exited.
- Hint: read returns zero when the write-side of a pipe is closed.
- It’s simplest to directly write 32-bit (4-byte) ints to the pipes, rather than using formatted ASCII I/O.
- You should create the processes in the pipeline only as they are needed.
- Add the program to UPROGS in Makefile.
這個程序是參考 《Go語言高級編程》1.6 常見的并發模式 中的那個 Golang 版本寫的。Golang 的并發模型和 UNIX Pipe 本身就很像(refer: Effective Go: Share by communicating),這里只需把 chan 換成 pipe,Goroutine 換成 fork 的進程。但是,一定要、一定要、一定要注意那些在子進程中使用的文件描述符,父進程不用就要關了,不然就涼了。
運行·結果:
$ primes prime 2 prime 3 prime 5 prime 7 prime 11 prime 13 prime 17 prime 19 prime 23 prime 29 prime 31find
Write a simple version of the UNIX find program: find all the files in a directory tree with a specific name. Your solution should be in the file user/find.c.
Some hints:
- Look at user/ls.c to see how to read directories.
- Use recursion to allow find to descend into sub-directories.
- Don’t recurse into “.” and “…”.
- Changes to the file system persist across runs of qemu; to get a clean file system run make clean and then make qemu.
- You’ll need to use C strings. Have a look at K&R (the C book), for example Section 5.5.
- Note that == does not compare strings like in Python. Use strcmp() instead.
- Add the program to UPROGS in Makefile.
主要就是抄 user/ls.c。這個指針玩的,,太騷了[捂臉]。C 還是有意思啊。
結果:
$ echo > b $ mkdir a $ echo > a/b $ find . b ./b ./a/bxargs
Write a simple version of the UNIX xargs program: read lines from the standard input and run a command for each line, supplying the line as arguments to the command. Your solution should be in the file user/xargs.c.
The following example illustrates xarg’s behavior:
$ echo hello too | xargs echo bye bye hello too $Note that the command here is “echo bye” and the additional arguments are “hello too”, making the command “echo bye hello too”, which outputs “bye hello too”.
Please note that xargs on UNIX makes an optimization where it will feed more than argument to the command at a time. We don’t expect you to make this optimization. To make xargs on UNIX behave the way we want it to for this lab, please run it with the -n option set to 1. For instance
$ echo "1\n2" | xargs -n 1 echo line line 1 line 2 $Some hints:
- Use fork and exec to invoke the command on each line of input. Use wait in the parent to wait for the child to complete the command.
- To read individual lines of input, read a character at a time until a newline (’\n’) appears.
- kernel/param.h declares MAXARG, which may be useful if you need to declare an argv array.
- Add the program to UPROGS in Makefile.
- Changes to the file system persist across runs of qemu; to get a clean file system run make clean and then make qemu.
xargs, find, and grep combine well:
$ find . b | xargs grep hellowill run “grep hello” on each file named b in the directories below “.”.
#include "kernel/types.h" #include "kernel/stat.h" #include "user/user.h" #include "kernel/param.h" // MAXARG#define is_blank(chr) (chr == ' ' || chr == '\t') int main(int argc, char *argv[]) {char buf[2048], ch;char *p = buf;char *v[MAXARG];int c;int blanks = 0;int offset = 0;if(argc <= 1){fprintf(2, "usage: xargs <command> [argv...]\n");exit(1);}for (c = 1; c < argc; c++) {v[c-1] = argv[c];}--c;while (read(0, &ch, 1) > 0) {if (is_blank(ch)) {blanks++;continue;}if (blanks) { // 之前有過空格buf[offset++] = 0;v[c++] = p;p = buf + offset;blanks = 0;}if (ch != '\n') {buf[offset++] = ch;} else {v[c++] = p;p = buf + offset;if (!fork()) {exit(exec(v[0], v));}wait(0);c = argc - 1;}}exit(0); }主要是字符串操作麻煩。。
運行:
$ echo hello too | xargs echo bye bye hello too $ find . b ./b ./a/b $ find . b | xargs echo hello hello ./b hello ./a/b參考
Woc,這個 Lab 做了好久。前天寫到 primes,被忘記 close 的 bug 卡了一下,然后就沉迷娛樂,補了半部番,看了兩本輕小說。昨天停電,練了一早上琴,下午又看了兩本輕小說🤦?♂?,就今天才寫完。
By CDFMLR 2020-02-24
頂部圖片來自于小歪API,系隨機選取的圖片,僅用于檢測屏幕顯示的機械、光電性能,與文章的任何內容及觀點無關,也并不代表本人局部或全部同意、支持或者反對其中的任何內容及觀點。如有侵權,聯系刪除。
總結
以上是生活随笔為你收集整理的6.S081 Lab 1: Xv6 and Unix utilities的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux mysql提交_MySQL
- 下一篇: 通过java类的反射机制获取类的属性类型